C++创建型模式之原型模式

C++ 原型模式(Prototype Pattern)

1. 解决的问题

原型模式(Prototype Pattern)是一种创建型设计模式,用于解决对象创建的问题,特别是在需要创建多个相似对象时,避免使用重复的构造代码。原型模式通过复制已有对象(原型)来创建新对象,而不是通过实例化一个类来创建。

2. 适用场景
  • 当系统需要创建多个相似对象,并且这些对象之间的差异只是部分属性值不同。
  • 当对象的创建过程比较复杂,并且通过复制现有对象可以简化创建过程。
  • 当需要避免使用子类的创建方式来生成对象时。
3. 模式的参与者角色
  • Prototype(抽象原型):声明一个克隆自身的接口。
  • ConcretePrototype(具体原型):实现克隆自身的操作。
  • Client(客户端):使用原型对象来克隆新的对象。
4. 示例代码

假设我们正在开发一个复杂的角色扮演游戏(RPG),游戏中有各种不同的角色,这些角色可以是玩家角色(Player)或敌人角色(Enemy)。每个角色都有不同的属性和能力,例如生命值、攻击力、防御力等。为了简化角色的创建过程,我们可以使用原型设计模式来复制现有的角色,并根据需要进行微调。

角色类图
复制代码
+----------------+
|   Prototype    |
|----------------|
| + clone()      |
| + print()      |
+----------------+
        ^
        |
+----------------+
|    Player      |
|----------------|
| + clone()      |
| + print()      |
+----------------+
        ^
        |
+----------------+
|    Enemy       |
|----------------|
| + clone()      |
| + print()      |
+----------------+
代码
cpp 复制代码
#include <iostream>
#include <string>
#include <memory>
#include <vector>

// Prototype 接口
class Prototype {
public:
    virtual ~Prototype() {}
    virtual std::unique_ptr<Prototype> clone() const = 0;
    virtual void print() const = 0;
    virtual void setHealth(int health) = 0;
    virtual void setAttack(int attack) = 0;
    virtual void setDefense(int defense) = 0;
};

// BaseCharacter 抽象类
class BaseCharacter : public Prototype {
public:
    BaseCharacter(std::string name, int health, int attack, int defense)
        : name_(name), health_(health), attack_(attack), defense_(defense) {}

    void setHealth(int health) override { health_ = health; }
    void setAttack(int attack) override { attack_ = attack; }
    void setDefense(int defense) override { defense_ = defense; }

    void print() const override {
        std::cout << "Name: " << name_ << ", Health: " << health_
                  << ", Attack: " << attack_ << ", Defense: " << defense_ << std::endl;
    }

protected:
    std::string name_;
    int health_;
    int attack_;
    int defense_;
};

// Player 具体原型
class Player : public BaseCharacter {
public:
    Player(std::string name, int health, int attack, int defense)
        : BaseCharacter(name, health, attack, defense) {}

    std::unique_ptr<Prototype> clone() const override {
        return std::make_unique<Player>(*this);
    }
};

// Enemy 具体原型
class Enemy : public BaseCharacter {
public:
    Enemy(std::string name, int health, int attack, int defense)
        : BaseCharacter(name, health, attack, defense) {}

    std::unique_ptr<Prototype> clone() const override {
        return std::make_unique<Enemy>(*this);
    }
};

// 客户端代码
int main() {
    // 创建原型对象
    auto playerPrototype = std::make_unique<Player>("Hero", 100, 20, 15);
    auto enemyPrototype = std::make_unique<Enemy>("Goblin", 50, 10, 5);

    // 创建角色列表
    std::vector<std::unique_ptr<Prototype>> characters;

    // 克隆玩家角色
    auto player1 = playerPrototype->clone();
    player1->setHealth(80); // 微调生命值
    characters.push_back(std::move(player1));

    auto player2 = playerPrototype->clone();
    player2->setHealth(90); // 微调生命值
    player2->setAttack(25); // 微调攻击力
    characters.push_back(std::move(player2));

    // 克隆敌人角色
    auto enemy1 = enemyPrototype->clone();
    characters.push_back(std::move(enemy1));

    auto enemy2 = enemyPrototype->clone();
    enemy2->setHealth(60); // 微调生命值
    enemy2->setAttack(15); // 微调攻击力
    characters.push_back(std::move(enemy2));

    // 打印所有角色信息
    for (const auto& character : characters) {
        character->print();
    }

    return 0;
}
代码说明
  1. Prototype 接口定义了克隆和打印方法,以及设置角色属性的方法。
  2. BaseCharacter 是一个抽象类,实现了 Prototype 接口的通用部分,包括角色属性的设置和打印方法。
  3. Player 和 Enemy 是具体原型类,继承自 BaseCharacter,并实现了 clone 方法。
  4. Client 在 main 函数中创建了原型对象,并通过克隆这些原型对象来创建新的角色。客户端可以根据需要微调角色的属性。

总结

