设计模式学习笔记--原型模式

本文深入探讨了原型模式的概念、应用场景及其实现方式。介绍了如何通过复制已有实例来高效地创建新对象,适用于对象实例化需要在运行时刻指定或状态组合有限的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

五、原型模式

原型模式(Prototype Pattern),用于创建重复对象,并保证性能。通过实现一个原型接口,来复制创建当前对象的备份。

简介

旨在用原型实例指定创建对象的种类,并拷贝原型来创建新的对象。
应用场景:实例化类需要在运行时刻指定,或一个类的实例只有那么几种可能的状态组合中的一种时,可以用该模式。
细胞分裂、object克隆。

  • 优点:性能高、避免构造函数的约束。
  • 缺点:要通盘考虑需被克隆的类,一些引用、接口的设计的。需要实现Cloneable接口,逃避构造函数的约束。

一般用于资源优化、安全与性能考虑、对象实例多次修改等场景。
Java中浅拷贝是实现Cloneable接口,重写clone(),而深拷贝实现的Serializable读取二进制。

实现

创建一个抽象类Shape和扩展的子类,定义一个ShapeCache,该类将shape对象存储在一个Hashtable中,在需要时返回它的克隆。
prototype

  • 创建抽象类
    Shape.java
public abstarct 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(Sting id){
    this.id = id;
  }
  public void setType(String type){
    this.type = type;
  }

  @override
  public Object clone(){
    Object clone = null;
    try{
      clone = super.clone();
    }catch(CloneNotSupportedException e){
      e.printStackTrace();
    }
    return clone;
  }
}
  • 创建实现类
    Rectangle.java
public class Rectangle extends Shape{
  public Rectangle(){
    type = "Rectangle";
  }

  @override
  public void draw(){
    System.out.print("矩形内部的 draw()方法);
  }
}

Square.java

public class Square extends Shape{
  public Square(){
    type = "Square";
  }
  @override
  public void draw(){
    System.out.println("正方形内的 draw()方法");
  }
}

Circle.java

public class Circlr extends Shape{
  public Circle(){
    type = "Circle"
  }
  @override
  public void draw(){
    System.out.println("圆形内的 draw()方法");
  }
}
  • 存储对象
    创建一个类,从数据库获取实体类,并存储到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 cacheShape = shapeMap.get(shapeId);
    return (Shape)cacheShape.clone();
  }
  //对每种形状,都运行数据库查询,并创建该形状
  //shapeMap.put(shapeKey,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 rect = new Rectangle();
    rect.setId("3");
    shapeMap.put(rect.getId(),rect);
  }
}
  • 演示demo
    PrototypePatternDemo.java
public class PrototypePatternDemo{
  public static void main(String[] args){
    //加载对象到数据库(模拟),保存,以备后用
    ShapeCache.loadCache();

    //通过id来获取数据库中保存的实例对象的clone
    Shape clonedShape1 = (Shape)ShapeCache.getShape("1");
    System.out.print("获取的图形是:" + clonedShape1.getType());
    Shape clonedShape2 = (Shape)ShapeCache.getShape("2");
    System.out.print("获取的图形是:" + clonedShape2.getType());
    Shape clonedShape3 = (Shape)ShapeCache.getShape("3");
    System.out.print("获取的图形是:" + clonedShape3.getType());
  }
}
  • 输出结果
获取的图形是:Circle
获取的图形是:Square
获取的图形是:Rectangle
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值