Prototype Pattern Article Index for
Prototype
Website Links For
Prototype
 

Information About

Prototype Pattern




To implement the pattern, declare an abstract base class that specifies a Pure Virtual ''clone()'' method. Any class that needs a " Polymorphic Constructor " capability derives itself from the abstract Base Class , and implements the ''clone()'' operation.

The client, instead of writing code that invokes the "new" operator on a hard-wired class name, calls the ''clone()'' method on the prototype, calls a Factory Method with a parameter designating the particular concrete derived class desired, or invokes the ''clone()'' method through some mechanism provided by another design pattern.


STRUCTURE

The Class Diagram is as shown below:


C# SAMPLE CODE


public enum RecordType
{
Car,
Person
}

  • ---

  • Record is the Prototype

  • /

  • public abstract class Record {

public abstract Record Clone();
}

  • ---

  • PersonRecord is the Concrete Prototype

  • /

  • public class PersonRecord : Record {

string name;
int age;

public Record Clone() {
return (Record)MemberwiseClone(); // default shallow copy
}
}

  • ---

  • CarRecord is another Concrete Prototype

  • /

  • 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;
}
}

  • ---

  • RecordFactory is the client

  • /

  • public class RecordFactory {

private static Dictionary _prototypes = new Dictionary();

// Constructor
public RecordFactory()
{
_prototypes.Add(RecordType.Car, new CarRecord());
_prototypes.Add(RecordType.Person, new PersonRecord());
}

// the Factory Method
public Record CreateRecord(RecordType type) {
return _prototypes {Link without Title} .Clone();
}
}


EXAMPLES


The Prototype pattern specifies the kind of objects to create using a prototypical instance. Prototypes of new products are often built prior to full production, but in this example, the prototype is passive and does not participate in copying itself. The mitotic division of a cell - resulting in two identical cells - is an example of a prototype that plays an active role in copying itself and thus, demonstrates the Prototype pattern. When a cell splits, two cells of identical genotype result. In other words, the cell clones itself. Duell, "Non-software examples of software design patterns", Object Magazine, Jul 97, p54


RULES OF THUMB


Sometimes Creational Pattern s overlap - there are cases when either Prototype or Abstract Factory would be appropriate. At other times they complement each other: Abstract Factory might store a set of Prototypes from which to clone and return product objects p126 . Abstract Factory, Builder, and Prototype can use Singleton in their implementations. p81, 134 .
Abstract Factory classes are often implemented with Factory Methods, but they can be implemented using Prototype. p95

Factory Method: creation through Inheritance .

Prototype: creation through Delegation .

Often, designs start out using Factory Method (less complicated, more customizable, subclasses proliferate) and evolve toward Abstract Factory, Prototype, or Builder (more flexible, more complex) as the designer discovers where more flexibility is needed. p136

Prototype doesn't require subclassing, but it does require an "initialize" operation. Factory Method requires subclassing, but doesn't require Initialize. p116

Designs that make heavy use of the Composite and Decorator patterns often can benefit from Prototype as well. p126