设计模式-原型模式(克隆、Clone、Prototype)

ops/2024/10/18 5:43:23/

原型模式(克隆、Clone、Prototype)是一种创建型设计模式, 使你能够复制已有对象, 而又无需使代码依赖它们所属的类。

 问题

    譬如美国研制了一种特效药,而且还在专利保护器内,而印度制药公司看中了,并且开始模仿,如果他不研究药里面的成分,只是照着药片的样子颜色复制出来肯定不可以,而且他还需要研究药里面每一份的组成和计量比率。这样的你就把药里面的每一个元素一个一个的分析和验证了一遍。就像我们开发中你必须新建一个属于相同类的对象。 然后, 你必须遍历原始对象的所有成员变量, 并将成员变量值复制到新对象中。

原型模式结构

上代码:

public abstract class Shape {public int x;public int y;public String color;public Shape() {}public Shape(Shape target) {if (target != null) {this.x = target.x;this.y = target.y;this.color = target.color;}}public abstract Shape clone();@Overridepublic boolean equals(Object object2) {if (!(object2 instanceof Shape)) return false;Shape shape2 = (Shape) object2;return shape2.x == x && shape2.y == y && Objects.equals(shape2.color, color);}
}
public class Circle extends Shape {public int radius;public Circle() {}public Circle(Circle target) {super(target);if (target != null) {this.radius = target.radius;}}@Overridepublic Shape clone() {return new Circle(this);}@Overridepublic boolean equals(Object object2) {if (!(object2 instanceof Circle) || !super.equals(object2)) return false;Circle shape2 = (Circle) object2;return shape2.radius == radius;}
}
public class Rectangle extends Shape {public int width;public int height;public Rectangle() {}public Rectangle(Rectangle target) {super(target);if (target != null) {this.width = target.width;this.height = target.height;}}@Overridepublic Shape clone() {return new Rectangle(this);}@Overridepublic boolean equals(Object object2) {if (!(object2 instanceof Rectangle) || !super.equals(object2)) return false;Rectangle shape2 = (Rectangle) object2;return shape2.width == width && shape2.height == height;}
}

测试 

public class Demo {public static void main(String[] args) {List<Shape> shapes = new ArrayList<>();List<Shape> shapesCopy = new ArrayList<>();Circle circle = new Circle();circle.x = 10;circle.y = 20;circle.radius = 15;circle.color = "red";shapes.add(circle);Circle anotherCircle = (Circle) circle.clone();shapes.add(anotherCircle);Rectangle rectangle = new Rectangle();rectangle.width = 10;rectangle.height = 20;rectangle.color = "blue";shapes.add(rectangle);cloneAndCompare(shapes, shapesCopy);}private static void cloneAndCompare(List<Shape> shapes, List<Shape> shapesCopy) {for (Shape shape : shapes) {shapesCopy.add(shape.clone());}for (int i = 0; i < shapes.size(); i++) {if (shapes.get(i) != shapesCopy.get(i)) {System.out.println(i + ": Shapes are different objects (yay!)");if (shapes.get(i).equals(shapesCopy.get(i))) {System.out.println(i + ": And they are identical (yay!)");} else {System.out.println(i + ": But they are not identical (booo!)");}} else {System.out.println(i + ": Shape objects are the same (booo!)");}}}
}
0: Shapes are different objects (yay!)
0: And they are identical (yay!)
1: Shapes are different objects (yay!)
1: And they are identical (yay!)
2: Shapes are different objects (yay!)
2: And they are identical (yay!)

原型模式优缺点 

  1. 你可以克隆对象, 而无需与它们所属的具体类相耦合。
  2. 你可以更方便地生成复杂对象。
  3. 你可以用继承以外的方式来处理复杂对象的不同配置。

http://www.ppmy.cn/ops/126400.html

相关文章

C语言哈希表

哈希表&#xff08;Hash Table&#xff09;是一种高效的数据结构&#xff0c;用于实现快速的数据查找、插入和删除操作。哈希表通过将关键字&#xff08;Key&#xff09;映射到表中的位置&#xff08;索引&#xff09;&#xff0c;实现近似常数时间的操作效率。哈希表在许多应用…

Oracle用户以及初学的经验

背景&#xff1a;目前略&#xff0c;后续补上 //创建用户 CREATE USER tpmeaccount IDENTIFIED BY 12345678; //授权权限 GRANT CONNECT, RESOURCE TO tpmeaccount;//登录&#xff0c; 以sysdba这个角色登录 &#xff0c; sys是用户名 /后面是密码 sqlplus sys/123456 as sysd…

【进阶OpenCV】 (18)-- Dlib库 --人脸关键点定位

文章目录 人脸关键点定位一、作用二、原理三、代码实现1. 构造人脸检测器2. 载入模型&#xff08;加载预测器&#xff09;3. 获取关键点4. 显示图像5. 完整代码 总结 人脸关键点定位 在dlib库中&#xff0c;有shape_predictor_68_face_landmarks.dat预测器&#xff0c;这是一个…

重构长方法之以方法对象取代方法

以方法对象取代方法 是重构长方法的一种技术&#xff0c;适用于那些过长、逻辑复杂且难以拆解的单一方法。此方法通过引入一个新的类&#xff0c;将原本庞杂的方法转化为一个对象方法&#xff0c;这样可以更容易将方法中的不同步骤拆解为多个私有方法&#xff0c;使代码结构清晰…

python实现屏幕录制,录音录制工具

python实现屏幕录制&#xff0c;录音录制工具 一&#xff0c;介绍 Python 实现的屏幕录制和录音录制工具是一个便捷的应用程序&#xff0c;旨在帮助用户同时捕捉计算机屏幕上的活动以及与之相关的音频输出。这个工具尤其针对教育工作者、内容创作者、技术支持人员以及任何需要…

数据库的相关概念

先看与数据库有关的几个名词&#xff1a; DB&#xff1a;database&#xff0c;数据库&#xff0c;里边保存了有组织的规范的数据。 DBMS&#xff1a;database management system &#xff0c; 数据库管理系统&#xff0c;简称数据库软件&#xff0c;数据库产品&#xff0c;数…

Python操作MySQL数据库:基础教程与示例代码

&#x1f49d;&#x1f49d;&#x1f49d;欢迎莅临我的博客&#xff0c;很高兴能够在这里和您见面&#xff01;希望您在这里可以感受到一份轻松愉快的氛围&#xff0c;不仅可以获得有趣的内容和知识&#xff0c;也可以畅所欲言、分享您的想法和见解。 推荐&#xff1a;「storm…

【数据分享】全国资源和环境-环境污染治理投资(1998-2021年)

数据介绍 一级标题指标名称单位指标解释资源和环境环境污染治理投资总额亿元环境污染治理投资指在污染源治理和城市环境基础设施建设的资金投入中&#xff0c;用于形成固定资产的资金&#xff0c;其中污染源治理投资包括工业污染源治理投资和“三同时”项目环保投资两部分。环…