SoftPilot.2000 Software Outsourcing Ukraine : Software development. Design Patterns. C#. Creational Patterns. Prototype. dotNet. SoftPilot.2000 Software Outsourcing Ukraine : Software development. Design Patterns. C#. Creational Patterns. Prototype. dotNet.
SoftPilot.2000 Software Outsourcing Ukraine : Software development. Design Patterns. C#. Creational Patterns. Prototype. dotNet.
SoftPilot.2000 Software Outsourcing Ukraine : Software development. Design Patterns. C#. Creational Patterns. Prototype. dotNet.
SoftPilot.2000 Software Outsourcing Ukraine : Software development. Design Patterns. C#. Creational Patterns. Prototype. dotNet.
softPilot.2000 : Software Outsourcing from Ukraine - Site Map SoftPilot.2000 Software Outsourcing Ukraine. Design Patterns. C#. Creational Patterns. Prototype.
SoftPilot.2000 - Software Outsourcing Ukraine : Software development. Design Patterns. C#. Creational Patterns. Prototype. dotNet. SoftPilot.2000 - Software Outsourcing Ukraine : Software development. Design Patterns. C#. Creational Patterns. Prototype. dotNet. SoftPilot.2000 - Software Outsourcing Ukraine : Software development. Design Patterns. C#. Creational Patterns. Prototype. dotNet. SoftPilot.2000 - Software Outsourcing Ukraine : Software development. Design Patterns. C#. Creational Patterns. Prototype. dotNet. SoftPilot.2000 - Software Outsourcing Ukraine : Software development. Design Patterns. C#. Creational Patterns. Prototype. dotNet. SoftPilot.2000 - Software Outsourcing Ukraine : Software development. Design Patterns. C#. Creational Patterns. Prototype. dotNet. SoftPilot.2000 - Software Outsourcing Ukraine : Software development. Design Patterns. C#. Creational Patterns. Prototype. dotNet.
SoftPilot.2000 : Software Outsourcing Ukraine- Home SoftPilot.2000 : Software Outsourcing Ukraine - About SoftPilot.2000 : Software Outsourcing Ukraine - Company Profile SoftPilot.2000 : Software Outsourcing Ukraine - Case Study SoftPilot.2000 : Software Outsourcing Ukraine - Services SoftPilot.2000 : Software Outsourcing Ukraine - Partners SoftPilot.2000 : Software Outsourcing Ukraine - Contacts SoftPilot.2000 : Software Outsourcing Ukraine - E-mail SoftPilot.2000 : Software Outsourcing to Ukraine

SoftPilot.2000 - Software Outsourcing from Ukraine.

Software development articles about design patterns implementation in C#.

Creational Patterns.




Prototype, part 3. The "Deep copy".

At the previous article the "shallow copy" technique been explored during the Prototype design pattern implementation. As you may found, there is key point of the prtotype pattern realization in the "clone()" method, which should return the copy of the object instance. The "this.MemberwiseClone()" method return the "shallow copy" of the object and work perfect for value types ( which are the primitive types like int, double, string in general ) of the object fileds - they are independent in the cloned objects. But the reference types ( the ArrayList in our example which stores the descriptions of the goods ) are shared between all instances of the class. This nuance should be known by each developer needed to implement the prototype design pattern functionality during application development. Ok, the problem is highlighetd. What should we do with the refernce types? It is often needed to hold them separately for each object instantiated. The "deep copy" technique will help us.

You may found below two methods of the "Deep Copy" implementation. First is close to the C++ technique, really fast but it take to pay attention to the internal types of the class members, because it is the type-dependent method in realization. The second method based on the "BinaryFormatter" is type-independent but slower. The using of this method is justifyed if you are storing your object hierarhy at harddrive.

 

   
 

Please, take a look at one of the possible methods of the "deep copy" realization for the Clone() method:

