设计模式——原型模式(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 小时前
GoF设计模式——解释器模式
java·后端·spring·设计模式
大数据新鸟4 小时前
设计模式四大基本原则
设计模式
南一Nanyi9 小时前
依赖注入和控制反转
前端·设计模式·nestjs
tiankong121312 小时前
如何拓展多态下的子类
设计模式·c#
jctech15 小时前
让「小程序」跑起真正的原生代码:ComboNative 多进程原生小程序运行时 · Alpha 首发
android·设计模式·架构
Kel1 天前
Node.js 单线程高并发:从内核中断到 JS 回调的完整技术栈
设计模式·架构·node.js
解局易否结局2 天前
ArkUI MVI 架构实战:单向数据流设计模式在 HarmonyOS NEXT 中的深度实践
华为·设计模式·架构·harmonyos
解局易否结局3 天前
ArkTS 设计模式与架构进阶:构建可维护的 HarmonyOS 应用
华为·设计模式·架构·harmonyos
happyness443 天前
AI编程与设计模式
设计模式·ai编程
就是小王同学啊3 天前
Spring小技巧之设计模式
sql·spring·设计模式