• 一、简单工厂(Simple Factory)模式


    简单工厂模式是类的创建模式,又叫做静态工厂方法模式(Static Factory Method)模式。由工厂对象决定创建出哪一种产品类的实例。

    对简单工厂来说,增加一个新产品是一个痛苦的过程。工厂角色需要知道每一种产品,如何创建他们,以及何时向客户端提供他们。即增加新的产品

    意味着需要修改这个工厂角色的源代码。

    一个使用简单工厂的代码示例如下:

       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: }
      51:  
      52: // fruit_factory.h
      53: #ifndef FRUIT_FACTORY_H_
      54: #define FRUIT_FACTORY_H_
      55: #include <string>
      56: class Fruit;
      57: class FruitFactory {
      58:   public:
      59:     static Fruit* GetFruit(const std::string& name);
      60:   private:
      61: };
      62: #endif  // FRUIT_FACTORY_H_
      63:  
      64: // fruit_factory.cc
      65: #include "fruit_factory.h"
      66:  
      67: #include <iostream>
      68: #include <stdio.h>
      69: #include "fruit.h"
      70:  
      71: Fruit* FruitFactory::GetFruit(const std::string& name) {
      72:   if (name.compare("apple") == 0) {
      73:     return new Apple();
      74:   } else if (name.compare("pear") == 0) {
      75:     return new Pear();
      76:   } else if (name.compare("orange") == 0) {
      77:     return new Orange();
      78:   } else {
      79:     fprintf(stderr, "Invalid name %s\n", name.c_str());
      80:     return NULL;
      81:   }
      82: }
      83:  
      84: // main.cc
      85: #include "fruit.h"
      86: #include "fruit_factory.h"
      87:  
      88: using namespace std;
      89:  
      90: void Test(const char* name) {
      91:   Fruit* fruit = FruitFactory::GetFruit(name);
      92:   if (fruit != NULL) {
      93:     fruit->Name();
      94:     delete fruit;
      95:   }
      96: }
      97: int main(int argc, char** argv) {
      98:   Test("apple");
      99:   Test("pear");
     100:   Test("orange");
     101:   Test("other");
     102:   return 0;
     103: }
       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

    从代码实现上也可以看出,当增加具体的Fruit类是,则需要修改FruitFactory的GetFruit方法,在其中增加相应的分支代码来返回具体的类。

  • 相关阅读:
    js获取Session问题 dodo
    复制一个datatable的指定行到另外一个datatable dodo
    sqlserver数据库备份与还原语句 dodo
    net软件测试实战技术大全 dodo
    AJAX 浏览器支持 dodo
    使用 vs2005进行负载测试 dodo
    sql使用in批量删除 dodo
    各种浏览器兼容存在的方法:Xenocode Browser Sandbox dodo
    C#调用Windows API函数 dodo
    ewebeditor在ie8下报错 dodo
  • 原文地址:https://www.cnblogs.com/lovemdx/p/2841923.html
Copyright © 2020-2023  润新知