This tutorial explains Gang of Four’s Prototype design pattern in java with UML class diagram. It explains the scenarios in which Prototype design pattern can be applied, and then implements an example use case in Java which shows how the Prototype pattern can be applied to a real world example. It shows the design of the use case using a class diagram, provides the Java code for the use case and lastly explains the working of the code.
Introduction: Prototype Design Pattern is a creational design pattern among the Gang Of Four(GOF) Design PatternsArticle on GOF Patterns & their types. Being a creational design pattern, the Prototype pattern deals with the design of how the object creation is managed.
What is Prototype Design Pattern
In a Prototype Design Pattern implementation the type of object to be created is specified using a prototypical instance. New objects are created by copying(or cloning) this prototype.
In simpler terms, in Prototype Pattern we keep an object of the type we want to create and any new object is created by simply cloning this existing object.
Scenarios in which Prototype Design Pattern can be used
- Exact class to be instantiated is specified dynamically: When the class of which instance is to be created is specified at runtime, then Prototype pattern can be used.
- Object creation using ‘new’ keyword is costlier: When the inherent cost of object creation using ‘new’ keyword is more than simply cloning an existing standard object.
- When Abstract Factory-like class hierarchy is not required: To avoid maintaining an abstract factory like class hierarchy of multiple factories.
- Object States can be pre-defined: If the objects to be created can only have one of a finite number of states, then keeping prototypes of these objects and cloning them when needed is more convenient
Class Diagram for Prototype Design Pattern
Explanation of Prototype Design Pattern’s Class Diagram
Prototypeis the base interface with a single method
- Every concrete class which can be cloned must extend
Prototypeand override the
- Every child of
Prototypeimplements its own cloning logic in the overriden
Clientinvokes the clone method of the concrete child of
Prototypeand gets a cloned object back.
Prototype Design Pattern – example use case implemented in Java – Class Diagram
The Java class diagram above depicts Prototype Design pattern implemented for few elements of Class Diagram of a UMLTool viz. Classes, attributes and association. Lets quickly go through whats there in Java’s example’s class diagram & corresponding code –
UMLToolis the client class.
Prototypeis the base interface which all the three classes we need to clone implement.
UMLAssociationare three classes we will clone.
PrototypeFactoryhelps in cloning as there are multiple children of
Prototypewhich are to be cloned. It returns an instance of the appropriate cloned object based on the
Stringidentifier passed to it.
PrototypeFactorystores a static
HashMapwhich contains one already instantiated object of three concrete classes which are cloneable.
UMLAssociationonce each and then prints the value of their name variable. As you can see in the output that first a cloned instance for each of them is created and then the name attribute’s value is correctly printed, as was stored in their instance stored in static map in
In the above tutorial we understood what is Prototype design pattern and the main scenarios in which it is applicable. We then looked at the UML class diagram for Prototype Design Pattern & its explanation, a Java Use Case implementing Prototype pattern with its class diagram and code for the classes shown in the class diagram, followed by explanation of both the class diagram & code. This concludes the tutorial on Prototype design pattern.