Sie sind auf Seite 1von 3

PROTOTYPE PATTERN This Pattern comes under the classification of Creational Patterns.

rns. The creational patterns deals with the best way to create objects. Prototype pattern implies cloning of an object rather than creation. This helps to copy or clone the existing objects to create new ones rather than creating from the scratch. When to Use o The prototype pattern is used when creating an instance of a class is very time consuming or complex in some way. o Then rather than creating more instances, it is possible to make copies of the original instances and modifying them as appropriate. o When we are not in a position to call a constructor for an object directly, we could alternatively clone a pre-existing object (a prototype) of the same class. o When there are many subclasses that differ only in the kind of objects they create a Prototype Pattern can be used to reduce the number of subclasses by cloning a prototype. o Prototype Design Pattern helps in reducing number of classes. Struncture :

The participants in the prototype pattern are detailed below: Prototype This object defines the interface for creating clones of its self. This typically involves defining a clone function that returns a copy of the original object. ConcretePrototype This object implements the cloning operation defined in the Prototype class. This involves copying the data and state of the original object. Client The client object asks the prototype to clone itself. Example : In C#, this pattern can be implemented easily by using the clone(). Any class, which wants to support cloning, should inherit from the ICloneable interface in C#. ICloneable interface contains a Clone() method which we can override in our class. Clone can be implemented either as a deep copy or a shallow copy.

Deep Copy Vs Shallow Copy o In a deep copy, all objects are duplicated; whereas, in a shallow copy, only the top-level objects are duplicated and the lower levels contain references. o In Shallow Copy any changes made to the clone will be reflected in the original copy and vice versa. But in Deep Copy once the object is cloned the changes would not appear in the clone if the data of the cloned object changes. The resulting clone must be of the same type as or a compatible type to the original instance. Clone() method does a Shallow copy not Deep copy. We can implement Deep copy by using ISerializable interface. One other drawback is that each subclass of Prototype must implement Clone() method.

C# sample code public enum RecordType { Car, Person } /// <summary> /// Record is the Prototype /// </summary> public abstract class Record { public abstract Record Clone(); } /// <summary> /// PersonRecord is the Concrete Prototype /// </summary> public class PersonRecord : Record { string name; int age; public Record Clone() { return (Record)this.MemberwiseClone(); // default shallow copy } } /// <summary> /// CarRecord is another Concrete Prototype /// </summary> public class CarRecord : Record { string carname; Guid id;

public Record Clone() { CarRecord clone = MemberwiseClone(); // default shallow copy clone.id = Guid.NewId(); // always generate new id return clone; } }

/// <summary> /// RecordFactory is the client /// </summary> public class RecordFactory { private static Dictionary<RecordType, Record> _prototypes = new Dictionary<RecordType, Record>(); /// <summary> /// Constructor /// </summary> public RecordFactory() { _prototypes.Add(RecordType.Car, new CarRecord()); _prototypes.Add(RecordType.Person, new PersonRecord()); } /// <summary> /// the Factory Method /// </summary> public Record CreateRecord(RecordType type) { return _prototypes[type].Clone(); } } The benefits of using the Prototype pattern: Adding and removing products at run time Specifying new objects by varying values Specifying new objects by varying structure Reduced subclasses Configuring an application with classes dynamically

Das könnte Ihnen auch gefallen