Prototype pattern
A prototype pattern is a creational design pattern used in software development when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects. This pattern is used for example when the inherent cost of creating a new object in the standard way (e.g., using the 'new' keyword) is prohibitively expensive for a given application.
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 } /// <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 override 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 override Record Clone() { CarRecord clone = (CarRecord)this.MemberwiseClone(); // default shallow copy clone.id = Guid.NewGuid(); // 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(); } }
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. [Michael Duell, "Non-software examples of software design patterns", Object Magazine, Jul 97, p54]
Rules of thumb
Sometimes creational patterns 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 (GoF, p126). Abstract Factory, Builder, and Prototype can use Singleton in their implementations. (GoF, p81, 134). Abstract Factory classes are often implemented with Factory Methods, but they can be implemented using Prototype. (GoF, 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. (GoF, p136)
Prototype doesn't require subclassing, but it does require an "initialize" operation. Factory Method requires subclassing, but doesn't require Initialize. (GoF, p116)
Designs that make heavy use of the Composite and Decorator patterns often can benefit from Prototype as well. (GoF, p126)
Cloning in PHP
In PHP 5, unlike previous versions, objects are by default passed by reference. In order to pass by value, use the "magic function" __clone(). This makes using the prototype pattern very easy to implement. See object cloning for more information.
References
- Gamma, Erich; Helm, Richard; Johnson, Ralph; Vlissides, John (1994). Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley. ISBN 0-201-63361-2.
{{cite book}}
: CS1 maint: multiple names: authors list (link)