Before we proceed to Prototype Design Pattern, We need to review on Clone() method in Java.
The Object cloning is a way to create exact copy of an object. The clone() method of Object class is used to clone an object.
The java.lang.Cloneable interface must be implemented by the class whose object clone we want to create. If we don‘t implement Cloneable interface, clone method
generates CloneNotSupportedException.
The clone() method is defined in the Object Class.
Advantage of Clone() method.
The clone() method saves the extra processing task for creating the exact copy of an object. If we perform by using the new keyword, it will take a lot of
processing time to be performed that is why we use object cloning.
Clone() is the fastest way to copy array.
Disadvantages of Clone()
Disadvantage of Object cloning
Following is a list of some disadvantages of clone() method:
- To use the Object.clone() method, we have to change a lot of syntaxes to our code, like implementing a Cloneable interface, defining the clone() method and handling CloneNotSupportedException, and finally, calling Object.clone() etc.
- We have to implement cloneable interface while it doesn‘t have any methods in it. We just have to use it to tell the JVM that we can perform clone() on our object.
- Object.clone() is protected, so we have to provide our own clone() and indirectly call Object.clone() from it.
- Object.clone() doesn‘t invoke any constructor so we don‘t have any control over object construction.
- If you want to write a clone method in a child class then all of its superclasses should define the clone() method in them or inherit it from another parent class. Otherwise, the super.clone() chain will fail.
- Object.clone() supports only shallow copying but we will need to override it if we need deep cloning.
class Student18 implements Cloneable{ 2.int rollno; 3.String name; 4. 5.Student18(int rollno,String name){ 6.this.rollno=rollno; 7.this.name=name; 8.} 9. 10.public Object clone()throws CloneNotSupportedException{ 11.return super.clone(); 12.} 13. 14.public static void main(String args[]){ 15.try{ 16.Student18 s1=new Student18(101,"amit"); 17. 18.Student18 s2=(Student18)s1.clone(); 19. 20.System.out.println(s1.rollno+" "+s1.name); 21.System.out.println(s2.rollno+" "+s2.name); 22. 23.}catch(CloneNotSupportedException c){} 24. 25.} 26.}
Prototype Design Pattern.
Prototype pattern refers to creating duplicate object while keeping performance in mind.
This type of design pattern comes under creational pattern.
This pattern is used when creation of object directly is costly. For example, an 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 and when needed thus reducing database calls.
Below is an example of Prototype design pattern demo with diagram flow and Code.
Step 1. Create an abstract class implementing Clonable interface.
public 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; } }
Create Concrete Classes extending the above class.
1. Rectangle.java
public class Rectangle extends Shape { public Rectangle(){ type = "Rectangle"; } @Override public void draw() { System.out.println("Inside Rectangle::draw() method."); } }
2. Square.java
public class Square extends Shape { public Square(){ type = "Square"; } @Override public void draw() { System.out.println("Inside Square::draw() method."); } }
3. Circle.java
public class Circle extends Shape { public Circle(){ type = "Circle"; } @Override public void draw() { System.out.println("Inside Circle::draw() method."); } }
Step 3. Create a class to get concrete classes from database and store them in a Hashtable.
import java.util.Hashtable; public 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(); } // 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); } }
Step 4. PrototypePatternDemo uses ShapeCache class to get clones of shapes stored in a Hashtable.
PrototypePatternDemo.java
public class PrototypePatternDemo { public static void main(String[] args) { ShapeCache.loadCache(); Shape clonedShape = (Shape) ShapeCache.getShape("1"); System.out.println("Shape : " + clonedShape.getType()); Shape clonedShape2 = (Shape) ShapeCache.getShape("2"); System.out.println("Shape : " + clonedShape2.getType()); Shape clonedShape3 = (Shape) ShapeCache.getShape("3"); System.out.println("Shape : " + clonedShape3.getType()); } }
原文地址:https://www.cnblogs.com/codingyangmao/p/11263293.html