SoftPilot.2000 Software Outsourcing Ukraine, Design Patterns, C#, Decorator, Software development, dot net, structural patterns SoftPilot.2000 Software Outsourcing Ukraine, Design Patterns, C#, Decorator, Software development, dot net, structural patterns
SoftPilot.2000 Software Outsourcing Ukraine, Design Patterns, C#, Decorator, Software development, dot net, structural patterns
SoftPilot.2000 Software Outsourcing Ukraine, Design Patterns, C#, Decorator, Software development, dot net, structural patterns
SoftPilot.2000 Software Outsourcing Ukraine, Design Patterns, C#, Decorator, Software development, dot net, structural patterns
softPilot.2000 : Software Outsourcing from Ukraine - Site Map SoftPilot.2000 Software Outsourcing Ukraine, Design Patterns, C#, Decorator, Software development, dot net, structural patterns
SoftPilot.2000 - Software Outsourcing Ukraine, Design Patterns, C#, Decorator, Software development, dot net, structural patterns SoftPilot.2000 - Software Outsourcing Ukraine, Design Patterns, C#, Decorator, Software development, dot net, structural patterns SoftPilot.2000 - Software Outsourcing Ukraine, Design Patterns, C#, Decorator, Software development, dot net, structural patterns SoftPilot.2000 - Software Outsourcing Ukraine, Design Patterns, C#, Decorator, Software development, dot net, structural patterns SoftPilot.2000 - Software Outsourcing Ukraine, Design Patterns, C#, Decorator, Software development, dot net, structural patterns SoftPilot.2000 - Software Outsourcing Ukraine, Design Patterns, C#, Decorator, Software development, dot net, structural patterns SoftPilot.2000 - Software Outsourcing Ukraine, Design Patterns, C#, Decorator, Software development, dot net, structural patterns
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 Ukraine.

Software development articles about design patterns implementation in C#.

Structural Patterns.




Decorator.

Stuctural design pattern "Decorator" used to extend or modify the object bahavior "on the fly", during the application lifecycle. In other words, the object responsibility may be changed dynamically.

During the analyze and design stages the system architector always trying to achieve several goals, all of them are the parts of stategy targeted in reduction of the system complexity, classes hierarchy and system coupling. It is always an operational art. Strictly speaking, the Decorator design pattern is the real alternative of subclassing what is why this design pattern needed to be handy.

At our example we will try to implement the decorator structural design pattern functionality together with the prototype design pattern. In that way the prototype design pattern will responsible for the object creation as well as the decorator design pattern will be responsible for object behavior modification.

In turn to achieve these goals we will derieve the "Decorator" class from the "Goods" and "Borrow" class from the "Decorator". The additional method "BorrowGoods()" will be implemented in the Borrow class. As you may found, the Decorator has the protected field of the Goods type, named goods. The Borrow class has the additional field, named borrowers and additional method, named BorrowGoods(). In that way the Decorator pattern extends the state and behavior of the base class.

At the results you may found, that the f1 instance of the Goods, been extended and modifyed through Borrow decorator has the same data with the decorated instance b.

Let's try to implement this in C#.

 

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

Design Patterns. Prototype. C#.



Design Patterns. Decorator. C#.



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-------------------------




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