创建型模式——工厂方法模式

工厂方法设计模式适用于以下情况:

• 当一个类不知道它所必须创建的对象的类的时候;

• 当一个类希望由它的子类来指定它所创建的对象的时候;

• 当类将创建对象的职责委托给多个帮助子类中的某一个;

下面是一个工厂方法模式的示例程序:

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

// --- Step 1: Abstract Product ---
// Defines the common interface for all objects the factory can create.
class Document {
public:
    virtual ~Document() = default; // Virtual destructor is crucial for polymorphism
    virtual void Read() const = 0;
};

// --- Step 2: Concrete Products ---
// Provide specific implementations of the Product interface.
class PdfDocument : public Document {
public:
    void Read() const override {
        std::cout << "Reading content from a PDF document.\n";
    }
};

class WordDocument : public Document {
public:
    void Read() const override {
        std::cout << "Reading content from a Word WordDocument.\n";
    }
};

// --- Step 3: Creator (Abstract Factory Class) ---
// Declares the factory method that returns a Product object.
class DocumentApplication {
public:
    virtual ~DocumentApplication() = default;

    // The core Factory Method
    virtual std::unique_ptr<Document> CreateDocument() const = 0;

    // The Creator often contains business logic relying on the Product
    void OpenDocument() const {
        // Call the factory method to create a Product object
        std::unique_ptr<Document> doc = CreateDocument();
        // Use the product without knowing its concrete class
        doc->Read();
    }
};

// --- Step 4: Concrete Creators ---
// Override the factory method to change the resulting product's type.
class PdfApplication : public DocumentApplication {
public:
    std::unique_ptr<Document> CreateDocument() const override {
        return std::make_unique<PdfDocument>();
    }
};

class WordApplication : public DocumentApplication {
public:
    std::unique_ptr<Document> CreateDocument() const override {
        return std::make_unique<WordDocument>();
    }
};

// --- Client Code ---
int main() {
    // Client interacts with creators and products via their abstract interfaces
    std::unique_ptr<DocumentApplication> app;

    std::cout << "Client: Launching PDF Application...\n";
    app = std::make_unique<PdfApplication>();
    app->OpenDocument();

    std::cout << "\nClient: Launching Word Application...\n";
    app = std::make_unique<WordApplication>();
    app->OpenDocument();

    return 0;
}

程序运行结果如下:

shell 复制代码
$ g++ -o main main.cpp
$ ./main
Client: Launching PDF Application...
Reading content from a PDF document.

Client: Launching Word Application...
Reading content from a Word WordDocument.
相关推荐
C++ 老炮儿的技术栈几秒前
Qt5.9.1 Windows 完整开发环境搭建流程
开发语言·c++·windows·qt·编辑器·代码化
键盘会跳舞44 分钟前
C++:std::tuple 源码级深度拆解——变参模板、SFINAE与模板元编程核心技巧
开发语言·c++·sfinae·变参模板
bkspiderx1 小时前
从零开始:VS Code搭建C/C++开发环境全指南(Windows/macOS/Linux)
c语言·c++·windows·vs code·c/c++开发环境
东华万里1 小时前
第40篇C++核心基础与工程实践:从底层逻辑到避坑指南
开发语言·c++·面试·大学生专区
luj_17681 小时前
元设计的诱惑与现实
c语言·开发语言·c++·经验分享·算法
小星星闪亮登场1 小时前
ST表--倍增思想
开发语言·数据结构·c++·算法·思维
东华万里1 小时前
第40篇 C++核心基础与工程实践:从底层逻辑到避坑指南
开发语言·c++·大学生专区
AC赳赳老秦1 小时前
OpenClaw 多源采集公开行业数据:从原始信息到研究报告初稿的自动化实践
java·c语言·c++·python·php·deepseek·openclaw
玩三国杀玩的2 小时前
Pytorch-c++-CUDA
c++·pytorch·python·深度学习
wangjialelele4 小时前
动态规划DP经典题型总结(Java、C++):路径、子数组、子序列、背包问题详解
java·c++·算法·面试·动态规划·代理模式