Java 原型模式

Java 原型模式 首页 / 设计模式入门教程 / Java 原型模式

原型模式(Prototype Pattern)指定创建对象的种类,并通过拷贝这些原型创建新的对象。

原型模式实例

无涯教程将创建一个抽象类 Shape 和一些具体的类来扩展 Shape 类。将定义 ShapeCache 类,将Shape对象存储在 Hashtable 中,并在请求时返回其克隆数据。

原型模式演示,演示类将使用 ShapeCache 类获取 Shape 对象。

Prototype Pattern UML Diagram

第1步  -  创建一个实现 Clonable 接口的抽象类。

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;
   }
}

第2步  -  创建扩展Shop的具体类。

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.");
   }
}

第3步  -  创建一个类以从数据库中获取具体的类,并将其存储在 Hashtable 中。

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(); } // 对于每个shape运行数据库查询并创建shape // shapeMap.put(shapeKey, shape); // 例如,我们要添加三个shape 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); } }

第4步  -  PrototypePatternDemo 使用 ShapeCache 类获取存储在 Hashtable 中的Shape的克隆。

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());		
   }
}

第5步  -  验证输出。

Shape : Circle
Shape : Square
Shape : Rectangle

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

左耳听风 -〔陈皓〕

趣谈网络协议 -〔刘超〕

重学前端 -〔程劭非(winter)〕

后端存储实战课 -〔李玥〕

Service Mesh实战 -〔马若飞〕

张汉东的Rust实战课 -〔张汉东〕

A/B测试从0到1 -〔张博伟〕

代码之丑 -〔郑晔〕

Web漏洞挖掘实战 -〔王昊天〕

好记忆不如烂笔头。留下您的足迹吧 :)