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. The Introduction article.

Do you remember the previous article about "Factory Method" pattern? Ok. What do you think, how many types of goods the storehouse has? I think about hundreds and thousands, not less. In that case we have a problem: it's too hard ( rather say - impossible ) to keep the object factory according to wide range of products. And ones more. There is no way to instantiate new object types "on the fly" without modifying sources using the "Factory Method" pattern. It's time to say: the Prototype design pattern will help us in this situation.

Implementation is simple, but this pattern has some nuances, what is why the article will be divided into three parts.

The core idea of the pattern is the cloning of the objects. This way, first of all, we will have the abstract prototype class GoodsProto which will have one public abstract method Clone which must be overridden at the concrete prototype class Goods which derived from GoodsProto. Ok, this is a half of the way. Again, each concrete class should implement the Clone method functionality in turn to return the copy of the object instance.

In our example we are using the return (GoodsProto) this.MemberwiseClone(); construction which return the "shallow copy" of the object. This is the reference to MSDN : " A shallow copy creates a new instance of the same type as the original object, and then copies the nonstatic fields of the original object. If the field is a value type, a bit-by-bit copy of the field is performed. If the field is a reference type, the reference is copied but the referred object is not; therefore, the reference in the original object and the reference in the clone point to the same object. In contrast, a deep copy of an object duplicates everything directly or indirectly referenced by the fields in the object. "

We will implement the deep copy cloning in our next part of the article. Finally, lets pay attention to the StoreHouse class which is the ObjectManager in our example. Using this construction we may implement many useful additional functionality. For example, serialization / deserialization of the objects may be implemented at this point. Of course, the ObjectManager may have advanced API for the object management: read, write, add, delete, search etc.

Let's try to implement this in C#.

   
  Prototype Pattern, C# implementation:

//Prototype design pattern implementation in C#

//------------------------------------
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 int m_group;
private int m_weight;
private double m_prise;
private int m_qty;

// Constructor
public Goods( string title, int group, int weight, double prise, int quantity)
{
  m_title = title;
  m_group = group;
  m_weight = weight;
  m_prise = prise;
  m_qty = quantity;
}

// Methods
public override GoodsProto Clone()
{
// Creates a 'shallow copy'
  return (GoodsProto) this.MemberwiseClone();
}

public void PrintInfo()
{
  Console.WriteLine( "Goods title: {0}, group: {1}, weight: {2}, prise: {3}, quantity: {4}. Amount for {0}: {5}",
m_title, m_group, m_weight, m_prise, m_qty, m_prise*m_qty );
}
}

//--------------------------------------
// Object 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 ); }
}
}


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

// Initialize goods
  storehouse[ "flatiron" ] = new Goods( "flatiron", 1, 10, 20.1, 10 );
  storehouse[ "mobile nokia 12345" ] = new Goods( "mobile nokia 12345", 2, 3, 7.2, 15 );

string goodsTitle = "flatiron";
Goods f1 = (Goods)storehouse[ goodsTitle ].Clone();
f1.PrintInfo();

Console.Read();
}
}
}

Design Patterns. Prototype. C#.



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