创建型模式——原型模式

原型设计模式适用于以下情况:

• 当一个系统应该独立于它的产品创建、构成和表示时;

• 当要实例化的类是在运行时指定时,例如,通过动态装载;

• 为了避免创建一个与产品类层次平行的工厂类层次时;

• 当一个类的实例只能有几个不同状态组合中的一种时;

下面是一个原型模式的示例程序:

cpp 复制代码
#include <iostream>
#include <memory>
#include <string>

// Step 1: The Prototype Base Class
class DocTemplate {
public:
    virtual ~DocTemplate() = default;
    
    // The "Make a Copy" operation
    virtual std::unique_ptr<DocTemplate> clone() const = 0;
    
    virtual void show() const = 0;
    virtual void setContent(const std::string& text) = 0;
};

// Step 2: Concrete Prototype (Resume Template)
class Resume : public DocTemplate {
private:
    std::string header_ = "=== RESUME TEMPLATE ===\nFormat: 1-Page, Arial Font\n";
    std::string content_ = "[Empty]";

public:
    // Clone copies the entire existing object state
    std::unique_ptr<DocTemplate> clone() const override {
        return std::make_unique<Resume>(*this); 
    }

    void setContent(const std::string& text) override { content_ = text; }
    
    void show() const override {
        std::cout << header_ << "Content: " << content_ << "\n\n";
    }
};

// Step 3: Concrete Prototype (Invoice Template)
class Invoice : public DocTemplate {
private:
    std::string header_ = "=== INVOICE TEMPLATE ===\nFormat: Table layout, Tax included\n";
    std::string content_ = "[Empty]";

public:
    std::unique_ptr<DocTemplate> clone() const override {
        return std::make_unique<Invoice>(*this);
    }

    void setContent(const std::string& text) override { content_ = text; }
    
    void show() const override {
        std::cout << header_ << "Content: " << content_ << "\n\n";
    }
};

// Client Code
int main() {
    // 1. Set up our golden master templates
    std::unique_ptr<DocTemplate> masterResume = std::make_unique<Resume>();
    std::unique_ptr<DocTemplate> masterInvoice = std::make_unique<Invoice>();

    // 2. Someone wants to write a resume. We CLONE the template.
    std::unique_ptr<DocTemplate> johnsResume = masterResume->clone();
    johnsResume->setContent("John Doe - Software Engineer Experience...");

    // 3. Someone wants to write an invoice. We CLONE the template.
    std::unique_ptr<DocTemplate> clientInvoice = masterInvoice->clone();
    clientInvoice->setContent("Order #1024 - Total: $150.00");

    // 4. Display the results
    std::cout << "--- Printing Cloned Documents ---\n";
    johnsResume->show();
    clientInvoice->show();

    return 0;
}

程序运行结果如下:

shell 复制代码
$ g++ -o main main.cpp
$ ./main
--- Printing Cloned Documents ---
=== RESUME TEMPLATE ===
Format: 1-Page, Arial Font
Content: John Doe - Software Engineer Experience...

=== INVOICE TEMPLATE ===
Format: Table layout, Tax included
Content: Order #1024 - Total: $150.00
相关推荐
用户938515635071 小时前
工厂模式与 Nest.js 核心思想 —— 从蜜雪冰城到企业级架构
后端·设计模式·nestjs
XLYcmy1 小时前
京东 算法实习一面 下+手撕
c++·python·llm·概率论·数据处理·训练·codebert
反转180度2 小时前
Qt 6 + C++17 实现 SFTP 批量部署:工业设备运维工具实战解析
运维·c++·qt
码匠许师傅3 小时前
【C++ 面试真题】聊聊 C++ 的多继承与虚继承
开发语言·c++·面试
有点。5 小时前
C++认识数
开发语言·c++
烬羽5 小时前
从 nest new 到 Hello World:一个最小 NestJS 项目,讲透工厂 + 装饰器 + 模块化
设计模式·node.js·nestjs
2023自学中5 小时前
Qt 简易聊天室(第二篇),单线程 改为 多线程,方便后续增加文件传输功能
c++·qt
Tyler_TXZ6 小时前
C++C语言之——二叉树
c语言·开发语言·数据结构·c++·二叉树
有点。7 小时前
C++二叉树二(练习题)
数据结构·c++·算法·图论
不会代码的小猴7 小时前
C++新增关键字
开发语言·c++·笔记