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

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

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

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

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

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

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.
相关推荐
Keven_1130 分钟前
算法札记:ACM适用的很骚的C++语法(持续更新)
开发语言·c++
mengzhi啊1 小时前
QPluginLoader 动态 DLL 插件版,支持插件之间通信,广播。请求和应答
c++·qt
老赵的博客4 小时前
c++ QT之动态库加载问题
c++·qt
(Charon)4 小时前
【C++】定时器进阶:使用最小堆管理定时任务
c++·算法
hansang_IR4 小时前
【题解】 [省选联考 2021 A/B 卷] 卡牌游戏
c++·算法
lzx_0025 小时前
C++11(一)
开发语言·c++·算法
小七在进步6 小时前
C++入门(2)
java·jvm·c++
en.en..7 小时前
C语言核心解析:#define与typedef本质区别
开发语言·c++·算法
码匠许师傅7 小时前
【设计模式精讲】26.策略模式(Strategy)
c++·设计模式·策略模式·uml
果果燕8 小时前
实习笔记(六)主机按钮状态上报完整流程
开发语言·c++·php