创建型模式——原型模式

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

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

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

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

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

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

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
相关推荐
高晶18 小时前
CANopen 在 BMS 系统中的 C 语言实战指南
设计模式·程序员
weixin_3077791318 小时前
C++代码实现MATLAB中的fitrtree函数功能
开发语言·c++·算法·matlab
AI wins18 小时前
c++新增頭文件
c++
Shadow(⊙o⊙)18 小时前
仿muduo实现高并发服务器——事件就绪管理Channel.hpp
服务器·c++
by2099918 小时前
哈希表——快速找到数据:冲突、删除与扩容的实现
数据结构·c++·哈希算法·散列表
博风18 小时前
13、设计模式应用举例:工厂方法模式:创建药品对象
设计模式·工厂方法模式
音草19 小时前
C++ 继承详解:从概念到菱形继承
c++
weixin_3077791319 小时前
C++代码实现MATLAB中的spectralcluster函数功能
开发语言·c++·算法·matlab
xiaoye-duck20 小时前
《C++ 项目:手写大模型接入 SDK》Trae IDE 开发环境搭建与依赖管理
c++·ide·语言模型
沫璃染墨20 小时前
《Qt从零入门系列(十四):QThread多线程详解——从线程创建到客户端阻塞处理》
开发语言·c++·笔记·qt·ui·交互·线程