原型模式(Prototype Pattern)
1.策略模式(Strategy Pattern)2.桥接模式(Bridge Pattern)3.代理模式(Proxy Pattern)4.中介者模式(Mediator Pattern)5.过滤器模式(Filter Pattern)6.适配器模式(Adapter Pattern)7.责任链模式(Chain Of Responsibility Pattern)8.装饰器模式(Decorator Pattern)9.建造者模式(Builder Pattern)10.观察者模式(Observer Pattern)11.工厂模式(Factory Pattern)12.单例模式(Singleton Pattern)13.组合模式(Composite Pattern)14.模板模式(Template Pattern)
15.原型模式(Prototype Pattern)
16.外观模式(Facade Pattern)17.享元模式(Flyweight Pattern)18.命令模式(Command Pattern)19.解释器模式(Interpreter Pattern)20.迭代器模式(Iterator Pattern)原型模式(Prototype Pattern)
一、意图
用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。
二、优缺点
优点:
1、性能提高。
2、逃避构造函数的约束。
缺点:
1、配备克隆方法需要对类的功能进行通盘考虑,这对于全新的类不是很难,但对于已有的类不一定很容易,特别当一个类引用不支持串行化的间接对象,或者引用含有循环结构的时候。
2、必须实现 Cloneable 接口。
三、具体实现
3.1 模型结构
- Prototype 抽象原型类:抽象原型类是定义具有克隆自己的方法的接口,是所有具体原型类的公共父类,可以是抽象类,也可以是接口。
- ConcretePrototype 具体原型类:具体原型类实现具体的克隆方法,在克隆方法中返回自己的一个克隆对象。
- Client 客户类:客户类让一个原型克隆自身,从而创建一个新的对象。在客户类中只需要直接实例化或通过工厂方法等方式创建一个对象,再通过调用该对象的克隆方法复制得到多个相同的对象。

3.2 实现
1、 定义shape
/**
* @author zhongtao
* @date 2023/5/21 18:35
*/
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;
}
@Override
public Object clone() {
Object clone = null;
try {
clone = super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return clone;
}
}
2、 定义具体的shape
/**
* @author zhongtao
* @date 2023/5/21 18:36
*/
public class Rectangle extends Shape {
public Rectangle() {
type = "Rectangle";
}
@Override
public void draw() {
System.out.println("Inside Rectangle::draw() method.");
}
}
/**
* @author zhongtao
* @date 2023/5/21 18:36
*/
public class Square extends Shape {
public Square() {
type = "Square";
}
@Override
public void draw() {
System.out.println("Inside Square::draw() method.");
}
}
/**
* @author zhongtao
* @date 2023/5/21 18:37
*/
public class Circle extends Shape {
public Circle() {
type = "Circle";
}
@Override
public void draw() {
System.out.println("Inside Circle::draw() method.");
}
}
3、缓存类
import java.util.Hashtable;
/**
* @author zhongtao
* @date 2023/5/21 18:37
*/
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();
}
// 对每种形状都运行数据库查询,并创建该形状
// 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 rectangle = new Rectangle();
rectangle.setId("3");
shapeMap.put(rectangle.getId(), rectangle);
}
}
/**
* @author zhongtao
* @date 2023/5/21 18:38
*/
public class PrototypePatternDemo {
public static void main(String[] args) {
ShapeCache.loadCache();
Shape clonedShape = (Shape) ShapeCache.getShape("1");
System.out.println("Shape : " + clonedShape.getType());
clonedShape.draw();
Shape clonedShape2 = (Shape) ShapeCache.getShape("2");
System.out.println("Shape : " + clonedShape2.getType());
clonedShape2.draw();
Shape clonedShape3 = (Shape) ShapeCache.getShape("3");
System.out.println("Shape : " + clonedShape3.getType());
clonedShape3.draw();
}
}
输出
Shape : Circle
Inside Circle::draw() method.
Shape : Square
Inside Square::draw() method.
Shape : Rectangle
Inside Rectangle::draw() method.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南