0% found this document useful (0 votes)
48 views8 pages

Prototype Pattern - Creational: Intent

The Prototype pattern is a creational design pattern that allows for creating objects by cloning existing object prototypes rather than creating objects through subclassing. This pattern is useful when the type of objects to create is determined by a prototypical instance, when the classes cannot have subclasses like in an immutable class, or when it is more convenient to create object instances by copying a prototype rather than creating a new instance from scratch. The key participants in this pattern are the prototype interface that declares a cloning method, concrete prototypes that implement this interface by returning a copy of themselves, and client classes that interact with prototypes to create copies.

Uploaded by

ali abbas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
48 views8 pages

Prototype Pattern - Creational: Intent

The Prototype pattern is a creational design pattern that allows for creating objects by cloning existing object prototypes rather than creating objects through subclassing. This pattern is useful when the type of objects to create is determined by a prototypical instance, when the classes cannot have subclasses like in an immutable class, or when it is more convenient to create object instances by copying a prototype rather than creating a new instance from scratch. The key participants in this pattern are the prototype interface that declares a cloning method, concrete prototypes that implement this interface by returning a copy of themselves, and client classes that interact with prototypes to create copies.

Uploaded by

ali abbas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 8

Prototype Pattern | Creational

Intent:

To specify the kinds of objects to create using a prototypical instance, and create
new objects by copying this prototype.
Motivation:

A prototype is a template of any object before the actual object is constructed. In


java also, it holds the same meaning. Prototype design pattern is used in scenarios
where application needs to create a number of instances of a class, which has
almost same state or differs very little.

In this design pattern, an instance of actual object (i.e. prototype) is created on


starting, and thereafter whenever a new instance is required, this prototype is cloned
to have another instance. The main advantage of this pattern is to have minimal
instance creation process which is much costly than cloning process.
Some of the situations where the prototype pattern can be applied are given below

Example 1:
In building stages for a game that uses a maze and different visual objects that the
character encounters it is needed a quick method of generating the haze map using
the same objects: wall, door, passage, room…

The Prototype pattern is useful in this case because instead of hard coding (using
new operation) the room, door, passage and wall objects that get instantiated,
CreateMaze method will be parameterized by various prototypical room, door, wall
and passage objects, so the composition of the map can be easily changed by
replacing the prototypical objects with different ones.
The Client is the CreateMaze method and the ConcretePrototype classes will be the
ones creating copies for different objects.

Example 2:
Suppose we are doing a sales analysis on a set of data from a database. Normally,
we would copy the information from the database, encapsulate it into an object and
do the analysis.

But if another analysis is needed on the same set of data, reading the database
again and creating a new object is not the best idea. If we are using the Prototype
pattern then the object used in the first analysis will be cloned and used for the other
analysis.

The Client methods process the object that encapsulates information from the
database. The ConcretePrototype class will be class that creates the object after
extracting data from the database, will copy it into objects used for analysis.
Applicability:

Use prototype pattern when a system should be independent of how its products are
created, composed and represented and:

• When the class to be instantiated is specified at runtime or


• To avoid building a hierarchy of factory classes or
• It is more convenient to clone an object rather than creating a new object

Structure:

Participants:

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.


ConcretePrototype – implements the operation for cloning itself.

Collaborations:
A client asks a prototype to clone itself.

Consequences:

Benefits of prototype pattern are:

• Adding and removing products at runtime.


• Specifying new objects by varying values.
• Specifying new objects by varying structure.
• Reduced subclassing.
• Configuring an application with classes dynamically.

Main drawback of prototype pattern is that each sub class of Prototype must
implement the clone operation, which may be difficult
Implementation:

?
1 abstract class AbstractProduct implements Cloneable
2 {
3 public static AbstractProduct thePrototype;
4 public static AbstractProduct makeProduct()
5 {
6 try
{
7 return (AbstractProduct) thePrototype.clone();
8 }
9 catch(CloneNotSupportedException e)
10 {
11 return null;
}
12 }
13 }
14
15 class ConcreteProductA extends AbstractProduct { }
16
17 class ConcreteProductB extends AbstractProduct { }
18
19 public class PrototypeDemo
{
20 public static void main(String[] args)
21 {
22 AbstractProduct.thePrototype = new ConcreteProductA();
23 AbstractProduct product = AbstractProduct.makeProduct();
24 System.out.println(product);
}
25 }
26
27
28
Implementation Issues:
Using a prototype manager:

When the application uses a lot of prototypes that can be created and destroyed
dynamically, a registry of available prototypes should be kept. This registry is called
the prototype manager and it should implement operations for managing registered
prototypes like registering a prototype under a certain key, searching for a prototype
with a given key, removing one from the register, etc.
The clients will use the interface of the prototype manager to handle prototypes at
run-time and will ask for permission before using the Clone() method.