通过原型设计模式,我们可以轻松地复制现有角色,避免了重复的构造代码,并且可以根据需要对克隆的角色进行微调。这种模式在复杂的游戏场景中非常有用,特别是在需要创建多个相似角色时。

原型模式与 C++ 拷贝构造函数的相似性与不同点

相似性
  1. 对象复制:

    • 原型模式:原型模式的核心思想是通过复制已有对象来创建新对象。
    • 拷贝构造函数:拷贝构造函数用于从已有对象创建一个新对象。
  2. 避免重复构造:

    • 原型模式:避免了重复的构造代码,通过复制已有对象来创建新对象。
    • 拷贝构造函数:避免了重复的构造代码,通过复制已有对象来创建新对象。
  3. 对象状态的复用:

    • 原型模式:可以复用已有对象的状态来创建新对象。
    • 拷贝构造函数:可以复用已有对象的状态来创建新对象。
不同点
  1. 设计模式 vs. 语言特性:

    • 原型模式:是一种设计模式,属于面向对象编程的设计原则之一。
    • 拷贝构造函数:是 C++ 语言的一个特性,用于实现对象的复制。
  2. 接口定义:

    • 原型模式 :通常定义一个 clone() 方法,用于克隆对象。
    • 拷贝构造函数 :是类的构造函数,定义为 ClassName(const ClassName& other),用于从已有对象创建新对象。
  3. 实现方式:

    • 原型模式 :可以由程序员显式实现 clone() 方法,并在方法内部调用拷贝构造函数或赋值操作符。
    • 拷贝构造函数:由编译器自动生成或由程序员显式实现。
  4. 灵活性:

    • 原型模式:提供了更灵活的对象复制机制,可以在运行时选择不同的原型进行复制。
    • 拷贝构造函数:是类的固有特性,无法在运行时选择不同的构造方式。
  5. 使用场景:

    • 原型模式:适用于需要创建多个相似对象,并且这些对象之间的差异只是部分属性值不同。
    • 拷贝构造函数:适用于任何需要从已有对象创建新对象的场景。
示例代码
cpp 复制代码
#include <iostream>
#include <string>
#include <memory>

// 使用原型模式的类
class Prototype {
public:
    virtual ~Prototype() {}
    virtual std::unique_ptr<Prototype> clone() const = 0;
    virtual void print() const = 0;
};

class ConcretePrototype : public Prototype {
public:
    ConcretePrototype(std::string name) : name_(name) {}

    std::unique_ptr<Prototype> clone() const override {
        return std::make_unique<ConcretePrototype>(*this);
    }

    void print() const override {
        std::cout << "ConcretePrototype: " << name_ << std::endl;
    }

private:
    std::string name_;
};

// 使用拷贝构造函数的类
class CopyConstructorExample {
public:
    CopyConstructorExample(std::string name) : name_(name) {}

    CopyConstructorExample(const CopyConstructorExample& other) : name_(other.name_) {
        std::cout << "Copy Constructor Called" << std::endl;
    }

    void print() const {
        std::cout << "CopyConstructorExample: " << name_ << std::endl;
    }

private:
    std::string name_;
};

int main() {
    // 原型模式示例
    auto prototype1 = std::make_unique<ConcretePrototype>("Prototype1");
    auto clone1 = prototype1->clone();
    clone1->print();

    // 拷贝构造函数示例
    CopyConstructorExample example1("Example1");
    CopyConstructorExample example2 = example1;
    example2.print();

    return 0;
}

总结

  • 相似性:原型模式和拷贝构造函数都用于对象的复制,避免了重复的构造代码。
  • 不同点 :原型模式是一种设计模式,通过 clone() 方法实现对象复制;拷贝构造函数是 C++ 语言特性,通过 ClassName(const ClassName& other) 实现对象复制。原型模式提供了更灵活的对象复制机制。
相关推荐
用户76241175670533 分钟前
第十五章 人工智能基础
c++
Tt有了t3 小时前
设计模式速查
java·python·设计模式
一条Go3 小时前
四个进程,一把锁:Alioth 的告警评测为什么不需要消息队列
后端·设计模式·架构
爱写Bug的浮游3 小时前
unique_ptr 零开销的真相与边界
c++·facebook
xiaoye-duck4 小时前
《C++ 项目:手写大模型接入 SDK》ChatGPT接口设计(下):流式响应实现和测试
网络·c++·语言模型·chatgpt
落樱弥城5 小时前
Perf 逐步探查性能缺口并优化
c++·性能优化
坤坤子吖5 小时前
Linux网络编程:UDP进阶,从字典服务器到简单聊天室
linux·服务器·网络·c++·笔记·学习
Rabitebla6 小时前
【Linux学习笔记】进程篇(九):进程的创建与退出——fork、写时拷贝与退出码
linux·c++·笔记·学习
时间的拾荒人6 小时前
CMake 实战指南:从基础语法到静态库/动态库制作与链接
开发语言·c++·面试
道尔柯南6 小时前
C++ STL 入门详解:从认识标准模板库到掌握学习方法
java·c++·学习方法