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 : 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 : 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 2. The "Shallow copy".

At this article we will be focused at the problems which are occured during the shallow copy process. Please, take a look at the example below and at the test results. You may found:

int, string, double are the value types. They are independent at each object instance. That way the "amount" as well as "total sum" are unchanged for the storehouse report. From the other hand, the description list are shared between object instances. That is why the descriptions are changed not only for f1 instance of "flatiron" object, but also for f2 instance of "flatiron" and for the main "flatiron's" prototype at the "storehouse" object manager.

 

   
  Prototype Pattern, C# implementation, part2:

//Prototype design pattern implementation in C#, part 2

//------------------------------------
using System;
using System.Collections;

namespace PrototypePattern
{
// "Prototype"
abstract class GoodsProto
{
// Methods
public abstract GoodsProto Clone();
}
// "ConcretePrototype"
class Goods : GoodsProto
{
// Fields
private string m_title;
private ArrayList m_descr;
private int m_group;
private double m_prise;
private int m_qty;


// Constructor
public Goods( string title, int group, double prise, int qty, ArrayList descr)
{
m_title = title;
m_group = group;
m_prise = prise;
m_qty = qty;
m_descr = descr;
}

// Methods
public override GoodsProto Clone()
{
// Creates a 'shallow copy'
return (GoodsProto) this.MemberwiseClone();
}
public void SetPrise(double prise)
{
m_prise = prise;
}
public void SetQty(int qty)
{
m_qty = qty;
}
public void SetTitle(string title)
{
m_title = title;
}
public void AddDescr(string descr)
{
m_descr.Add(descr);
}

public double PrintInfo()
{
Console.Write( "{0} \t {1} \t {2} \t {3} \t : {4}",
m_title, m_group, m_prise, m_qty, m_prise*m_qty );

for (int i=0; i<m_descr.Count; i++)
{
Console.Write( " Descr {0} : {1} ", i, m_descr[i]);
}

Console.WriteLine("");

return m_prise*m_qty;
}
}

// Prototype manager
class StoreHouse
{
private Hashtable m_goods = new Hashtable();

// Indexers
public GoodsProto this[ string name ]
{
get{ return (GoodsProto)m_goods[ name ]; }
set{ m_goods.Add( name, value ); }
}
public void PrintInfo()
{
double sum = 0;
Goods g = null;
Console.WriteLine("-Storehouse report begin-------------------------------------");
Console.WriteLine( "Goods title: group: prise: qty: Amount: Description:");
foreach (DictionaryEntry e in m_goods)
{
g = (Goods)e.Value;
sum += g.PrintInfo();
}
Console.WriteLine("==================================== Storehouse Total : {0}", sum);
Console.WriteLine("-Storehouse report end---------------------------------------");
}
}

class PrototypeTest
{
public static void Main( string[] args )
{
StoreHouse storehouse = new StoreHouse();

// Initialize with standard goods
storehouse[ "flatiron" ] = new Goods( "flatiron", 1, 20.1, 10, new ArrayList() );
storehouse[ "mobile nokia" ] = new Goods( "mobile nokia", 2, 7.2, 15, new ArrayList() );
storehouse[ "plate" ] = new Goods( "simple plate", 3, 3.5, 120, new ArrayList() );

storehouse.PrintInfo();

string goodsTitle = "flatiron";
Goods f1 = (Goods)storehouse[ goodsTitle ].Clone();
f1.AddDescr("d1");
Console.WriteLine("Flatiron object, instance f1");
f1.PrintInfo();

Goods f2 = (Goods)storehouse[ goodsTitle ].Clone();
f2.AddDescr("d2");
Console.WriteLine("Flatiron object, instance f2");
f2.PrintInfo();

Console.WriteLine("Flatiron object, instance f1, Qty = 9");
f1.SetQty(9); f1.SetTitle("Flatiron f1");
f1.PrintInfo();

Console.WriteLine("Flatiron object, instance f2");
f2.PrintInfo();

storehouse.PrintInfo();

Console.Read();
}
}
}

Design Patterns. Prototype. C#.


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



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