设计模式——原型模式(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 天前
领域特定语言(Domain-Specific Language, DSL)
设计模式·程序员·架构
Larcher3 天前
AI Loop:让AI像人一样自主完成任务的核心机制
javascript·人工智能·设计模式
咖啡八杯4 天前
GoF设计模式——享元模式
java·spring·设计模式·享元模式
想吃火锅10054 天前
【前端手撕】instanceof
前端·javascript·原型模式
:mnong4 天前
学习创建结构行为设计模式
设计模式
UXbot5 天前
帮助企业低门槛开展AI应用开发的平台推荐
前端·低代码·ui·交互·产品经理·原型模式·web app
w_t_y_y5 天前
Agent设计模式(四)多模态融合模式(Multi-Modal Fusion)
设计模式
zhouhui0015 天前
订单状态的 if-else 地狱上线就崩——状态模式的工业级落地
设计模式
geovindu5 天前
go: Reactor Pattern
开发语言·后端·设计模式·golang·反应器模式