You are on page 1of 13

Design Patterns

focused on object creational pattern


Design patterns
Design Patterns

Creational Design Structural Design Behavioural Design


Patterns Patterns Patterns
An overview
• Design Patterns
– Design pattern is a general reusable solution to commonly occurring
problem in software design. A design pattern can be transformed
directly into code.
– There are three types of design patterns
• Creational design patterns
– This pattern can be divided into class-creation patterns and object-creational patterns.
While class-creation patterns use inheritance effectively in the instantiation process,
object-creation patterns use delegation effectively to get the job done.
• Structural design patterns
– This patterns are all about Class and Object composition. Structural class-creation
patterns use inheritance to compose interfaces. Structural object-patterns define
ways to compose objects to obtain new functionality.
• Behavioural Design Patterns
– This patterns are all about Class's objects communication. Behavioural patterns are
those patterns that are most specifically concerned with communication between
objects.
Creational Design Patterns
Prototype Pattern
• Definition
– The Prototype pattern is basically the creation of new instances through cloning existing instances.
By creating a prototype, new objects are created by copying this prototype.

– 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.

– Pros and Cons


• Each subclass of Prototype must implement the Clone operation. Could be difficult with existing classes with
internal objects with circular references or which does not support copying
Object Prototype Pattern
• Present days programming is all about optimising costs associated
with it. Optimisation of costs is a big issue when it comes to using
computer resources. Programmers are putting their best efforts to
find the ways of improving the performance.

• When it comes to object creation we can find a better way to have


new objects: cloning. To this idea one particular design pattern is
related: rather than creation it uses cloning. If the cost of creating a
new object is large and creation is resource intensive, we clone the
object.

• The Prototype design pattern is the one in question. It allows an object


to create customized objects without knowing their class or any details
Object Prototype Pattern Contd…
• Intent
– specifying the kind of objects to create using a prototypical instance
– creating new objects by copying this prototype
• Implementation
– This pattern uses abstract classes, as we will see below and only three types of
classes making its implementation rather easy.

• The classes participating to the Prototype Pattern are:


– Client - creates a new object by asking a prototype to clone itself.
– Prototype - declares an interface for cloning itself.
– Concrete Prototype - implements the operation for cloning itself.

• 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

Code is available at FORUM

You might also like