Creational Design Pattern: Prototype Pattern - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Community

Creational Design Pattern: Prototype Pattern

Share This
The Prototype Pattern suggests the replication of an existing product rather than building a new one, which can also be tailored to specific needs.

If the cost of producing a new object is high and requires a significant amount of resources, it is advisable to adhere to this pattern. The advantage of the Prototype Pattern is its ability to create new objects by cloning existing ones, allowing for efficient object creation and reducing the need for complex initialization processes.

The primary benefits of the prototype pattern are as follows: 
  • It decreases the necessity for sub-classing. 
  • It conceals the intricacies involved in item creation. 
  • The clients can acquire novel entities without prior knowledge of their specific nature. 
  • It allows you to dynamically add or remove objects during program execution.

Application of the Prototype Pattern

  • When the classes are created and initialized during the execution of a program. 
  • When the production cost of an article is high or intricate. 
  • When aiming to minimize the number of classes in an application. 
  • When the client application requires object creation and representation to be concealed

Source code of Shape.java
package com.t4b.test.java.dp.cp.pp; abstract class Shape implements Cloneable { private String id; protected String type; abstract void draw(); public String getType() { return type; } public String getId() { return id; } public void setId(String id) { this.id = id; } public Object clone() { Object clone = null; try { clone = super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return clone; } }
Source code of RectangleShape.java
package com.t4b.test.java.dp.cp.pp; class RectangleShape extends Shape { public RectangleShape() { type = "Rectangle"; } @Override public void draw() { System.out.println("I am in RectangleShape."); } }
Source code of CircleShape.java
package com.t4b.test.java.dp.cp.pp; class CircleShape extends Shape { public CircleShape() { type = "Circle"; } @Override public void draw() { System.out.println("I am in CircleShape."); } }
Source code of SquareShape.java
package com.t4b.test.java.dp.cp.pp; class SquareShape extends Shape { public SquareShape() { type = "Square"; } @Override public void draw() { System.out.println("I am in SquareShape."); } }
Source code of ShapeProtoType.java
package com.t4b.test.java.dp.cp.pp; import java.util.Hashtable; class ShapeProtoType { private static Hashtable<String, Shape> shapeMap = new Hashtable<String, Shape>(); public static Shape getShape(String shapeId) { Shape cachedShape = shapeMap.get(shapeId); return (Shape) cachedShape.clone(); } public static void loadCache() { CircleShape circle = new CircleShape(); circle.setId("1"); shapeMap.put(circle.getId(), circle); SquareShape square = new SquareShape(); square.setId("2"); shapeMap.put(square.getId(), square); RectangleShape rectangle = new RectangleShape(); rectangle.setId("3"); shapeMap.put(rectangle.getId(), rectangle); } }
Source code of TestMain.java
package com.t4b.test.java.dp.cp.pp; public class TestMain { public static void main(String[] args) { ShapeProtoType.loadCache(); Shape clonedShape1 = (Shape) ShapeProtoType.getShape("1"); System.out.println("Shape : " + clonedShape1.getType()); Shape clonedShape2 = (Shape) ShapeProtoType.getShape("2"); System.out.println("Shape : " + clonedShape2.getType()); Shape clonedShape3 = (Shape) ShapeProtoType.getShape("3"); System.out.println("Shape : " + clonedShape3.getType()); } }

Happy Exploring!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.