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

相关推荐
码匠许师傅1 小时前
【设计模式精讲】7.建造者模式(Builder)
设计模式·建造者模式
码匠许师傅2 小时前
【设计模式精讲】6.抽象工厂(Abstract Factory)
c++·设计模式
workflower16 小时前
人形机器人灵巧手产业链
人工智能·设计模式·机器人·云计算·无人机
Java后端的Ai之路18 小时前
03、Python - 抽象工厂模式
开发语言·python·设计模式·抽象工厂模式·通用智慧
AI人工智能+电脑小能手1 天前
大白话说Java设计模式-45-解释器模式(源码剖析篇)
java·设计模式·解释器模式·源码分析·pattern·spel·javacc
码匠许师傅1 天前
【设计模式精讲】4.单例模式(Singleton)
c++·单例模式·设计模式
雾削木1 天前
2026 编程领域热点知识速览:AI 辅助开发、设计模式实战与嵌入式高效迭代
人工智能·设计模式
神奇霸王龙2 天前
Codex MCP GA 实测:Qwen / GLM / Kimi / DeepSeek / MiniMax 调度 Codex 沙箱的真实成本
人工智能·ai·agent·ai编程·原型模式·mcp
AI人工智能+电脑小能手2 天前
大白话说Java设计模式-42-访问者模式(业务实战篇)
java·spring·设计模式·访问者模式·asm·营销规则·数据结构与操作分离
AI人工智能+电脑小能手2 天前
大白话说Java设计模式-43-访问者模式(源码剖析篇)
java·设计模式·访问者模式·源码分析·elementvisitor·asm classvisitor·spring spel