Prototype Design Pattern

Prototype design pattern is creational design pattern. Developers needs to use this design pattern when the object creation is costlier in terms o resources or by some other means. One of the example of costly object creation is that , while creating object we need to make some database calls or may be some webservice calls. In this case we should use prototype design pattern. This pattern is doing nothing special , what it does is it is cashing the new created object and whenever required it will clone this cashed object and will return that copy to the client.



We will be having one abstract class in which we will be implementing the cloneable interface. The reason behind implementing the cloneable interface is that, that every subsequent class which will be extending this base abstract class will be using this base method implementation from abstract class. In above mentioned class we have the abstract class named as Shape so we will implement clone method in that class.

One more things which needs to be done is, we need to create each type of object and cache it in some kind of store like HashMap or HashTable and whenever required we can return the required type of object by cloning the cached copy. Let's see some practical example now.


Abstract Shape Class

public abstract class Shape implements Cloneable {

	public  Object clone() {
		Object clone = null;
		try {
			clone = super.clone();
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		return clone;
	}

	abstract String displayType();

}

Circle Class

package com.design.pattern.prototype;

public class Circle extends Shape {

	private String type;

	public Circle() {
		this.type = "Circle";
	}
	
	public String displayType() {
		return "This is "+type+" of object";
	}
}

Rectangle Class

package com.design.pattern.prototype;

public class Rectangle extends Shape {
	private String type;

 	public Rectangle() {
		this.type = "rectangle";
	}
	
	public String displayType() {
		return "This is "+type+" of object";
	}
}

Triangle Class

package com.design.pattern.prototype;

public class Triangle extends Shape{
	 	private String type;
	 	
	 	public Triangle() {
			this.type = "Triangle";
		}
	 	
		public String displayType() {
			return "This is "+type+" of object";
		}
}

ShapeStore Class

package com.design.pattern.prototype;

import java.util.HashMap;
import java.util.Map;

public class ShapeStore {
	
	public static Map cachedObjects=new HashMap<>();
	
	public static void loadObjects() {
		cachedObjects.put("circle", new Circle());
		cachedObjects.put("triangle", new Triangle());
		cachedObjects.put("rectangle", new Rectangle());
	}
	
	public static Shape getShape(String shape) {
		return (Shape) cachedObjects.get(shape).clone();
	}
	
}

PrototypeDemo Class

package com.design.pattern.prototype;

public class PrototypeDemo {

	public static void main(String[] args) {

		ShapeStore.loadObjects();
		Shape circle=ShapeStore.getShape("circle");
		Shape triangle=ShapeStore.getShape("triangle");
		Shape rectangle=ShapeStore.getShape("rectangle");
		
		System.out.println(circle.displayType());
		System.out.println(triangle.displayType());
		System.out.println(rectangle.displayType());
	}
}



Share this:

CONVERSATION

0 comments:

Post a Comment