SE450
:
Creational Patterns: Prototype
[5/10]
Name
-
Intent
- Specify the kinds of objects to create using a prototypical instance, and create new objects by copying the prototype
Problem
-
Classes are instantiated or specified at run time by dynamic loading
You want to avoid building a class heirarchy of factories that parallel the class heirarchy of products
When instances of a class can only have a few combinations of state.
Solution
- Create a prototypical instance, and copy it (or clone it) when you need new objects
Participants and Collaborators
Prototype
ConcretePrototype (or just a Prototype)
Client
Consequences
Hides the concrete product class - clients don't need to change as much
Need to be concerned about shallow vs. deep copy
Allows you to return the type you want, instead of
Object
like the
clone
method of Java
Implementation
Use a prototype manager
Implement the "clone" operation
Initialize the "clone"
GoF Reference (117)
Example files are
here