橘子味的心
标题:Java原型模式

原型模式指在创建重复对象的同时保持性能。 这种类型的设计模式属于创建模式,因为此模式提供了创建对象的最佳方法之一。

这个模式涉及实现一个原型接口,它只创建当前对象的克隆。有时直接创建对象时使用这种模式是昂贵的。例如,在昂贵的数据库操作之后创建对象。因此我们可以缓存对象,在下一个请求时返回其克隆,并在需要时更新数据库,从而减少数据库调用。

实现实例

我们将创建一个抽象类Shape和扩展Shape类的具体类。 在下一步中定义ShapeCache类,在Hashtable中存储形状(Shape)对象,并在请求时返回其克隆。

PrototypPatternDemo这是一个演示类,将使用ShapeCache类来获取一个Shape对象。实现结构图如下所示 -

第1步

创建一个实现Clonable接口的抽象类。

Shape.java

  1. public abstract class Shape implements Cloneable {
  2.  
  3. private String id;
  4. protected String type;
  5.  
  6. abstract void draw();
  7.  
  8. public String getType(){
  9. return type;
  10. }
  11.  
  12. public String getId() {
  13. return id;
  14. }
  15.  
  16. public void setId(String id) {
  17. this.id = id;
  18. }
  19.  
  20. public Object clone() {
  21. Object clone = null;
  22.  
  23. try {
  24. clone = super.clone();
  25.  
  26. } catch (CloneNotSupportedException e) {
  27. e.printStackTrace();
  28. }
  29.  
  30. return clone;
  31. }
  32. }
  33. Java

第2步

创建扩展上述类的具体类。

Rectangle.java

  1. public class Rectangle extends Shape {
  2.  
  3. public Rectangle(){
  4. type = "Rectangle";
  5. }
  6.  
  7. @Override
  8. public void draw() {
  9. System.out.println("Inside Rectangle::draw() method.");
  10. }
  11. }
  12. Java

Square.java

  1. public class Square extends Shape {
  2.  
  3. public Square(){
  4. type = "Square";
  5. }
  6.  
  7. @Override
  8. public void draw() {
  9. System.out.println("Inside Square::draw() method.");
  10. }
  11. }
  12. Java

Circle.java

  1. public class Circle extends Shape {
  2.  
  3. public Circle(){
  4. type = "Circle";
  5. }
  6.  
  7. @Override
  8. public void draw() {
  9. System.out.println("Inside Circle::draw() method.");
  10. }
  11. }
  12. Java

第3步

创建一个类来获取具体的类,并将它们存储在Hashtable中。

ShapeCache.java

  1. import java.util.Hashtable;
  2.  
  3. public class ShapeCache {
  4.  
  5. private static Hashtable<String, Shape> shapeMap = new Hashtable<String, Shape>();
  6.  
  7. public static Shape getShape(String shapeId) {
  8. Shape cachedShape = shapeMap.get(shapeId);
  9. return (Shape) cachedShape.clone();
  10. }
  11.  
  12. // for each shape run database query and create shape
  13. // shapeMap.put(shapeKey, shape);
  14. // for example, we are adding three shapes
  15.  
  16. public static void loadCache() {
  17. Circle circle = new Circle();
  18. circle.setId("1");
  19. shapeMap.put(circle.getId(),circle);
  20.  
  21. Square square = new Square();
  22. square.setId("2");
  23. shapeMap.put(square.getId(),square);
  24.  
  25. Rectangle rectangle = new Rectangle();
  26. rectangle.setId("3");
  27. shapeMap.put(rectangle.getId(), rectangle);
  28. }
  29. }
  30. Java

第4步

PrototypePatternDemo使用ShapeCache类来获取存储在Hashtable中的形状(shape)的克隆。

PrototypePatternDemo.java

  1. public class PrototypePatternDemo {
  2. public static void main(String[] args) {
  3. ShapeCache.loadCache();
  4.  
  5. Shape clonedShape = (Shape) ShapeCache.getShape("1");
  6. System.out.println("Shape : " + clonedShape.getType());
  7.  
  8. Shape clonedShape2 = (Shape) ShapeCache.getShape("2");
  9. System.out.println("Shape : " + clonedShape2.getType());
  10.  
  11. Shape clonedShape3 = (Shape) ShapeCache.getShape("3");
  12. System.out.println("Shape : " + clonedShape3.getType());
  13. }
  14. }
  15. Java

第5步

验证输出,执行上面的代码得到以下结果 -

  1. Shape : Circle
  2. Shape : Square
  3. Shape : Rectangle
  4. Java