DEV Community

Ariston
Ariston

Posted on

设计模式

创建型模式

一、工厂模式

1.简单工厂模式

最废物的模式,没人用

2.工厂方法模式

就是定义一个抽象工厂类,然后把创建对象推迟到子类中,

class Product{
public:
virtual string operation() = 0;
};

class ConcreteProduteA : public Product{
public:
string operation() const override {
return "result of ConcreteProduct";
}
};
//符合开闭原则,使用时只需要添加新的类就好了
Enter fullscreen mode Exit fullscreen mode

3.抽象工厂模式

这个其实还是有点复杂的,涉及到产品组,总而言之就是用1风格的工厂能够创建出1风格的产品组
先用语言来说,两个抽象产品PA和PB,然后我们分别用具体的产品类集成他们也就是PA1、PB1还有PA2、PB2,所以1和2分别就是产品族,然后我们再搞一个抽象工厂AB,其中有两个具体的工厂分别是CF1和CF2,作用就是创建出1风格的产品族合2风格的产品族。
然后我们设置一个client函数,其中参数是AB,当我们想创建1风格产品族的时候,只需要把CF1传进去就ok
好处就是我们想要添加新的产品族只需要新增一个具体的工厂即可,客户端代码不用动

#include <iostream>
#include <string>

// 抽象产品A
class AbstractProductA {
public:
    virtual std::string UsefulFunctionA() const = 0;
};

// 抽象产品B
class AbstractProductB {
public:
    virtual std::string UsefulFunctionB() const = 0;
    virtual std::string AnotherUsefulFunctionB(const AbstractProductA& collaborator) const = 0;
};

// 具体产品A1
class ConcreteProductA1 : public AbstractProductA {
public:
    std::string UsefulFunctionA() const override {
        return "The result of the product A1.";
    }
};

// 具体产品A2
class ConcreteProductA2 : public AbstractProductA {
public:
    std::string UsefulFunctionA() const override {
        return "The result of the product A2.";
    }
};

// 具体产品B1
class ConcreteProductB1 : public AbstractProductB {
public:
    std::string UsefulFunctionB() const override {
        return "The result of the product B1.";
    }
    std::string AnotherUsefulFunctionB(const AbstractProductA& collaborator) const override {
        const std::string result = collaborator.UsefulFunctionA();
        return "The result of the B1 collaborating with ( " + result + " )";
    }
};

// 具体产品B2
class ConcreteProductB2 : public AbstractProductB {
public:
    std::string UsefulFunctionB() const override {
        return "The result of the product B2.";
    }
    std::string AnotherUsefulFunctionB(const AbstractProductA& collaborator) const override {
        const std::string result = collaborator.UsefulFunctionA();
        return "The result of the B2 collaborating with ( " + result + " )";
    }
};

// 抽象工厂
class AbstractFactory {
public:
    virtual AbstractProductA* CreateProductA() const = 0;
    virtual AbstractProductB* CreateProductB() const = 0;
};

// 具体工厂1
class ConcreteFactory1 : public AbstractFactory {
public:
    AbstractProductA* CreateProductA() const override {
        return new ConcreteProductA1();
    }
    AbstractProductB* CreateProductB() const override {
        return new ConcreteProductB1();
    }
};

// 具体工厂2
class ConcreteFactory2 : public AbstractFactory {
public:
    AbstractProductA* CreateProductA() const override {
        return new ConcreteProductA2();
    }
    AbstractProductB* CreateProductB() const override {
        return new ConcreteProductB2();
    }
};

// 客户端代码
void ClientCode(const AbstractFactory& factory) {
    const AbstractProductA* productA = factory.CreateProductA();
    const AbstractProductB* productB = factory.CreateProductB();
    std::cout << productB->UsefulFunctionB() << std::endl;
    std::cout << productB->AnotherUsefulFunctionB(*productA) << std::endl;
    delete productA;
    delete productB;
}

// 主函数
int main() {
    ConcreteFactory1* factory1 = new ConcreteFactory1();
    ClientCode(*factory1);
    delete factory1;
    ConcreteFactory2* factory2 = new ConcreteFactory2();
    ClientCode(*factory2);
    delete factory2;
    return 0;
}

Enter fullscreen mode Exit fullscreen mode

Top comments (0)