1、有一個工廠,專門生產不同品牌的汽車。當有人需要從此工廠提貨的時候,只需要告訴他,要什麼品牌的,就可以了,並不關心這些車是怎麼生產出來的。 2、以上方式,如果增加品牌的時候,也要修改工廠,有點麻煩。於是,把工廠也抽象了。 1的類圖與實現: 首先,是通用的車 然後是不同品牌的車,繼承自Car 接著, ...
1、有一個工廠,專門生產不同品牌的汽車。當有人需要從此工廠提貨的時候,只需要告訴他,要什麼品牌的,就可以了,並不關心這些車是怎麼生產出來的。
2、以上方式,如果增加品牌的時候,也要修改工廠,有點麻煩。於是,把工廠也抽象了。
1的類圖與實現:
首先,是通用的車
1 /// 2 /// @file Car.h 3 /// @author marrs([email protected]) 4 /// @date 2017-08-12 20:10:31 5 /// 6 7 #ifndef __CAR_H__ 8 #define __CAR_H__ 9 10 #include <iostream> 11 12 namespace marrs{ 13 14 using std::cout; 15 using std::cerr; 16 using std::endl; 17 18 class Car 19 { 20 public: 21 Car() : b_IsRunning(0){} 22 virtual ~Car(){}; 23 public: 24 virtual void Run() = 0; 25 virtual void Stop() = 0; 26 protected: 27 bool b_IsRunning; 28 }; 29 30 } 31 32 #endif //__CAR_H__
然後是不同品牌的車,繼承自Car
1 /// 2 /// @file Benz.h 3 /// @author marrs([email protected]) 4 /// @date 2017-08-12 20:20:54 5 /// 6 7 #ifndef __BENZ_H__ 8 #define __BENZ_H__ 9 10 #include "Car.h" 11 12 namespace marrs{ 13 14 class Benz 15 : public Car 16 { 17 public: 18 ~Benz(){} 19 public: 20 void Run(); 21 void Stop(); 22 }; 23 24 } 25 26 #endif //__BENZ_H__
1 /// 2 /// @file Benz.cc 3 /// @author marrs([email protected]) 4 /// @date 2017-08-12 20:21:54 5 /// 6 7 #include "Benz.h" 8 9 namespace marrs{ 10 11 void Benz::Run() 12 { 13 if (b_IsRunning) 14 { 15 cerr << "Benz is running!" << endl; 16 } 17 18 cout << "Benz is going to running!" << endl; 19 b_IsRunning = true; 20 } 21 22 void Benz::Stop() 23 { 24 if (!b_IsRunning) 25 { 26 cerr << "Benz isn't running..." << endl; 27 } 28 29 cout << "Benz is going to stopping!" << endl; 30 b_IsRunning = false; 31 } 32 33 }
1 /// 2 /// @file Audi.h 3 /// @author marrs([email protected]) 4 /// @date 2017-08-12 20:20:54 5 /// 6 7 #ifndef __AUDI_H__ 8 #define __AUDI_H__ 9 10 #include "Car.h" 11 12 namespace marrs{ 13 14 class Audi 15 : public Car 16 { 17 public: 18 ~Audi(){} 19 public: 20 void Run(); 21 void Stop(); 22 }; 23 24 } 25 26 #endif//__AUDI_H__
1 /// 2 /// @file Audi.cc 3 /// @author marrs([email protected]) 4 /// @date 2017-08-12 20:21:54 5 /// 6 7 #include "Audi.h" 8 9 namespace marrs{ 10 11 void Audi::Run() 12 { 13 if (b_IsRunning) 14 { 15 cerr << "Audi is running!" << endl; 16 } 17 18 cout << "Audi is going to running!" << endl; 19 b_IsRunning = true; 20 } 21 22 void Audi::Stop() 23 { 24 if (!b_IsRunning) 25 { 26 cerr << "Audi isn't running..." << endl; 27 } 28 29 cout << "Audi is going to stopping!" << endl; 30 b_IsRunning = false; 31 } 32 33 }
1 /// 2 /// @file Lamborghini.h 3 /// @author marrs([email protected]) 4 /// @date 2017-08-12 20:20:54 5 /// 6 7 #ifndef __LAMBORGHINI_H__ 8 #define __LAMBORGHINI_H__ 9 10 #include "Car.h" 11 12 namespace marrs{ 13 14 class Lamborghini 15 : public Car 16 { 17 public: 18 ~Lamborghini(){} 19 public: 20 void Run(); 21 void Stop(); 22 }; 23 24 } 25 26 #endif//__LAMBORGHINI_H__
1 /// 2 /// @file Lamborghini.cc 3 /// @author marrs([email protected]) 4 /// @date 2017-08-12 20:21:54 5 /// 6 7 #include "Lamborghini.h" 8 9 namespace marrs{ 10 11 void Lamborghini::Run() 12 { 13 if (b_IsRunning) 14 { 15 cerr << "Lamborghini is running!" << endl; 16 } 17 18 cout << "Lamborghini is going to running!" << endl; 19 b_IsRunning = true; 20 } 21 22 void Lamborghini::Stop() 23 { 24 if (!b_IsRunning) 25 { 26 cerr << "Lamborghini isn't running..." << endl; 27 } 28 29 cout << "Lamborghini is going to stopping!" << endl; 30 b_IsRunning = false; 31 } 32 33 }
接著,有個生產工廠
1 /// 2 /// @file Factory.h 3 /// @author marrs([email protected]) 4 /// @date 2017-08-12 20:27:42 5 /// 6 7 #ifndef __FACTORY_H__ 8 #define __FACTORY_H__ 9 10 11 #include "Benz.h" 12 #include "Audi.h" 13 #include "Lamborghini.h" 14 15 16 enum Brand 17 { 18 EN_BRAND_CAR_BANZ = 0, 19 EN_BRAND_CAR_AUDI, 20 EN_BRAND_CAR_LAMBORGHINI, 21 }; 22 23 24 namespace marrs{ 25 26 using std::cout; 27 using std::endl; 28 29 class Factory 30 { 31 public: 32 Car * Produce(int int_brand); 33 void Reclaim(Car * car_brand); 34 }; 35 36 } 37 38 39 #endif //__FACTORY_H__
1 /// 2 /// @file Factory.cc 3 /// @author marrs([email protected]) 4 /// @date 2017-08-12 20:39:05 5 /// 6 7 #include "Factory.h" 8 9 namespace marrs{ 10 11 Car * Factory::Produce(int int_brand) 12 { 13 switch(int_brand) 14 { 15 case EN_BRAND_CAR_BANZ: 16 return new Benz; 17 case EN_BRAND_CAR_AUDI: 18 return new Audi; 19 case EN_BRAND_CAR_LAMBORGHINI: 20 return new Lamborghini; 21 default:break; 22 } 23 return NULL; 24 } 25 26 void Factory::Reclaim(Car * car_brand) 27 { 28 delete car_brand; 29 } 30 31 }
為了方便統一處理方式,我把車的銷毀也放到工廠類里了。
1 /// 2 /// @file main.cc 3 /// @author marrs([email protected]) 4 /// @date 2017-08-12 20:40:59 5 /// 6 7 #include "Factory.h" 8 9 using namespace marrs; 10 11 int main() 12 { 13 Factory factory; 14 15 Car * car_first = factory.Produce(EN_BRAND_CAR_BANZ); 16 car_first->Run(); 17 car_first->Stop(); 18 factory.Reclaim(car_first); 19 20 Car * car_second = factory.Produce(EN_BRAND_CAR_AUDI); 21 car_second->Run(); 22 car_second->Stop(); 23 factory.Reclaim(car_second); 24 25 Car * car_third = factory.Produce(EN_BRAND_CAR_LAMBORGHINI); 26 car_third->Run(); 27 car_third->Stop(); 28 factory.Reclaim(car_third); 29 30 }
編譯,運行
[ccx@ubuntu ~/object-oriented/Factory-Pattern]$>g++ * -o car_factory.exe [ccx@ubuntu ~/object-oriented/Factory-Pattern]$>./car_factory.exe Benz is going to running! Benz is going to stopping! Audi is going to running! Audi is going to stopping! Lamborghini is going to running! Lamborghini is going to stopping!
2的類圖與實現 (畫圖功底不行....略亂)
在1的基礎之上,修改Factory
1 /// 2 /// @file Factory.h 3 /// @author marrs([email protected]) 4 /// @date 2017-08-12 20:27:42 5 /// 6 7 #ifndef __FACTORY_H__ 8 #define __FACTORY_H__ 9 10 11 #include "Car.h" 12 13 namespace marrs{ 14 15 using std::cout; 16 using std::endl; 17 18 class Factory 19 { 20 public: 21 virtual ~Factory(){} 22 public: 23 virtual Car * Produce() = 0; 24 void Reclaim(Car * car_brand) 25 { 26 delete car_brand; 27 } 28 }; 29 30 } 31 32 33 #endif //__FACTORY_H__
然後是不同的工廠
1 /// 2 /// @file Benz_Factory.h 3 /// @author marrs([email protected]) 4 /// @date 2017-08-12 21:21:58 5 /// 6 7 #ifndef __BENZ_FACTORY_H__ 8 #define __BENZ_FACTORY_H__ 9 10 #include "Factory.h" 11 #include "Benz.h" 12 13 namespace marrs{ 14 15 class BenzFactory 16 : public Factory 17 { 18 public: 19 Car * Produce() 20 { 21 return new Benz; 22 } 23 }; 24 25 } 26 27 #endif // __BENZ_FACTORY_H__
1 /// 2 /// @file Audi_Factory.h 3 /// @author marrs([email protected]) 4 /// @date 2017-08-12 21:21:58 5 /// 6 7 #ifndef __AUDI_FACTORY_H__ 8 #define __AUDI_FACTORY_H__ 9 10 #include "Factory.h" 11 #include "Audi.h" 12 13 namespace marrs{ 14 15 class AudiFactory 16 : public Factory 17 { 18 public: 19 Car * Produce() 20 { 21 return new Audi; 22 } 23 }; 24 25 } 26 27 #endif // __AUDI_FACTORY_H__
1 /// 2 /// @file Lamborghini_Factory.h 3 /// @author marrs([email protected]) 4 /// @date 2017-08-12 21:21:58 5 /// 6 7 #ifndef __LAMBORGHINI_FACTORY_H__ 8 #define __LAMBORGHINI_FACTORY_H__ 9 10 #include "Factory.h" 11 #include "Lamborghini.h" 12 13 namespace marrs{ 14 15 class LamborghiniFactory 16 : public Factory 17 { 18 public: 19 Car * Produce() 20 { 21 return new Lamborghini; 22 } 23 }; 24 25 } 26 27 #endif // __LAMBORGHINI_FACTORY_H__
最後修改main.cc
1 /// 2 /// @file main.cc 3 /// @author marrs([email protected]) 4 /// @date 2017-08-12 20:40:59 5 /// 6 7 #include "Benz_Factory.h" 8 #include "Audi_Factory.h" 9 #include "Lamborghini_Factory.h" 10 11 using namespace marrs; 12 13 void BenzAction() 14 { 15 Factory * factory = new BenzFactory; 16 Car * car_first = factory->Produce(); 17 car_first->Run(); 18 car_first->Stop(); 19 factory->Reclaim(car_first); 20 delete factory; 21 } 22 23 void AudiAction() 24 { 25 Factory * factory = new AudiFactory; 26 Car * car_first = factory->Produce(); 27 car_first->Run(); 28 car_first->Stop(); 29 factory->Reclaim(car_first); 30 delete factory; 31 } 32 33 void LamborghiniAction() 34 { 35 Factory * factory = new LamborghiniFactory; 36 Car * car_first = factory->Produce(); 37 car_first->Run(); 38 car_first->Stop(); 39 factory->Reclaim(car_first); 40 delete factory; 41 } 42 43 44 int main() 45 { 46 BenzAction(); 47 AudiAction(); 48 LamborghiniAction(); 49 50 return 0; 51 }
編譯,運行
[ccx@ubuntu ~/object-oriented/Factory-Pattern_2]$>g++ * -o car_Factory.exe [ccx@ubuntu ~/object-oriented/Factory-Pattern_2]$>./car_Factory.exe Benz is going to running! Benz is going to stopping! Audi is going to running! Audi is going to stopping! Lamborghini is going to running! Lamborghini is going to stopping!