设计模式——原型模式(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)就是"拿来主义"------当创建成本高或复杂时,通过克隆已有对象来生成新的实例。

相关推荐
Zane199412 小时前
策略模式现在该不该上?一次讲清楚过度设计和设计不足怎么找平衡
设计模式
她说..13 小时前
常见设计模式-模板方法模式
java·spring·设计模式·springboot
xiaofeiyang1501 天前
第六章 · 桥接 — 三支毛笔,画出九种颜色
设计模式
Shadow(⊙o⊙)2 天前
OTOL设计模式 One Thread One Loop
服务器·网络·设计模式
sarasuki2 天前
如何让LLM 能在半夜偷偷打开网易云呢?
人工智能·设计模式·agent
小王师傅662 天前
【设计模式】装饰模式(四):框架源码实战——从 Java I/O 到 Spring 到 MyBatis
java·设计模式
执明wa2 天前
Android RecyclerView 多类型, 多种 Item
android·xml·开发语言·设计模式·android studio
sarasuki2 天前
如何让 Agent 获取更多的能力?插件 / 技能系统
人工智能·设计模式·agent
sarasuki2 天前
如何让 Agent 安全运行你的命令 :命令分级 + Hook + 读写锁
人工智能·设计模式·agent
Zane19942 天前
技术不难,为什么项目却越改越不敢动?聊聊复杂系统开发该怎么想
设计模式