Prototype Pattern, prototypepattern
(5) Prototype Pattern)
1. Prototype Pattern is used to create duplicate objects and ensure performance. This type of design mode belongs to the creation mode, which provides an optimal way to create objects.
2. This mode implements a prototype interface that is used to create a clone of the current object. This mode is used when the cost of directly creating an object is large. For example, an object needs to be created after a high-cost database operation. We can cache this object, return its clone in the next request, and update the database as needed to reduce database calls.
3.
The prototype mode allows you to create another customizable object from an object without any details.
4.
Implementation
We will create an abstract classShapeAnd extendedShapeClass. The next step is to define the classShapeCacheThis class stores the shape object inHashtable.
PrototypPatternDemo, The usage of our demo classShapeCacheClass to obtainShapeObject.
Step 1
Create an implementationClonableThe abstract class of the interface.
Shape. java
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; }}Step 2
Create an entity class that extends the abstract class above.
Rectangle. java
public class Rectangle extends Shape { public Rectangle(){ type = "Rectangle"; } @Override public void draw() { System.out.println("Inside Rectangle::draw() method."); }}
Square. java
public class Square extends Shape { public Square(){ type = "Square"; } @Override public void draw() { System.out.println("Inside Square::draw() method."); }}
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, obtain the entity classes from the database, and store them inHashtable.
ShapeCache. java
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 ();} // run a database query on each shape and create the shape // shapeMap. put (shapeKey, shape); // For example, we want to add 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
PrototypePatternDemoUseShapeCacheClass to obtainHashtable.
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()); }}Step 5
Verify the output.
Shape : CircleShape : SquareShape : Rectangle
5. Conclusion: Generally, cloning is the best solution if the initialization information does not change. This not only hides the details of object creation, but also greatly improves the performance. (See: http://www.runoob.com/design-pattern/prototype-pattern.html)