There is not much difference between an implementation of a prototype which uses


a prototype manager and a factory method implemented using class registration
mechanism. Maybe the only difference consists in the performance.

Implementing the Clone operation:


A small discussion appears when talking about how deep or shallow a clone should
be: a deep clone clones the instance variables in the cloning object while a shallow
clone shares the instance variables between the clone and the original.

Usually, a shallow clone is enough and very simple, but cloning complex prototypes
should use deep clones so the clone and the original are independent, a deep clone
needing its components to be the clones of the complex object’s components.
Initializing clones:
There are cases when the internal states of a clone should be initialized after it is
created. This happens because these values cannot be passed to the Clone()
method, that uses an interface which would be destroyed if such parameters were
used.

In this case the initialization should be done by using setting and resetting operations
of the prototype class or by using an initializing method that takes as parameters the
values at which the clone’s internal states should be set.

Sample Code:
We’re going to create an abstract class Shape and concrete classes extending
the Shape class. A class ShapeCache is defined as a next step which stores shape
objects in a Hashtable and returns their clone when requested

Create an abstract class implementing Clonable interface.


Shape.java
?
1
2
3 public abstract class Shape implements Cloneable
4 {
private String id;
5
protected String type;
6 abstract void draw();
7 public String getType()
8 {
9 return type;
}
10
public String getId()
11 {
12 return id;
13 }
14 public void setId(String id)
{
15
this.id = id;
16 }
17 public Object clone()
18 {
19 Object clone = null;
try
20
{
21 clone = super.clone();
22 }
23 catch (CloneNotSupportedException e)
24 {
e.printStackTrace();
25
}
26 return clone;
27 }
28 }
29
30
31

Create concrete classes extending the above class.


Rectangle.java

1
2 public class Rectangle extends Shape
3 {
public Rectangle()
4 {
5 type = "Rectangle";
6 }
7 @Override
8 public void draw()
{
9 System.out.println("Inside Rectangle::draw() method.");
10 }
11 }
12

Square.java

1
2 public class Square extends Shape
3 {
public Square()
4 {
5 type = "Square";
6 }
7 @Override
8 public void draw()
{
9 System.out.println("Inside Square::draw() method.");
10 }
11 }
12

Circle.java

1
2 public class Circle extends Shape
3 {
public Circle()
4 {
5 type = "Circle";
6 }
7 @Override
8 public void draw()
{
9 System.out.println("Inside Circle::draw() method.");
10 }
11 }
12
Create a class to get concreate classes from database and store them in
a Hashtable.

ShapeCache.java

?
1
2
3 import java.util.Hashtable;
public class ShapeCache
4 {
5 private static Hashtable<String, Shape> shapeMap = new Hashtable<String, Shape>();
6 public static Shape getShape(String shapeId)
7 {
8 Shape cachedShape = shapeMap.get(shapeId);
return (Shape) cachedShape.clone();
9 }
10 public static void loadCache()
11 {
12 Circle circle = new Circle();
13 circle.setId("1");
shapeMap.put(circle.getId(),circle);
14 Square square = new Square();
15 square.setId("2");
16 shapeMap.put(square.getId(),square);
17 Rectangle rectangle = new Rectangle();
18 rectangle.setId("3");
shapeMap.put(rectangle.getId(),rectangle);
19 }
20 }
21
22

PrototypePatternDemo uses ShapeCache class to get clones of shapes stored in


a Hashtable

PrototypePatternDemo.java
?
1
2 public class PrototypePatternDemo
3 {
public static void main(String[] args)
4
{
5 ShapeCache.loadCache();
6 Shape clonedShape = (Shape) ShapeCache.getShape("1");
7 System.out.println("Shape : " + clonedShape.getType());
8 Shape clonedShape2 = (Shape) ShapeCache.getShape("2");
System.out.println("Shape : " + clonedShape2.getType());
9
Shape clonedShape3 = (Shape) ShapeCache.getShape("3");
10 System.out.println("Shape : " + clonedShape3.getType());
11 }
12 }
13
Known Uses:

The first widely known application of the pattern in an object-oriented language was
in ThingLab, where users could form a composite object and then promote it to a
prototype by installing it in a library of reusable objects. Prototype is used in Etgdb.
Etgdb is a debugger front-end based on ET++ that provides a point-and-click
interface to different line-oriented debuggers. The “interaction technique library” in
Mode Composer stores prototypes of objects that support various interaction
techniques.

Related Patterns:
Prototype and Abstract Factory patterns can be used together. Designs that make
heavy use of the Composite and Decorator patterns often can benefit from Prototype
as well.

Example prototype classes in Java API:

java.lang.Object#clone() (the class has to implement java.lang.Cloneable)


Non-software example:

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.

You might also like