package com.interview.design.pattern.creational; import java.util.Hashtable; /** * Created_By: stefanie * Date: 14-12-2 * Time: 下午10:55 * * This pattern involves implementing a prototype interface which tells to create a clone of the current object. * This pattern is used when creation of object directly is costly. * For example, a object is to be created after a costly database operation. We can cache the object, * returns its clone on next request and update the database as as and when needed thus reducing database calls. */ public class PrototypePattern { /** * A prototype class Shape which implements Cloneable */ static 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; } } /** * Three sub-class of Shape */ static class Rectangle extends Shape { public Rectangle(){ type = "Rectangle"; } @Override public void draw() { System.out.println("Inside Rectangle::draw() method."); } } static class Square extends Shape { public Square(){ type = "Square"; } @Override public void draw() { System.out.println("Inside Square::draw() method."); } } static class Circle extends Shape { public Circle(){ type = "Circle"; } @Override public void draw() { System.out.println("Inside Circle::draw() method."); } } /** * The abstract factory to create Shape using clone */ static class ShapeCache { 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(); } // assume for each shape run database query and create shape // shapeMap.put(shapeKey, shape); // for example, we are adding three shapes public static void loadCache() { Circle circle = new Circle(); circle.setId("1"); shapeMap.put(circle.getId(),circle); Square square = new Square(); square.setId("2"); shapeMap.put(square.getId(),square); Rectangle rectangle = new Rectangle(); rectangle.setId("3"); shapeMap.put(rectangle.getId(),rectangle); } } public static void main(String[] args) { ShapeCache.loadCache(); Shape clonedShape = ShapeCache.getShape("1"); System.out.println("Shape : " + clonedShape.getType()); Shape clonedShape2 = ShapeCache.getShape("2"); System.out.println("Shape : " + clonedShape2.getType()); Shape clonedShape3 = ShapeCache.getShape("3"); System.out.println("Shape : " + clonedShape3.getType()); } }