码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 【设计模式 05】原型模式


    有的时候,我们创建对象,需要耗费大量时间在一些资源型操作上,这个时候,我们就可以先创建出一个模板,然后每次创建的时候直接从模板复制即可,不用反复进行耗时的资源型操作。

    python代码:

    1. import copy
    2. class ComplexObject:
    3. def __init__(self, data):
    4. # 资源型操作
    5. self.data = data
    6. def clone(self):
    7. # 复制
    8. return copy.deepcopy(self)
    9. # 创建原型对象
    10. obj1 = ComplexObject(data = "large data")
    11. # 创建新对象,直接拷贝原对象
    12. new_object = original_object.clone()

     

    JAVA代码:

    1. // 1. 定义抽象原型类
    2. public abstract class Prototype implements Coneable {
    3. public abstract Prototype clone();
    4. }
    5. // 2. 定义具体原型类
    6. public class ConcretePrototype extends Prototype {
    7. private String data;
    8. public ConcretePrototype(String data) {
    9. this.data = data;
    10. }
    11. @Override
    12. public Prototype clone() {
    13. return new ConcretePrototype(this.data);
    14. }
    15. public String getData() {
    16. return data;
    17. }
    18. }
    19. // 3. 客户端代码
    20. public class Client {
    21. public static void main(String[] args) {
    22. // 创建原型对象
    23. Prototype original = new ConcretePrototype("原始对象");
    24. // 克隆原型对象
    25. Prototype clone = original.clone();
    26. // 输出克隆对象的数据
    27. System.out.println("Clone Data: " + ((ConcretePrototype) clone).getData());
    28. }
    29. }

     【设计模式专题之原型模式】5. 矩形原型

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. // 抽象原型类
    6. class Prototype {
    7. public:
    8. virtual Prototype* clone() const = 0;
    9. virtual string getDetails() const = 0;
    10. virtual ~Prototype() {}
    11. };
    12. // 具体矩形原型类
    13. class RectanglePrototype : public Prototype {
    14. private:
    15. string color;
    16. int width;
    17. int height;
    18. public:
    19. // 构造方法
    20. RectanglePrototype(const string& color, int width, int height) : color(color), width(width), height(height) {}
    21. // 克隆方法
    22. Prototype* clone() const override {
    23. return new RectanglePrototype(*this);
    24. }
    25. string getDetails() const override {
    26. return "Color: " + color + ", Width: " + to_string(width) + ", Height: " + to_string(height);
    27. }
    28. };
    29. // 客户端
    30. int main() {
    31. vector rectangles;
    32. // 读取需要创建的矩形数量
    33. int N;
    34. cin >> N;
    35. // 地区每个矩形的属性星系并创建矩形对象
    36. for (int i = 0; i < N; ++i) {
    37. string color;
    38. int width, height;
    39. cin >> color >> width >> height;
    40. // 创建原型对象
    41. Prototype* originalRectangle = new RectanglePrototype(color, width, height);
    42. // 将原型对象保存到向量中
    43. rectangles.push_back(originalRectangle);
    44. }
    45. // 克隆对象
    46. for (const auto& rectangle : rectangles) {
    47. Prototype* cloneRectangle = rectangle->clone();
    48. cout << cloneRectangle->getDetails() << endl;
    49. // 释放克隆对象的内存
    50. delete cloneRectangle;
    51. }
    52. // 释放原型对象的内存
    53. for (const auto& rectangle : rectangles) {
    54. delete rectangle;
    55. }
    56. return 0;
    57. }

     

  • 相关阅读:
    【网安别学成开发】之——python篇
    vue3 + vite 性能优化 ( 从5s -> 0.5s )
    sshd 解决问题 Deprecated SSH Cryptographic Settings 通过修改配置 去掉废弃的加密算法
    【JavaScript】判断对象是否具有某个属性
    Egoroff‘s Theorem
    鸿鹄工程项目管理系统 Spring Cloud+Spring Boot+Mybatis+Vue+ElementUI+前后端分离构建工程项目管理系统项目背景
    造车先做三蹦子220101--机器学习字符(字母、和数字识别)的“小白鼠”与“果蝇”
    【移动Web】移动端适配
    《WEB前端框架开发技术》HTML5响应式旅游景区网站设计与实现——榆林子州HTML+CSS+JavaScript...
    Eth-Trunk负载分担不均怎么办,如何通过Hash算法实现负载分担?
  • 原文地址:https://blog.csdn.net/weixin_55252589/article/details/136546235
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号