Java 原型模式

Java设计模式 - 原型模式

原型模式是创建模式之一。

原型模式有助于创建具有更好性能的重复对象。

在原型模式中,将返回一个现有对象的克隆,而不是创建新的对象。

我们使用原型设计模式,如果创建一个新对象的成本是昂贵和资源密集型。

例子

以下代码显示如何使用Prototype Pattern创建对象。

首先它创建一个Shape抽象类,实现Cloneable接口。

复制代码
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;
   }
}

然后它创建三个具体的类扩展Shape类。

复制代码
class Rectangle extends Shape {

   public Rectangle(){
     type = "Rectangle";
   }

   @Override
   public void draw() {
      System.out.println("Inside Rectangle::draw() method.");
   }
}
class Square extends Shape {

   public Square(){
     type = "Square";
   }

   @Override
   public void draw() {
      System.out.println("Inside Square::draw() method.");
   }
}
class Circle extends Shape {

   public Circle(){
     type = "Circle";
   }

   @Override
   public void draw() {
      System.out.println("Inside Circle::draw() method.");
   }
}

之后,它创建一个ShapeProtoType类来返回形状的原型。

复制代码
class ShapeProtoType{
   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();
   }
   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 class Main{
   public static void main(String[] args) {
      ShapeProtoType.loadCache();

      Shape clonedShape = (Shape) ShapeProtoType.getShape("1");
      System.out.println("Shape : " + clonedShape.getType());    

      Shape clonedShape2 = (Shape) ShapeProtoType.getShape("2");
      System.out.println("Shape : " + clonedShape2.getType());    

      Shape clonedShape3 = (Shape) ShapeProtoType.getShape("3");
      System.out.println("Shape : " + clonedShape3.getType());    
   }
}

上面的代码生成以下结果。

相关推荐
lsx20240629 分钟前
HTML 音频(Audio)详解
开发语言
woshihonghonga32 分钟前
【动手学深度学习】
开发语言·python
郝开38 分钟前
Spring Boot 2.7.18(最终 2.x 系列版本)1 - 技术选型:连接池技术选型对比;接口文档技术选型对比
java·spring boot·spring
威风的虫43 分钟前
ES6 数组方法:告别循环,拥抱函数式编程
开发语言·前端·javascript
码界筑梦坊1 小时前
240-基于Python的医疗疾病数据可视化分析系统
开发语言·python·信息可视化·数据分析·毕业设计·echarts
2301_803554521 小时前
C++ 锁类型大全详解
开发语言·c++
wuwu_q1 小时前
用通俗易懂方式,详细讲讲 Kotlin Flow 中的 map 操作符
android·开发语言·kotlin
曼巴UE51 小时前
UE5 C++ Slate 画曲线
开发语言·c++·ue5
小猪咪piggy1 小时前
【项目】小型支付商城 MVC/DDD
java·jvm·数据库
知兀1 小时前
【Spring/SpringBoot】SSM(Spring+Spring MVC+Mybatis)方案、各部分职责、与Springboot关系
java·spring boot·spring