当前位置: 首页 > article >正文

设计模式 | 原型模式

定义

原型设计模式(Prototype Pattern)是一种创建型设计模式,它通过复制已有对象来创建新对象,而不是通过构造函数。这种模式适用于需要大量相似对象的情况,避免了重复的初始化工作。

原型模式的核心思想是通过一个原型实例来生成新的对象。通常,一个类会实现一个接口,该接口定义了一个克隆方法,用于复制其自身。

应用场景

1. 对象创建成本较高:当创建对象的代价很大时,可以通过复制现有对象来减少开销。
2. 需要动态生成对象:在运行时动态生成多个相似对象,例如游戏中的角色、复杂模型等。
3. 替代构造函数:在无法确定创建对象的类时,可以使用原型模式进行对象创建。
4. 减少类的数量:通过复制对象,可以减少需要定义的类的数量,方便管理。

实例分享

#include <iostream>
#include <thread>
#include <mutex>
#include <chrono>

class Prototype {
  public:
    Prototype() = default;
    Prototype(const std::string& name) : name_(name) {}
    virtual ~Prototype() = default;

    virtual std::unique_ptr<Prototype> clone() const = 0;
    virtual void show() const = 0;

  protected:
    std::string getName() const {
      return name_;
    }
  
  private:
    std::string name_;
};

class ConcretePrototype1 : public Prototype {
  public:
    ConcretePrototype1(const std::string& name, int value1)
       : value1_(value1), Prototype(name) {}

    std::unique_ptr<Prototype> clone() const override {
      return std::make_unique<ConcretePrototype1>(*this);
    }

    void show() const override {
      std::cout << "ConcretePtototype obj:" << this << " name:" << getName()
                << "  value:" << value1_ << std::endl;
    }
  
  private:
    int value1_;
};

class ConcretePrototype2 : public Prototype {
  public:
    ConcretePrototype2(const std::string& name, int value2)
       : value2_(value2), Prototype(name) {}

    std::unique_ptr<Prototype> clone() const override {
      return std::make_unique<ConcretePrototype2>(*this);
    }

    void show() const override {
      std::cout << "ConcretePtototype obj:" << this << " name:" << getName()
                << "  value:" << value2_ << std::endl;
    }

  private:
    int value2_;
};


int main() {
  // Create a prototype object, and clone the original prototype object. Then they have the
  // same object address(only clone).
  auto ori_concrete_obj1 = std::make_unique<ConcretePrototype1>("ConcreteProtoType_01", 100);
  auto clone_obj1_1 = ori_concrete_obj1->clone();
  auto clone_obj1_2 = ori_concrete_obj1->clone();
  ori_concrete_obj1->show();
  clone_obj1_1->show();
  clone_obj1_2->show();
  /**
   * ConcretePtototype obj:0x55f3e65ede90 name:ConcreteProtoType_01  value:100
   * ConcretePtototype obj:0x55f3e65edef0 name:ConcreteProtoType_01  value:100
   * ConcretePtototype obj:0x55f3e65edf30 name:ConcreteProtoType_01  value:100
   */

  clone_obj1_1 = std::make_unique<ConcretePrototype1>("Updated_ConcreteProtoType_01", 120);
  ori_concrete_obj1->show();
  clone_obj1_1->show();
  clone_obj1_2->show();
  /**
   * ConcretePtototype obj:0x55f3e65ede90 name:ConcreteProtoType_01  value:100
   * ConcretePtototype obj:0x55f3e65ee3f0 name:Updated_ConcreteProtoType_01  value:120
   * ConcretePtototype obj:0x55f3e65edf30 name:ConcreteProtoType_01  value:100
   */

  return 0;
}

代码分析

1. 原型接口(Prototype):
   - 声明了一个克隆方法 `clone` 和一个显示方法 `show`。

2. 具体原型类(ConcretePrototype):
   - 实现了 `clone` 方法,返回自身的克隆(使用 `std::make_unique` 创建新的对象)。
   - 实现了 `show` 方法,输出对象的名称。

3. 客户端代码(main 函数):
   - 创建了一个原型对象 `original`。
   - 通过调用 `clone` 方法生成两个克隆对象 `clone1` 和 `clone2`。
   - 显示原型对象及其克隆的名称。
   - 修改 `clone1` 的名称并输出变化。


http://www.kler.cn/news/293179.html

相关文章:

  • 数学建模强化宝典(14)Fisher 最优分割法
  • 电脑硬盘数据丢失了怎么恢复?简单实用的硬盘数据找回的方法
  • JS生成二维码QRCode代码
  • EI会议推荐-第二届大数据与数据挖掘国际会议(BDDM 2024)
  • 地平线SuperDrive首秀:千人研发投入,出场即「比肩第一梯队」
  • C++ STL-List容器概念及应用方法详解
  • 如何优化Oracle数据库的SQL性能?
  • MySQL5.7.36之高可用架构部署-MHA-VIP漂移
  • 【无标题】一起学习LeetCode热题100道(67/100)
  • Pikachu靶场之RCE漏洞详解
  • 通义灵码助力高校开学第一课,“包”你满意,新学期加油!
  • 后端开发面经系列--快手音视频C++开发
  • 集成电路学习:什么是RAM随机存取存储器
  • 【时时三省】(C语言基础)指针进阶 例题3
  • C++身份证实名认证-实名制-身份证三要素认证-身份认证-身份验真-接口
  • Proxifier代理配置
  • 【奔驰中国-注册安全分析报告】
  • 机器学习-33-机理模型和非机理模型
  • 【Focal Loss 本质】
  • 【开源免费】基于SpringBoot+Vue.JS在线竞拍系统(JAVA毕业设计)
  • 加载SQLite扩展的db.loadExtension方法
  • C#编写上位机通过OPC DA读取西门子PLC数据
  • 大数据开发:可视化组件Redash安装部署
  • springboot整合logback进行日志管理(上篇)
  • etc bashrc和 etc profile傻傻分不清楚?_
  • 怎么在mathtype中打空格 MathType空格键不能用
  • WHAT - React 函数与 useMemo vs useCallback
  • Redis安装步骤——离线安装与在线安装详解
  • 基于uniapp的登录状态保持(APP免登录)
  • 基于yolov8的西红柿检测系统python源码+onnx模型+评估指标曲线+精美GUI界面