2. Overview
Prototype is a creational pattern.
This pattern encapsulate the knowledge
about which classes a system users, but
they hide the details of how the instances
of these classes are created and put
together.
2
3. Intent
Specify the kind of objects to create using
a prototypical instance, and create new objects
by copying this prototype.
3
4. Applicability
4
• When the classes to instance are
specified at run-time, for example, by
dynamic loading
• To avoid building a class hierarchy of
factories that parallels the class
hierarchy of products
• When instance of a class can have one
of only a few different combinations of
state.
6. Participant
• Prototype
–Declares an interface for cloning itself
• Concrete Prototype
–Implements an operation for cloning itself
• Client
–Creates a new object by asking a prototype
to clone itself
6
7. Consequences
• Adding and removing products at run-time
• Specifying new objects by varying values
• Specifying new objects by varying structure
• Reduced subclassing
• Configuring an application with classes
dynamically
7
8. Copy options
• Deep Copy (implementation in user
code), but in some languages exist
functions which do it
• Simple object clone (standard
function MemberwiseClone())
8
11. Related Patterns
Prototype and Abstract Factory are
competing patterns. They can also be used
together, however. An Abstract Factory might
store a set prototypes from which to clone and
return product objects.
Designs that make heavy use of the
Composite and Decorator patterns often can
benefit from Prototype as well.
11