设计模式学习——工厂模式(Factory Pattern)

时间:2021-10-19 18:44:22

1、有一个工厂,专门生产不同品牌的汽车。当有人需要从此工厂提货的时候,只需要告诉他,要什么品牌的,就可以了,并不关心这些车是怎么生产出来的。

2、以上方式,如果增加品牌的时候,也要修改工厂,有点麻烦。于是,把工厂也抽象了。

 

1的类图与实现:

设计模式学习——工厂模式(Factory Pattern)

 

 首先,是通用的车

 1  ///
2 /// @file Car.h
3 /// @author marrs(chenchengxi993@gmail.com)
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(chenchengxi993@gmail.com)
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(chenchengxi993@gmail.com)
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(chenchengxi993@gmail.com)
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(chenchengxi993@gmail.com)
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(chenchengxi993@gmail.com)
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(chenchengxi993@gmail.com)
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(chenchengxi993@gmail.com)
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(chenchengxi993@gmail.com)
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(chenchengxi993@gmail.com)
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的类图与实现 (画图功底不行....略乱)

设计模式学习——工厂模式(Factory Pattern)

 

在1的基础之上,修改Factory

 1  ///
2 /// @file Factory.h
3 /// @author marrs(chenchengxi993@gmail.com)
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(chenchengxi993@gmail.com)
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(chenchengxi993@gmail.com)
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(chenchengxi993@gmail.com)
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(chenchengxi993@gmail.com)
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
!