23种设计模式——原型模式 (Prototype Pattern)详解
✅作者简介:大家好,我是 Meteors., 向往着更加简洁高效的代码写法与编程方式,持续分享Java技术内容。
🍎个人主页:Meteors.的博客
💞当前专栏:设计模式
✨特色专栏:知识分享
🥭本文内容:23种设计模式——代理模式(Proxy Pattern)
📚 ** ps ** :阅读文章如果有问题或者疑惑,欢迎在评论区提问或指出。
目录
一. 前言
二. 介绍
三. 使用原因
四.具体示例
1. 定义原型接口
2. 具体原型类 - 图形基类
3. 具体原型类 - 圆形
4. 具体原型类 - 矩形
5. 原型管理器
6. 使用
五. 原型模式的类型
六. 优缺点
七. 适用场景
八. 总结
一. 前言
我记得原型模式是的在学校课程中很早学的,不过由于很久没用,我自己也会慢慢的忘了,重新学习之后,我完成了这篇文章......也许现在我提醒你深拷贝、浅拷贝,你会想起某个东西。又或者你是个初入编程世界不久的新人,对于这些还没有进行过学习,不过不要紧,下面内容将为你详解原型模式。
二. 介绍
原型模式是一种创建型设计模式,它允许我们通过复制现有对象来创建新对象,而不是通过实例化类来创建。这种模式的核心思想是使用原型实例指定要创建的对象类型,并通过克隆这些原型来创建新的对象。
生活中的比喻
原型模式就像细胞的有丝分裂过程。在生物学中,新细胞是通过复制现有细胞来创建的,而不是从零开始构建。同样,在软件开发中,我们可以通过复制一个已经配置好的对象来创建新对象,而不是重新初始化所有属性。
三. 使用原因
避免复杂的初始化过程:当对象创建过程很复杂时,克隆比重新创建更简单
提高性能:在某些情况下,克隆比通过构造函数创建对象更高效
动态创建对象:可以在运行时动态添加和删除原型,更加灵活
四.具体示例
用形状的做一个例子:
1. 定义原型接口
interface Prototype {Prototype clone(); }
2. 具体原型类 - 图形基类
abstract class Shape implements Prototype {protected String color;protected int x, y;public Shape(String color, int x, int y) {this.color = color;this.x = x;this.y = y;}// 浅拷贝实现@Overridepublic Prototype clone() {try {return (Shape) super.clone();} catch (CloneNotSupportedException e) {throw new RuntimeException("克隆失败", e);}}public abstract void draw();// getter和setter方法public String getColor() { return color; }public void setColor(String color) { this.color = color; }public int getX() { return x; }public void setX(int x) { this.x = x; }public int getY() { return y; }public void setY(int y) { this.y = y; } }
3. 具体原型类 - 圆形
// 具体原型类 - 圆形 class Circle extends Shape {private int radius;public Circle(String color, int x, int y, int radius) {super(color, x, y);this.radius = radius;}// 深拷贝实现@Overridepublic Prototype clone() {Circle cloned = new Circle(this.color, this.x, this.y, this.radius);return cloned;}@Overridepublic void draw() {System.out.println("绘制一个 " + color + " 的圆形,位置(" + x + "," + y + "),半径:" + radius);}public int getRadius() { return radius; }public void setRadius(int radius) { this.radius = radius; } }
4. 具体原型类 - 矩形
class Rectangle extends Shape {private int width, height;public Rectangle(String color, int x, int y, int width, int height) {super(color, x, y);this.width = width;this.height = height;}// 深拷贝实现@Overridepublic Prototype clone() {Rectangle cloned = new Rectangle(this.color, this.x, this.y, this.width, this.height);return cloned;}@Overridepublic void draw() {System.out.println("绘制一个 " + color + " 的矩形,位置(" + x + "," + y + "),宽:" + width + ",高:" + height);}public int getWidth() { return width; }public void setWidth(int width) { this.width = width; }public int getHeight() { return height; }public void setHeight(int height) { this.height = height; } }
5. 原型管理器
class ShapePrototypeManager {private Map<String, Shape> prototypes = new HashMap<>();public void addPrototype(String key, Shape prototype) {prototypes.put(key, prototype);}public Shape getPrototype(String key) {return (Shape) prototypes.get(key).clone();} }
6. 使用
public class PrototypePatternDemo {public static void main(String[] args) {// 创建原型管理器ShapePrototypeManager manager = new ShapePrototypeManager();// 创建原型对象Circle originalCircle = new Circle("红色", 10, 20, 5);Rectangle originalRectangle = new Rectangle("蓝色", 30, 40, 15, 25);// 注册原型manager.addPrototype("红色圆形", originalCircle);manager.addPrototype("蓝色矩形", originalRectangle);// 克隆对象Circle clonedCircle = (Circle) manager.getPrototype("红色圆形");clonedCircle.setX(100);clonedCircle.setY(200);Rectangle clonedRectangle = (Rectangle) manager.getPrototype("蓝色矩形");clonedRectangle.setColor("绿色");// 绘制原始对象System.out.println("原始对象:");originalCircle.draw();originalRectangle.draw();// 绘制克隆对象System.out.println("\n克隆对象:");clonedCircle.draw();clonedRectangle.draw();} }
五. 原型模式的类型
- 浅拷贝 (Shallow Copy):
- 只复制对象本身,不复制对象内部引用的对象
- 新旧对象共享内部引用对象
- 深拷贝 (Deep Copy):
- 复制对象本身以及对象内部引用的所有对象
- 新旧对象完全独立
六. 优缺点
优点:
- 性能提升:对于创建复杂对象,克隆比实例化更高效
- 简化对象创建:避免复杂的初始化过程
- 动态配置:可以在运行时添加和删除原型
- 减少子类化:通过克隆而不是继承来创建新对象
缺点:
- 克隆复杂性:深拷贝实现可能比较复杂
- 违背开闭原则:需要修改类的实现来支持克隆
- 可能隐藏设计问题:过度使用可能掩盖类设计的问题
七. 适用场景
- 对象创建成本很高:当对象初始化需要大量资源时
- 动态创建对象:需要动态加载类并在运行时创建对象
- 避免复杂的初始化:当对象创建过程复杂且需要多次重复时
- 系统独立于对象创建:希望系统不依赖于具体类的实现
八. 总结
原型模式是一种强大的创建型设计模式,它通过复制现有对象来创建新对象,避免了复杂的初始化过程。在实际开发中,当我们需要创建大量相似对象或对象创建成本较高时,原型模式是一个很好的选择。通过合理使用原型模式,可以提高代码的性能和可维护性。
在Android开发中,原型模式可以用于创建复杂的UI组件、配置对象等场景,特别是在需要大量相似对象的场合下,原型模式能够显著提升性能。
最后,
其它设计模式会陆续更新,希望文章对你有所帮助!