| |
Decorator Pattern, C# implementation:
//Decorator design pattern implementation in C#
//------------------------------------
using System;
using System.Collections;
namespace DecoratorPattern
{
// "Prototype"
abstract class GoodsProto
{
// Methods
public abstract GoodsProto Clone();
}
// "ConcretePrototype"
class Goods : GoodsProto
{
// Fields
private string m_title = "";
private ArrayList m_descr = null;
private double m_prise = 0.0;
private int m_qty = 0;
// Constructor
public Goods( string title, double prise, int qty, ArrayList descr )
{
m_title = title;
m_prise = prise;
m_qty = qty;
m_descr = descr;
}
public Goods( Goods g )
{
m_title = g.GetTitle();
m_prise = g.GetPrise();
m_qty = g.GetQty();
if (m_descr == null)
m_descr = new ArrayList();
}
// Methods
public override GoodsProto Clone()
{
Goods g = new Goods(m_title, m_prise, m_qty, new ArrayList());
for (int i=0; i<m_descr.Count; i++)
g.AddDescr(m_descr[i].ToString());
return g;
}
public double GetPrise() { return m_prise; }
public void SetPrise(double d) { m_prise=d; }
public void SetQty(int i) { m_qty = i; }
public int GetQty() {return m_qty;}
public void SetTitle(string Title) { m_title = Title; }
public string GetTitle() { return m_title; }
public void AddDescr(string descr) { m_descr.Add(descr); }
public double PrintInfo()
{
Console.Write( " {0} \t {1} \t {2} \t {3}",
m_title, 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;
}
}
// "Decorator"
//=========================
abstract class Decorator : Goods
{
// Fields
protected Goods goods = null;
// Constructors
public Decorator (Goods g)
: base( g ) { this.goods = g; }
public new void PrintInfo()
{
goods.PrintInfo();
}
}
// "ConcreteDecorator"
class Borrow : Decorator
{
// Fields
protected ArrayList borrowers = new ArrayList();
// Constructors
public Borrow( Goods g )
: base( g ) {}
// Methods
public void BorrowGoods( string name )
{
borrowers.Add( name );
goods.SetQty(base.goods.GetQty()-1);
}
public new void PrintInfo()
{
base.PrintInfo();
foreach( string borrower in borrowers )
Console.WriteLine( " borrower: {0}", borrower );
}
}
//==========================================
// 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( "Title: 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---------------------------------------\n");
}
}
//=============================================
class PrototypeTest
{
public static void Main( string[] args )
{
StoreHouse storehouse = new StoreHouse();
// Initialize with standard goods
storehouse[ "cd" ] = new Goods( "video-cd ", 7.2, 10, new ArrayList());
storehouse[ "dvd" ] = new Goods( "video-dvd ", 9.1, 15, new ArrayList());
storehouse.PrintInfo();
string goodsTitle = "dvd";
Goods f1 = (Goods)storehouse[ goodsTitle ].Clone();
f1.AddDescr("dvd, Ameli");
f1.PrintInfo();
Console.WriteLine("++Object f1, Qty = 9, title = 'Ameli, Fr.'");
f1.SetQty(9); f1.SetTitle("Ameli, Fr.");
f1.PrintInfo();
// Make video borrowable, then borrow and display
Console.WriteLine( "\n++Object f1 - design pattern 'decorator' implementation begin:" );
Borrow b = new Borrow(f1);
b.BorrowGoods( "First borrower" );
b.BorrowGoods( "Second borrower" );
b.PrintInfo();
Console.WriteLine("\n++Object f1:");
f1.PrintInfo();
storehouse.PrintInfo();
Console.Read();
}
}
}
|
Test results are below:
-Storehouse report begin-------------------------
Title: prise: qty: Amount: Description:
video-dvd 9.1 15 136.5
video-cd 7.2 10 72
============================== Storehouse Total : 208.5
-Storehouse report end---------------------------
video-dvd 9.1 15 136.5 Descr 0 : dvd, Ameli
++Object f1, Qty = 9, title = 'Ameli, Fr.'
Ameli, Fr. 9.1 9 81.9 Descr 0 : dvd, Ameli
++Object f1 - design pattern 'decorator' implementation begin:
Ameli, Fr. 9.1 7 63.7 Descr 0 : dvd, Ameli
borrower: First borrower
borrower: Second borrower
++Object f1:
Ameli, Fr. 9.1 7 63.7 Descr 0 : dvd, Ameli
-Storehouse report begin--------------------------
Title: prise: qty: Amount: Description:
video-dvd 9.1 15 136.5
video-cd 7.2 10 72
============================= Storehouse Total : 208.5
-Storehouse report end------------------------- |