| |
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;
}
}
|
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:
|