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

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

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

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

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

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

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.
相关推荐
努力实干8 小时前
面向对象设计原则——C++
开发语言·c++·数码相机
MobotStone10 小时前
AI 写代码越来越爽,但程序员可能越来越废了
人工智能·设计模式
syagain_zsx11 小时前
jsoncpp 使用指南:编译链接、Json::Value 取值与高频坑
c++·json·库·http格式
墨子夕11 小时前
设计模式——Java × Python 双语言精讲 · 23 个经典模式 + 现代演进(前言篇)
java·开发语言·设计模式
布莱克60512 小时前
理解 = delete 与 = default:两种赋值语义的差异
c++
小小、码农13 小时前
[特殊字符]【C++11】类的新功能全面解析:移动构造、移动赋值、default、delete、final 与 override
linux·开发语言·网络·c++
库玛西13 小时前
外部排序全解析:败者树、置换-选择排序与最佳归并树
c语言·数据结构·c++·笔记·考研·算法
墨子夕14 小时前
设计模式——Java × Python 双语言精讲 · 23 个经典模式 + 现代演进(第一篇)
java·python·设计模式
General_G15 小时前
创建型模式—原型(Prototype)模式
c++·设计模式·原型模式
syagain_zsx15 小时前
protobuf 实战:从 .proto 到 C++ 序列化的完整链路
c++·学习·程序人生·谷歌·protobuf