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

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

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

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

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

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

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.
相关推荐
无名猿17 小时前
new/delete 与 malloc/free:为什么绝对不能混用
c++·内存管理·现代c++·踩坑记录
长按助力退休18 小时前
设计模式实战,这 5 个最常用
设计模式
2601_9622186119 小时前
C++中decltype关键字的实现
开发语言·c++
繁华的地方不一定留下你的脚印19 小时前
C++ std::variant 与 std::visit:安全保存多种类型,写清每个处理分支
开发语言·c++
程序员老陆20 小时前
WIN32_LEAN_AND_MEAN:Windows 头文件里的“精简编译开关”
c++·windows
Carl_奕然20 小时前
【智能体】Agent的四种设计模式之:Multi-Agent Collaboration
设计模式
hold?fish:palm20 小时前
45 二叉树的右视图
开发语言·c++·算法
Lost of 程序猿20 小时前
建造者模式实战:告别“十参数构造函数“的数据导出任务
后端·设计模式·c#·asp.net
SL_staff21 小时前
合规性折旧:当知识资产因主权缺位在审计中‘功能性清零’
java·设计模式·开源
ShineWinsu21 小时前
对于Redis:Redis的认识以及分布式系统的解析
linux·数据库·c++·redis·缓存·消息队列·分布式架构