// Methods
public override GoodsProto Clone()
{

// Creates a 'deep copy'
  Goods g = new Goods(m_title, m_group, m_prise, m_qty, new ArrayList());
  for (int i=0; i<m_descr.Count; i++)
    g.AddDescr(m_descr[i].ToString());

return g;

}

The results are below:

-Storehouse report begin-------------------------------------
Goods title: group: prise: qty: Amount: Description:
simple plate 3 3.5 120 : 420
flatiron 1 20.1 10 : 201
mobile nokia 2 7.2 15 : 108
==================================== Storehouse Total : 729
-Storehouse report end---------------------------------------
Flatiron object, instance f1
flatiron 1 20.1 10 : 201 Descr 0 : d1
Flatiron object, instance f2
flatiron 1 20.1 10 : 201 Descr 0 : d2
Flatiron object, instance f1, Qty = 9
Flatiron f1 1 20.1 9 : 180.9 Descr 0 : d1
Flatiron object, instance f2
flatiron 1 20.1 10 : 201 Descr 0 : d2
-Storehouse report begin-------------------------------------
Goods title: group: prise: qty: Amount: Description:
simple plate 3 3.5 120 : 420
flatiron 1 20.1 10 : 201
mobile nokia 2 7.2 15 : 108
==================================== Storehouse Total : 729
-Storehouse report end---------------------------------------

We could say: "perfect, the problem is solved". Frankly speaking, this is a good and fast method, close to the "copy constructor" C++ technique. But, this method doesn't such flexible how we could expect. At our situation it works good ( and fast ), but we are using the string values for the ArrayList of Descriptions and needs to treat them properly in different manner for different cases. In other words, this technique is type-dependent.

At the end of our investigation lets try to implement the "deep copy" method which is type-independent. We will use BinaryFormatter for the objects serialization / deserialization.

1. Add following namespaces:
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

2. The GoodsProto() and Goods() classes should be marked as serializable:

// "Prototype"
[Serializable()] abstract class GoodsProto
{
// Methods
public abstract GoodsProto Clone();
}
// "ConcretePrototype"
[Serializable()] class Goods : GoodsProto
{
// Fields
... Skipped.

3. Finally, below is the Clone() method realization with BinaryFormatter

public override GoodsProto Clone()
{
  GoodsProto g;
  Stream stream;
  BinaryFormatter bFormatter = new BinaryFormatter();
  try
  {
    stream = File.Open("tmp.dat", FileMode.Create);
    bFormatter.Serialize(stream, this);
    stream.Close();
    stream = File.Open("tmp.dat", FileMode.Open);
    g = (GoodsProto)bFormatter.Deserialize(stream);
    stream.Close();
    return g;
  }
  catch(Exception ex)
  {
    Console.WriteLine (ex.ToString());
    Console.Read();
     return null;
 }
}


Design Patterns. Prototype. C#.


The "BinaryFormatter" results below:

-Storehouse report begin------------------------------
Goods title: group: prise: qty: Amount: Description:
simple plate 3 3.5 120 : 420
flatiron 1 20.1 10 : 201
mobile nokia 2 7.2 15 : 108
=============================== Storehouse Total : 729
-Storehouse report end-----------------------------
Flatiron object, instance f1
flatiron 1 20.1 10 : 201 Descr 0 : d1
Flatiron object, instance f2
flatiron 1 20.1 10 : 201 Descr 0 : d2
Flatiron object, instance f1, Qty = 9
Flatiron f1 1 20.1 9 : 180.9 Descr 0 : d1
Flatiron object, instance f2
flatiron 1 20.1 10 : 201 Descr 0 : d2
-Storehouse report begin-----------------------------
Goods title: group: prise: qty: Amount: Description:
simple plate 3 3.5 120 : 420
flatiron 1 20.1 10 : 201
mobile nokia 2 7.2 15 : 108
============================== Storehouse Total : 729
-Storehouse report end----------------------------

Link Partners:




 
   
(C) SoftPilot.2000 All Rights Reserved. 2000 - 2006