21xrx.com
2024-09-19 09:46:43 Thursday
登录
文章检索 我的文章 写文章
C++ 抽象工厂模式介绍
2023-07-03 19:19:10 深夜i     --     --
C++ 抽象工厂模式 介绍

抽象工厂模式是一种创建型设计模式,它通过提供一个接口,来创建相关或者依赖对象的家族,而不需要明确指定具体类。

该模式的主要目标是在不同的客户端上保持一致的接口,同时分离了客户端和具体实现的细节,使得接口与实现之间具有松耦合的关系。这种松耦合使得应用程序更加灵活和可维护。

抽象工厂模式由以下几个组件组成:

1. 抽象工厂:定义了一组创建相关或依赖对象的接口。

2. 具体工厂:实现抽象工厂中定义的接口,创建相关或依赖对象。

3. 抽象产品:定义了一组产品对象的接口。

4. 具体产品:实现抽象产品中定义的接口。

以下是使用抽象工厂模式的示例:

首先,我们定义一个抽象产品接口:

class AbstractProductA {

 public:

  virtual ~AbstractProductA(){}

  virtual std::string UsefulFunctionA() const = 0;

};

class AbstractProductB {

public:

 virtual ~AbstractProductB(){}

 virtual std::string UsefulFunctionB() const = 0;

 virtual std::string AnotherUsefulFunctionB(

   const AbstractProductA& collaborator) const = 0;

};

然后,我们定义一个抽象工厂接口,该接口定义了一组创建相关或依赖产品的方法:

class AbstractFactory {

public:

 virtual ~AbstractFactory(){}

 virtual AbstractProductA* CreateProductA() const = 0;

 virtual AbstractProductB* CreateProductB() const = 0;

};

接下来,我们实现具体产品类:

class ConcreteProductA1 : public AbstractProductA {

public:

 std::string UsefulFunctionA() const override

  return "The result of the product A1.";

};

class ConcreteProductA2 : public AbstractProductA {

public:

 std::string UsefulFunctionA() const override

  return "The result of the product A2.";

};

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 the (" + result + ")";

 }

};

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 the (" + result + ")";

 }

};

最后,我们实现具体工厂类:

class ConcreteFactory1 : public AbstractFactory {

public:

 AbstractProductA* CreateProductA() const override {

  return new ConcreteProductA1();

 }

 AbstractProductB* CreateProductB() const override {

  return new ConcreteProductB1();

 }

};

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() {

 std::cout << "Client: Testing client code with the first factory type:\n";

 ConcreteFactory1* f1 = new ConcreteFactory1();

 ClientCode(*f1);

 delete f1;

 std::cout << "Client: Testing the same client code with the second factory type:\n";

 ConcreteFactory2* f2 = new ConcreteFactory2();

 ClientCode(*f2);

 delete f2;

 return 0;

}

输出:

Client: Testing client code with the first factory type:

The result of the product B1.

The result of the B1 collaborating with the (The result of the product A1.).

Client: Testing the same client code with the second factory type:

The result of the product B2.

The result of the B2 collaborating with the (The result of the product A2.).

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复