DreamWorks

Never say Never。
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

C++设计模式之工厂模式

Posted on 2013-07-30 00:22  _Babyface  阅读(209)  评论(0编辑  收藏  举报

代码在VC++ 6.0通过编译

 1 //InitilizeFactory.h
 2 #ifndef __INITILIZEFACTORY__
 3 #define __INITILIZEFACTORY__
 4 
 5 #include <iostream>
 6 using namespace std;
 7 
 8 class Car{
 9 public:
10     virtual void info()=0;
11 };
12 
13 
14 
15 #include <iostream>
16 
17 class Toyota:public Car{
18 public:
19     virtual void info(){
20         cout<<"This is Toyota car"<<endl;
21     }
22 };
23 
24 class Ford: public Car{
25 public:
26     virtual void info(){
27         cout<<"This is Ford car"<<endl;
28     }
29 };
30 
31 
32 #endif
View Code
 1 //FactoryHeader.h
 2 #ifndef __FACTORYHEADER__
 3 #define __FACTORYHEADER__
 4 
 5 #include "InitilizeFactory.h"
 6 
 7 class CarFactory{
 8 public:
 9     CarFactory();
10     virtual ~CarFactory();
11     Car* requestCar();
12     int getNumCarsInProduct() const;
13 protected:
14     virtual Car* createCar() = 0;
15 private:
16     int mNumCarsInproduct;
17 };
18 
19 class ToyotaFactory : public CarFactory{
20 public:
21 private:
22     virtual Car* createCar();
23 };
24 
25 class FordFactory : public CarFactory{
26 public:
27 private:
28     virtual Car* createCar();
29 };
30 
31 
32 #endif
View Code
 1 //FactoryHeader.cpp
 2 #include "FactoryHeader.h"
 3 #include <vector>
 4 
 5 CarFactory::CarFactory():mNumCarsInproduct(0)
 6 {
 7 }
 8 
 9 CarFactory::~CarFactory()
10 {
11     mNumCarsInproduct = 0;
12 }
13 
14 Car* CarFactory::requestCar()
15 {
16     mNumCarsInproduct++;
17     return createCar();
18 }
19 
20 int CarFactory::getNumCarsInProduct() const
21 {
22     return mNumCarsInproduct;
23 }
24 
25 Car* ToyotaFactory::createCar()
26 {
27     return new Toyota();
28 }
29 
30 Car* FordFactory::createCar()
31 {
32     return new Ford();
33 }
34 
35 CarFactory* getLeastBusyFactory(const vector<CarFactory*>& inFactories)
36 {
37     if(inFactories.size() == 0)
38         return NULL;
39     CarFactory* bestSoFar = inFactories[0];
40     for(size_t i=1;i<inFactories.size();i++)
41     {
42         if (inFactories[i]->getNumCarsInProduct() < bestSoFar->getNumCarsInProduct())
43         {
44             bestSoFar = inFactories[i];
45         }
46     }
47     return bestSoFar;
48 }
49 
50 
51 int main(int argc,char** argv)
52 {
53     vector<CarFactory*> factories;
54     
55     FordFactory* ff1 = new FordFactory();
56     FordFactory* ff2 = new FordFactory();
57     ToyotaFactory* tf1 = new ToyotaFactory();
58     FordFactory* ff3 = new FordFactory();
59     ToyotaFactory* tf2 = new ToyotaFactory();
60 
61     ff1->requestCar();
62     ff2->requestCar();
63     tf1->requestCar();
64     ff3->requestCar();
65     tf2->requestCar();
66 
67     factories.push_back(ff1);
68     factories.push_back(ff2);
69     factories.push_back(tf1);
70     factories.push_back(ff3);
71     factories.push_back(tf2);
72 
73     for (int i=0;i<10;i++)
74     {
75         CarFactory* currentFactory = getLeastBusyFactory(factories);
76         Car* theCar = currentFactory->requestCar();
77         theCar->info();
78     }
79 
80     return 0;
81 }
View Code