Design Patterns - Class - 5
Design Patterns - Class - 5
– Where to use
• When a system needs to be independent of how its objects are created, composed, and represented.
• When adding and removing objects at runtime.
• When specifying new objects by changing an existing objects structure.
• When configuring an application with classes dynamically.
• When keeping trying to keep the number of classes in a system to a minimum
• When state population is an expensive or exclusive process.
– Benifits
• Speeds up instantiation of large, dynamically loaded classes and Reduced subclassing.
• The process of cloning starts with an initialized and instantiated class. The
Client asks for a new object of that type and sends the request to the
Prototype class. A Concrete Prototype, depending of the type of object is
needed, will handle the cloning through the Clone() method, making a
Object Prototype Pattern Contd…
STRUCTURE
Object Prototype Pattern Contd… (Example)
• an editor for music scores by customizing a general framework for graphical editors and
adding new objects that represent notes, rests, and staves.
• Let's assume the framework provides an abstract Graphic class for graphical
components, like notes and staves. Moreover, it'll provide an abstract Tool class for
defining tools like those in the palette.
• But Graphic tool presents a problem to the framework designer. The classes for notes
and staves are specific to our application, but the Graphic tool class belongs to the
framework. Graphic tool doesn't know how to create instances of our music classes to
add to the score. We could subclass Graphic tool for each kind of music object, but that
would produce lots of subclasses that differ only in the kind of music object they
instantiate.
• The solution lies in making Graphic tool create a new Graphic by copying or "cloning“
an instance of a Graphic subclass. We call this instance a prototype.
• instance will produce a music object by cloning its prototype and adding the clone to
Object Prototype Pattern Contd… (Example)
• Participants
– Prototype (Graphic)
• Declares an interface for cloning itself.
– Concrete Prototype (Staff, WholeNote, HalfNote)
• Implements an operation for cloning itself.
– Client (GraphicTool)
• Creates a new object by asking a prototype to clone itself.
• Collaborations
– A client asks a prototype to clone itself.
Object Prototype Pattern Contd… (Example)
Benefits of prototype pattern
• Adding and removing products at run-time.
– Prototypes let you incorporate a new concrete product class into a system simply by
registering a prototypical instance with the client.
• Specifying new objects by varying values.
– Highly dynamic systems let you define new behaviour through object composition—by
specifying values for an object's variables, for example—and not by defining new
classes.
• Specifying new objects by varying structure.
– Many applications build objects from parts and subparts. Editors for circuit design, for
example, build circuits out of sub circuits
• Reduced subclassing.
– The Prototype pattern lets you clone a prototype instead of asking a factory method to
make a new object. Hence you don't need a Creator class hierarchy at all. This benefit
applies primarily to languages like C++ that don't treat classes as first-class objects.
• Configuring an application with classes dynamically.
– Some run-time environments let you load classes into an application dynamically. The
Prototype pattern is the key to exploiting such facilities in a language like C++.
Issues in implementation of Prototype Pattern
• Using a Prototype Manager
– When the number of prototypes in a system isn't fixed (that is, they can be
created and destroyed dynamically), keep a registry of available prototypes.
Clients won't manage prototypes themselves but will store and retrieve them
from the registry. A client will ask the registry for a prototype before cloning it.
We call this registry a prototype manager.
• Implementing the Clone operation.
– The hardest part of the Prototype pattern is implementing the Clone operation
correctly. It's particularly tricky when object structures contain circular
references. Most languages provide some support for cloning objects. For
example, Smalltalk provides an implementation of copy that's inherited by all
subclasses of Object.
• Initializing clones
– While some clients are perfectly happy with the clone as is, others will want to
initialize some or all of its internal state to values of their choosing. You
generally can't pass these values in the Clone operation, because their number
will vary between classes of prototypes. Some prototypes might need multiple
initialization parameters; others won't need any. Passing parameters in the
Sample Code
• MazePrototypeFactory subclass of the MazeFactory
class
– MazePrototypeFactory will be initialized with prototypes
of the objects it will create so that we don't have to
subclass it just to change the classes of walls or rooms it
creates. MazePrototypeFactory augments the
MazeFactory interface with a constructor that takes the
prototypes as arguments