C++ 工厂模式(简单工厂模式、工厂方法模式、抽象工厂模式)

本文深入探讨了工厂模式在软件设计中的应用,包括简单工厂、工厂方法和抽象工厂模式,通过实例展示了如何利用这些模式来创建和管理对象,适用于不同场景的需求。

  利用工厂模式来生成对象,不需要关注对象的生成过程;还可以简化名称,以便使用。

简单工厂模式

  同一工厂生产多个产品。
  违背开放–封闭规则(开放指新版本设计时可以使用旧版本的代码;封闭指不能修改旧版本的代码)。因此适用于规模固定、不易发生产品改动的工厂。实现如下:

//简单工厂模式
#include<iostream>
class Fruit
{
public:
     Fruit(std::string nm)
         :mname(nm) {}
     virtual void operation() = 0;
protected:
     std::string mname;
};
class Apple :public Fruit
{
public:
     Apple(std::string name)
         :Fruit(name) {}
     void operation()
     {
         std::cout << "this is an Apple!" << std::endl;
     }
};
class Banana :public Fruit
{
public:
     Banana(std::string name)
         :Fruit(name) {}
     void operation()
     {
         std::cout << "this is a Banana!" << std::endl;
     }
};
class Pear :public Fruit
{
public:
     Pear(std::string name)
        :Fruit(name) {}
     void operation()
     {
         std::cout << "this is a Pear!" << std::endl;
     }
};
class Factory
{
public:
     Fruit* createFruit(int flag)
     {
         switch (flag)
         {
         case 1:
               return new Apple("apple");
               break;
         case 2:
               return new Banana("banana");
               break;

         default:
         return NULL;
         }
     }
};
int main()
{
     Factory fa;

     Fruit* pf1=fa.createFruit(1);
     pf1->operation();
     Fruit* pf2 = fa.createFruit(2);
     pf2->operation();
     return 0;
}

执行过程如下:
在这里插入图片描述

工厂方法模式

  一个工厂只能生产一种产品。
  适用于规模不固定、产品有变化的。

//工厂方法模式
//我们让F1工厂去生产苹果、F2生产香蕉
#include<iostream>
class Fruit
{
public:
     Fruit(std::string nm)
         :mname(nm) {}
     virtual void operation() = 0;
protected:
     std::string mname;
};
class Apple :public Fruit
{
public:
     Apple(std::string name)
         : Fruit(name) {}
     void operation()
     {
         std::cout << "this is an Apple!" << std::endl;
     }
};
class Banana :public Fruit
{
public:
     Banana(std::string name)
          :Fruit(name) {}
     void operation()
     {
          std::cout << "this is a Banana!" << std::endl;
     }
};
class Factory
{
public:
     Factory(std::string name)
         :mname(name) {}
     virtual Fruit* createFruit() = 0;
protected:
     std::string mname;
};
class F1 :public Factory
{
public:
     F1(std::string name)
        :Factory(name) {}
     Fruit* createFruit()
     {
         return new Apple("apple");
     }
};
class F2 :public Factory
{
public:
     F2(std::string name)
         :Factory(name) {}
     Fruit* createFruit()
     {
          return new Banana("banana");
     }
};
int main()
{
     Factory* pfa = new F1("f1");
     Fruit* pf = pfa->createFruit();
     pf->operation();
     return 0;
}

结果如下:
在这里插入图片描述

抽象工厂模式

  抽象工厂模式其实是用来创建一组相互依赖的对象。
  假设我们要生产13.5的笔记本和15.6的笔记本,那么需要生产对应的13.5和15.6的屏幕以及13.5和15.6的外壳。也就是现在需要生产A(13.5的笔记本)、B(15.6的笔记本)两种的产品,A产品需要生产A1(13.5的屏幕)、B1(13.5的外壳);B产品需要生产A2(15.6的屏幕)、B2(15.6的外壳)。
在这里插入图片描述

#include<iosream>
//抽象工厂模式
class A
{
public:
     A(std::string nm)
        :mname(nm) {}
     virtual void operation() = 0;
protected:
     std::string mname;
};
class A1 :public A
{
public:
     A1(std::string nm)
        :A(nm)
     {}
     virtual void operation()
     {
         std::cout << "a1" << std::endl;
     }
};
class A2 :public A
{
public:
     A2(std::string nm)
        :A(nm)
     {}
     virtual void operation()
     {
         std::cout << "a2" << std::endl;
     }
};
class B
{
public:
     B(std::string nm)
       :mname(nm) {}
     virtual void operation() = 0;
protected:
     std::string mname;
};
class B1 :public B
{
public:
     B1(std::string nm)
       :B(nm)
     {}
     virtual void operation()
     {
         std::cout << "b1" << std::endl;
     }
};
class B2 :public B
{
public:
     B2(std::string nm)
       :B(nm)
     {}
     virtual void operation()
     {
          std::cout << "b2" << std::endl;
      }
};
class AbstactFactory
{
public:
     AbstactFactory(std::string nm)
        :mname(nm)
     {}
     virtual A*createA() = 0;
     virtual B*createB() = 0;
protected:
     std::string mname;
};
//一号工厂产A产品  需要A1 B1
class Factory1 :public AbstactFactory
{
public:
     Factory1(std::string name)
         :AbstactFactory(name)
     {}
     virtual A* createA()
     {
          return new A1("a1");
     }
     virtual B* createB()
     {
          return new B1("b1");
     }
};
//二号工厂产B产品  需要A2 B2
class Factory2 :public AbstactFactory
{
public:
     Factory2(std::string name)
         :AbstactFactory(name)
     {}
     virtual A* createA()
     {
         return new A2("a2");
     }
     virtual B* createB()
     {
         return new B2("b2");
     }
};
int main()
{
     //建工厂去生产A产品,需要a1 b1
     AbstactFactory* paf = new Factory1("f1");
     A* pa = paf->createA();
     B* pb = paf->createB();

     pa->operation();
     pb->operation();

     return 0;
}

结果如下:
在这里插入图片描述

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值