Chinaunix首页 | 论坛 | 博客
  • 博客访问: 56844
  • 博文数量: 16
  • 博客积分: 306
  • 博客等级: 二等列兵
  • 技术积分: 162
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-20 15:08
文章分类

全部博文(16)

文章存档

2013年(1)

2012年(15)

我的朋友

分类: C/C++

2012-12-04 20:41:20

 Factory 模式的两个最重要的功能:
1)定义创建对象的接口,封装了对象的创建;
2)使得具体化类的工作延迟到了子类中。

点击(此处)折叠或打开

  1. /* author: FiveWei */
  2. /* time:2012.12.04 */
  3. /* filename:Car.h */


  4. #ifndef _CAR_H
  5. #define _CAR_H


  6. #include <cstdio>

  7. class Car
  8. {
  9. public:
  10.      virtual void info() = 0 ;
  11. } ;

  12. class Ford : public Car
  13. {
  14. public:
  15.      virtual void info()
  16.      {
  17.           printf( "Ford\n" ) ;
  18.      }
  19. } ;

  20. class Toyota : public Car
  21. {
  22. public:
  23.      virtual void info()
  24.      {
  25.           printf( "Toyota\n" ) ;
  26.      }
  27. } ;

  28. #endif

点击(此处)折叠或打开

  1. /* author: FiveWei */
  2. /* time:2012.12.04 */
  3. /* filename:CarFactory.h */

  4. #ifndef _CARFACTORY_H
  5. #define _CARFACTORY_H

  6. #include "Car.h"

  7. class CarFactory
  8. {
  9. public:
  10.      CarFactory() ;
  11.      Car* requestCar() ;
  12.      int getCarNums() const ;

  13. protected:
  14.      virtual Car* createCar() = 0 ;

  15. private:
  16.      int CarNums ;
  17. } ;

  18. class FordFactory : public CarFactory
  19. {
  20. protected:
  21.      virtual Car* createCar() ;
  22. } ;

  23. class ToyotaFactory : public CarFactory
  24. {
  25. protected :
  26.      virtual Car* createCar() ;
  27. } ;

  28. #endif

点击(此处)折叠或打开

  1. /* author: FiveWei */
  2. /* time:2012.12.04 */
  3. /* filename:CarFactory.cpp */


  4. #ifndef _CARFACTORY_CPP
  5. #define _CARfACTORY_CPP


  6. #include "CarFactory.h"

  7. #include <cstdio>

  8. CarFactory::CarFactory() : CarNums(0) {}

  9. Car* CarFactory::requestCar() {
  10.     CarNums ++ ;
  11.     return createCar() ;
  12. }

  13. int CarFactory::getCarNums() const
  14. {
  15.     return CarNums ;
  16. }

  17. Car* FordFactory::createCar()
  18. {
  19.     return new Ford() ;
  20. }

  21. Car* ToyotaFactory::createCar()
  22. {
  23.     return new Toyota() ;
  24. }

  25. #endif

点击(此处)折叠或打开

  1. /* author: FiveWei */
  2. /* time:2012.12.04 */
  3. /* filename:TestFactory.cpp */

  4. #include <vector>
  5. #include "CarFactory.h"

  6. #include <cstdio>
  7. using namespace std ;

  8. CarFactory* getBest( const vector<CarFactory*>& inFacs ) //找到最闲的工厂
  9. {
  10.     size_t Size = inFacs.size() ;
  11.     if ( Size == 0 ) return NULL ;

  12.     CarFactory* best = inFacs[0] ;
  13.     for ( size_t i = 0 ; i < Size ; i++ )
  14.     {
  15.         if ( best->getCarNums() > inFacs[i]->getCarNums() )
  16.             best = inFacs[i] ;
  17.     }
  18.     return best ;
  19. }

  20. int main()
  21. {
  22.     vector<CarFactory*> factories ;

  23.     FordFactory* factory1 = new FordFactory() ;
  24.     FordFactory* factory2 = new FordFactory() ;
  25.     FordFactory* factory3 = new FordFactory() ;
  26.     ToyotaFactory* factory4 = new ToyotaFactory() ;

  27.     factories.push_back( factory1 ) ;
  28.     factories.push_back( factory2 ) ;
  29.     factories.push_back( factory3 ) ;
  30.     factories.push_back( factory4 ) ;

  31.     for ( int i = 0 ; i < 10 ; i++ )
  32.     {
  33.         // request the least busy factory from the 4 factories
  34.         CarFactory* CurFac = getBest( factories ) ;
  35.         Car* theCar = CurFac->requestCar() ;
  36.         theCar->info() ;
  37.     }

  38.     return 0 ;
  39. }

阅读(892) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~