设计模式——原型模式(prototype)

文章目录

引言

一句话说明原型模式------直接拷贝,而非再"设计"。

举个例子:现在你有一个工厂和一个产品的设计图,你造了一个流水线用来生产这个产品。这时候你就已经拥有了原型------设计图+流水线 ,你用它来生产相同的产品,而不是每次生产都重新画一个设计图和建一个流水线。换句话来说------原型广义 上的,代表的不仅仅是一个具体的产品,更重要的是它保留了这个产品的所有特征流程,在我们需要再次使用的时候就可以直接拷贝,而不是从头开始。

再比如:操作系统中的进程(Process)复制 ------ fork(),在 Linux 或 UNIX 系统中,当我们调用 fork() 系统调用时,会克隆当前进程,创建一个几乎一模一样的新进程(子进程)。这个新进程继承了父进程的大量状态信息(代码段、堆栈、文件描述符等)。

又如:游戏引擎中的对象克隆(Unity、Unreal)

在游戏中,大量游戏对象(敌人、子弹、粒子特效)通常基于**模板对象(Prefab)**克隆生成。在 Unity 中,一个敌人原型对象可能包含:

  • 模型(Mesh)
  • 动画
  • 攻击逻辑
  • 状态数据

游戏运行时会通过 Instantiate() 克隆原型:

csharp 复制代码
Enemy clone = Instantiate(enemyPrefab);

原型思想:

游戏世界中的对象不是手动 new 出来的,而是"复制"原型对象,减少初始化成本、保持一致性。

所以总结来说:

原型模式(Prototype Pattern)就是"拿来主义"------当创建成本高或复杂时,通过克隆已有对象来生成新的实例。

它的核心作用是:

  • 避免复杂的初始化过程;
  • 提升性能;
  • 让对象创建逻辑更灵活(可以运行时决定复制哪个原型)。

框架结构

每一个具体原型ConcreteProtoytpe实现Clone()用于返回自身的深拷贝 ,用户Client在操作的时候可以直接通过Clone()复制

实现

光看上面我们已经知道了原型模式是一个什么东西,但是在现实复杂系统中,我们还需要一个管理结构------管理已创建的原型和创建新的原型,使用户能够高效的搜索。

在下面的代码实例中,我结合了抽象工厂 + 原型模式实现了一个简单的原型管理器和原型结构

  • Prototype 定义克隆接口;
  • PrototypeA/B 提供深拷贝;
  • PrototypeFactory 负责初始创建;
  • PrototypeManager 缓存原型并克隆。
cpp 复制代码
#include <iostream>
#include <memory>
#include <unordered_map>

// =============================
// 1. 原型类型定义
// =============================
enum class PrototypeType {
    A,
    B,
    C,
    D,
};


// =============================
// 2. 原型抽象类
// =============================
class Prototype {
public:
    virtual std::unique_ptr<Prototype> clone() const = 0;
    virtual void show() const = 0; // 演示用途
    virtual ~Prototype() = default;
};

// =============================
// 3. 具体原型类
// =============================
class PrototypeA : public Prototype {
public:
    std::unique_ptr<Prototype> clone() const override {
        return std::make_unique<PrototypeA>(*this); // 深拷贝
    }
    void show() const override { std::cout << "Prototype A cloned\n"; }
};

class PrototypeB : public Prototype {
public:
    std::unique_ptr<Prototype> clone() const override {
        return std::make_unique<PrototypeB>(*this);
    }
    void show() const override { std::cout << "Prototype B cloned\n"; }
};

// =============================
// 4. 原型抽象工厂
// =============================
class PrototypeFactory {
public:
    std::unique_ptr<Prototype> build(PrototypeType typeId) {
        switch (typeId) {
            case PrototypeType::A:
                return std::make_unique<PrototypeA>();
            case PrototypeType::B:
                return std::make_unique<PrototypeB>();
            default:
                throw std::invalid_argument("Unknown prototype type");
        }
    }
};

// =============================
// 5. 原型管理器
// =============================
// hash 特化以支持 unordered_map
template<>
struct m_hash<PrototypeType> {
    size_t operator()(const PrototypeType& t) const noexcept {
        return static_cast<size_t>(t);
    }
};
class PrototypeManager {
private:
    using typeId = PrototypeType;
    std::unordered_map<typeId, std::unique_ptr<Prototype>, m_hash> table;
    PrototypeFactory factory;

public:
    std::unique_ptr<Prototype> get(PrototypeType type) {
        // 原型未注册则创建
        if (table.find(type) == table.end()) {
            table[type] = factory.build(type);
        }
        // 返回克隆副本
        return table[type]->clone();
    }
};

// =============================
// 6. 测试用例
// =============================
int main() {
    PrototypeManager manager;

    auto a1 = manager.get(PrototypeType::A);
    a1->show();

    auto b1 = manager.get(PrototypeType::B);
    b1->show();

    auto a2 = manager.get(PrototypeType::A);
    a2->show();
}

总结

原型模式(Prototype Pattern)就是"拿来主义"------当创建成本高或复杂时,通过克隆已有对象来生成新的实例。

相关推荐
兵bing3 小时前
设计模式-访问者模式
设计模式·访问者模式
python零基础入门小白3 小时前
【万字长文】大模型应用开发:意图路由与查询重写设计模式(从入门到精通)
java·开发语言·设计模式·语言模型·架构·大模型应用开发·大模型学习
MC丶科4 小时前
Java设计模式漫画英雄宇宙-工厂模式 —Factory博士的“超级英雄制造机”!
java·设计模式·漫画
明洞日记4 小时前
【设计模式手册013】命令模式 - 请求封装的优雅之道
java·设计模式·命令模式
ada0_ada15 小时前
行为型模式:②命令模式(Command Pattern)
设计模式
o0向阳而生0o6 小时前
113、23种设计模式之中介者模式(21/23)
设计模式·中介者模式
心语星光6 小时前
23种经典设计模式
设计模式
ACE19851 天前
AI Agent 设计模式深度解析:提示链(Prompt Chaining)模式
人工智能·设计模式·prompt
明洞日记1 天前
【设计模式手册012】责任链模式 - 请求处理的流水线艺术
java·设计模式·责任链模式
ada0_ada11 天前
行为型模式:①责任链模式(Chain of Responsibility Pattern)
设计模式