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

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

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

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

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

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

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.
相关推荐
Adellle6 小时前
设计模式指南 (Java 版) ㊁
java·开发语言·设计模式
自信150413057597 小时前
重生之从0开始学习c++之——vector-中篇
c++·学习·算法
是2的10次方啊7 小时前
设计模式与生活⑱:工资还没焐热,账单已经凑成一摞
设计模式
ShineWinsu10 小时前
对于Qt:QDial常见属性以及信号的解析
开发语言·c++·qt·gui·客户端·旋钮·qdial
科技苑18 小时前
C++ CAS与ABA问题详解,无锁编程的经典陷阱怎么解决?
c++·人工智能
百思不得小李20 小时前
一个复杂的动态表单是如何实现的
前端·设计模式
BaiPeter1 天前
ZPL文件预览
开发语言·c++
爱你真是太好了1 天前
上下文组成以及如何压缩
设计模式
悠仁さん1 天前
【C++】哈希表
c++·算法·散列表
Severus_black1 天前
【C++初阶】list使用及相关实现,思路顺滑流畅!
c语言·数据结构·c++·stl·list