zoukankan      html  css  js  c++  java
  • 二、工厂方法(Factory Method) 模式

    工厂方法模式是类的创建模式,又叫虚拟构造子(Virtual Constructor)模式或者多态性工厂(Polymorphic Factory)模式。

    该模式中,核心的工厂类不再是负责所有的产品创建,而是将具体创建的工作交给具体子类去做。

    即提供一个抽象的接口工厂类。针对不同的产品从抽样的接口工厂类实现对应的产品工厂类。工厂类和产品类一一对应。

    工厂方法模式添加新的产品,只需要对应的向系统中增加一个对应产品的工厂类即可。因此其实完全支持“开闭”原则的。

    一个工厂方法的示例如下:

    fruit的代码如下:

       1: // fruit.h
       2: #ifndef FRUIT_H_
       3: #define FRUIT_H_
       4: class Fruit {
       5:   public:
       6:     virtual ~Fruit();
       7:     virtual void Name() = 0;
       8:   protected:
       9:     Fruit();
      10:   private:
      11: };
      12:  
      13: class Apple : public Fruit {
      14:   public:
      15:     virtual void Name();
      16: };
      17:  
      18: class Pear : public Fruit {
      19:   public:
      20:     virtual void Name();
      21: };
      22:  
      23: class Orange : public Fruit {
      24:   public:
      25:     virtual void Name();
      26: };
      27: #endif  // FRUIT_H_
      28:  
      29: //fruit.cc
      30: #include <iostream>
      31: #include "fruit.h"
      32: using namespace std;
      33:  
      34: Fruit::~Fruit() {
      35: }
      36:  
      37: Fruit::Fruit() {
      38: }
      39:  
      40: void Apple::Name() {
      41:   cout << "This is Apple !" << endl;
      42: }
      43:  
      44: void Pear::Name() {
      45:   cout << "This is Pear !" << endl;
      46: }
      47:  
      48: void Orange::Name() {
      49:   cout << "This is Orange !" << endl;
      50: }

    fruit_factory代码如下:

       1: // fruit_factory.h
       2: #ifndef FRUIT_FACTORY_H_
       3: #define FRUIT_FACTORY_H_
       4: class Fruit;
       5: class FruitFactory {
       6:   public:
       7:     virtual Fruit* GetFruit() = 0;
       8:   private:
       9: };
      10:  
      11: class AppleFactory : public FruitFactory {
      12:   public:
      13:     virtual Fruit* GetFruit();
      14: };
      15:  
      16: class PearFactory : public FruitFactory {
      17:   public:
      18:     virtual Fruit* GetFruit();
      19:   private:
      20: };
      21:  
      22: class OrangeFactory : public FruitFactory {
      23:   public:
      24:     virtual Fruit* GetFruit();
      25:   private:
      26: };
      27: #endif  // FRUIT_FACTORY_H_
      28:  
      29: // fruit_factory.cc
      30: #include "fruit_factory.h"
      31:  
      32: #include <iostream>
      33: #include <stdio.h>
      34: #include "fruit.h"
      35:  
      36: Fruit* AppleFactory::GetFruit() {
      37:   return new Apple();
      38: }
      39:  
      40: Fruit* PearFactory::GetFruit() {
      41:   return new Pear();
      42: }
      43:  
      44: Fruit* OrangeFactory::GetFruit() {
      45:   return new Orange();
      46: }

    client 的代码如下:

       1: #include "fruit.h"
       2: #include "fruit_factory.h"
       3:  
       4: using namespace std;
       5:  
       6: int main(int argc, char** argv) {
       7:   {
       8:     FruitFactory* factory = new AppleFactory();
       9:     Fruit* fruit = factory->GetFruit();
      10:     fruit->Name();
      11:     delete fruit;
      12:     delete factory;
      13:   }
      14:   {
      15:     FruitFactory* factory = new PearFactory();
      16:     Fruit* fruit = factory->GetFruit();
      17:     fruit->Name();
      18:     delete fruit;
      19:     delete factory;
      20:   }
      21:   {
      22:     FruitFactory* factory = new OrangeFactory();
      23:     Fruit* fruit = factory->GetFruit();
      24:     fruit->Name();
      25:     delete fruit;
      26:     delete factory;
      27:   }
      28:   return 0;
      29: }

    makefile文件

       1: # UNIX makefile
       2: CXX = g++
       3: LD  = g++
       4: CXXFLAGS = -g
       5: AR = ar
       6: TARGETLIB = libfactory.a
       7: object = fruit_factory.o fruit.o
       8: main: main.o $(TARGETLIB)
       9:     $(CXX) -o main main.o $(TARGETLIB)
      10: $(TARGETLIB): $(object)
      11:     $(AR) cq $@ $^
      12: main.o: fruit.h fruit_factory.h
      13: fruit_factory.o: fruit.h fruit_factory.h
      14: fruit.o: fruit.h
      15: .PHONY: clean
      16: clean:
      17:     rm -rf *.o *.a main

    运行结果如下:

       1: This is Apple !
       2: This is Pear !
       3: This is Orange !

    对于工厂方法.当需要增加一个水果时,则只需要从Fruit派生出一个具体的水果类.然后从FruitFactory派生出对应水果的工厂类即可.不需要修改原有的代码.

    因此,工厂方法满足“开闭”原则

  • 相关阅读:
    设计模式系列
    Python3 系列之 可变参数和关键字参数
    设计模式系列
    【HANA系列】SAP HANA ODBC error due to mismatch of version
    【FICO系列】SAP FICO FS00修改科目为未清项目管理
    【FIORI系列】SAP OpenUI5 (SAPUI5) js框架简单介绍
    【HANA系列】SAP HANA SQL获取当前日期加若干天后的日期
    【HANA系列】SAP HANA SQL获取本周的周一
    【HANA系列】SAP HANA SQL获取当前日期
    【HANA系列】SAP HANA SQL获取当前日期最后一天
  • 原文地址:https://www.cnblogs.com/lovemdx/p/2841982.html
Copyright © 2011-2022 走看看