| |
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();
}
}
}
|
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------------------------------ |