创建型模式——原型模式

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

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

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

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

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

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

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
相关推荐
倒头就睡的小比特5 天前
算法竞赛C++常用的STL
c++·算法
weilx12345 天前
C++笔记-文件IO-<fcntl.h>
c++
Smileyqp沛沛5 天前
前端?C++ ?较大差异基础罗列
c++·基础·前端转c++
C语言小火车5 天前
C/C++ 为什么需要编译器?
开发语言·c++
旖旎夜光5 天前
力控面试题 01.01: 判定字符是否唯一(位运算) —— 题解
c++·学习·算法·leetcode·力控
吞下星星的少年·-·5 天前
C++ 萌新语法入门篇
c++·算法比赛
霍霍的袁5 天前
【C++】map 和 set 的使用 | 从用法到底层
开发语言·c++·学习·visual studio
another heaven5 天前
【算法/C++ MD5算法能否逆解码?原理、C++实现与同类哈希算法对比】
c++·算法·哈希算法
程序猿编码5 天前
告别改源码适配模型:纯 C++ 可配置 LLM 推理引擎,全格式全结构兼容
c++·大模型·llm·推理引擎
此生决int5 天前
深入理解C++系列(20)——C++11(下)
开发语言·c++