zoukankan      html  css  js  c++  java
  • 简单工厂模式(转)

    工厂模式有一种非常形象的描述,建立对象的类就如一个工厂,而需要被建立的对象就是一个个产品;在工厂中加工产品,使用产品的人,不用在乎产品是如何生产出来的。从软件开发的角度来说,这样就有效的降低了模块之间的耦合。

    UML类图

    对于工厂模式,具体上可以分为三类:

    1.简单工厂模式Simple Factory;

    2.工厂方法模式;

    3.抽象工厂模式。

    对于上面的三种工厂模式,从上到下逐步抽象,并且更具一般性。而这篇博文主要讲的是简单工厂模式,后两种会在之后的博文中接着总结。

     ProductA、ProductB和ProductC继承自Product虚拟类,Show方法是不同产品的自描述;Factory依赖于ProductA、ProductB和ProductC,Factory根据不同的条件创建不同的Product对象。

    适用场合

    1.在程序中,需要创建的对象很多,导致对象的new操作多且杂时,需要使用简单工厂模式; 2.由于对象的创建过程是我们不需要去关心的,而我们注重的是对象的实际操作,所以,我们需要分离对象的创建和操作两部分,如此,方便后期的程序扩展和维护。

    代码实现:

    /*
    ** FileName   : SimpleFactoryPatternDemo
    ** Author    : Jelly Young
    ** Date     : 2013/11/17
    ** Description : More information
    */
    
    #include <iostream>
    #include <vector>
    using namespace std;
    
    typedef enum ProductTypeTag
    {
      TypeA,
      TypeB,
      TypeC
    }PRODUCTTYPE;
    
    // Here is the product class
    class Product
    {
    public:
      virtual void Show() = 0;
    };
    
    class ProductA : public Product
    {
    public:
      void Show()
      {
        cout<<"I'm ProductA"<<endl;
      }
    };
    
    class ProductB : public Product
    {
    public:
      void Show()
      {
        cout<<"I'm ProductB"<<endl;
      }
    };
    
    class ProductC : public Product
    {
    public:
      void Show()
      {
        cout<<"I'm ProductC"<<endl;
      }
    };
    
    // Here is the Factory class
    class Factory
    {
    public:
      Product* CreateProduct(PRODUCTTYPE type)
      {
        switch (type)
        {
        case TypeA:
          return new ProductA();
    
        case TypeB:
          return new ProductB();
    
        case TypeC:
          return new ProductC();
    
        default:
          return NULL;
        }
      }
    };
    
    int main(int argc, char *argv[])
    {
      // First, create a factory object
      Factory *factory = new Factory();
      Product *productA = factory->CreateProduct(TypeA);
      if (productA != NULL)
        productA->Show();
    
      Product *productB = factory->CreateProduct(TypeB);
      if (productB != NULL)
        productB->Show();
    
      Product *productC = factory->CreateProduct(TypeC);
      if (productC != NULL)
        productC->Show();
    
      delete factory;
      factory = NULL;
    
      delete productA;
      productA = NULL;
    
      delete productB;
      productB = NULL;    
    
      delete productC;
      productC = NULL;
    
      return 0;
    }

    原文转自 http://www.jb51.net/article/55858.htm

  • 相关阅读:
    leetcode 29-> Divide Two Integers without using multiplication, division and mod operator
    ros topic 发布一次可能会接收不到数据
    python中的print()、str()和repr()的区别
    python 部分函数
    uiautomatorviewer错误 unable toconnect to adb
    pyqt 不规则形状窗口显示
    appium 计算器demo
    Spring 3.0 注解注入详解
    Spring Autowire自动装配
    restful 学习地址
  • 原文地址:https://www.cnblogs.com/happykoukou/p/5381903.html
Copyright © 2011-2022 走看看