File tree 31 files changed +839
-31
lines changed
31 files changed +839
-31
lines changed Original file line number Diff line number Diff line change
1
+ //
2
+ // Created by xiemenghui on 2018/7/20.
3
+ //
4
+
5
+ #include " Factory.h"
6
+ #include " concrete_factory.h"
7
+
8
+ Factory* Factory::CreateFactory (FACTORY_TYPE factory)
9
+ {
10
+ Factory *pFactory = nullptr ;
11
+ switch (factory) {
12
+ case FACTORY_TYPE::BENZ_FACTORY: // 奔驰工厂
13
+ pFactory = new BenzFactory ();
14
+ break ;
15
+ case FACTORY_TYPE::BMW_FACTORY: // 宝马工厂
16
+ pFactory = new BmwFactory ();
17
+ break ;
18
+ case FACTORY_TYPE::AUDI_FACTORY: // 奥迪工厂
19
+ pFactory = new AudiFactory ();
20
+ break ;
21
+ default :
22
+ break ;
23
+ }
24
+ return pFactory;
25
+ }
Original file line number Diff line number Diff line change
1
+ //
2
+ // Created by xiemenghui on 2018/7/20.
3
+ //
4
+
5
+ #ifndef DESIGNPATTERN_FACTORY_H
6
+ #define DESIGNPATTERN_FACTORY_H
7
+
8
+ #include " product.h"
9
+
10
+ // 抽象工厂模式
11
+ class Factory {
12
+ public:
13
+ enum FACTORY_TYPE {
14
+ BENZ_FACTORY, // 奔驰工厂
15
+ BMW_FACTORY, // 宝马工厂
16
+ AUDI_FACTORY // 奥迪工厂
17
+ };
18
+
19
+ virtual ICar* CreateCar () = 0; // 生产汽车
20
+ virtual IBike* CreateBike () = 0; // 生产自行车
21
+ static Factory * CreateFactory (FACTORY_TYPE factory); // 创建工厂
22
+ };
23
+
24
+ #endif // DESIGNPATTERN_FACTORY_H
Original file line number Diff line number Diff line change
1
+ //
2
+ // Created by xiemenghui on 2018/7/20.
3
+ //
4
+
5
+ #include " Factory.h"
6
+ #include " product.h"
7
+ #include " FactoryMain.h"
8
+ #include < iostream>
9
+ using namespace std ;
10
+
11
+ void FactoryMain ()
12
+ {
13
+ // ąźłŰ
14
+ Factory * pFactory = Factory::CreateFactory (Factory::FACTORY_TYPE::BENZ_FACTORY);
15
+ ICar * pCar = pFactory->CreateCar ();
16
+ IBike * pBike = pFactory->CreateBike ();
17
+
18
+ cout << " Benz factory - Car: " << pCar->Name () << endl;
19
+ cout << " Benz factory - Bike: " << pBike->Name () << endl;
20
+
21
+ SAFE_DELETE (pCar);
22
+ SAFE_DELETE (pBike);
23
+ SAFE_DELETE (pFactory);
24
+
25
+ // ąŚÂí
26
+ pFactory = Factory::CreateFactory (Factory::FACTORY_TYPE::BMW_FACTORY);
27
+ pCar = pFactory->CreateCar ();
28
+ pBike = pFactory->CreateBike ();
29
+ cout << " Bmw factory - Car: " << pCar->Name () << endl;
30
+ cout << " Bmw factory - Bike: " << pBike->Name () << endl;
31
+
32
+ SAFE_DELETE (pCar);
33
+ SAFE_DELETE (pBike);
34
+ SAFE_DELETE (pFactory);
35
+
36
+ // °ÂľĎ
37
+ pFactory = Factory::CreateFactory (Factory::FACTORY_TYPE::AUDI_FACTORY);
38
+ pCar = pFactory->CreateCar ();
39
+ pBike = pFactory->CreateBike ();
40
+ cout << " Audi factory - Car: " << pCar->Name () << endl;
41
+ cout << " Audi factory - Bike: " << pBike->Name () << endl;
42
+
43
+ SAFE_DELETE (pCar);
44
+ SAFE_DELETE (pBike);
45
+ SAFE_DELETE (pFactory);
46
+ }
Original file line number Diff line number Diff line change
1
+ //
2
+ // Created by xiemenghui on 2018/7/20.
3
+ //
4
+
5
+ #ifndef DESIGNPATTERN_FACTORYMAIN_H
6
+ #define DESIGNPATTERN_FACTORYMAIN_H
7
+
8
+ #ifndef SAFE_DELETE
9
+ #define SAFE_DELETE (p ) { if(p) {delete(p); (p)=nullptr;}}
10
+ #endif
11
+
12
+ void FactoryMain ();
13
+
14
+ #endif //DESIGNPATTERN_FACTORYMAIN_H
Original file line number Diff line number Diff line change
1
+ //
2
+ // Created by xiemenghui on 2018/7/20.
3
+ //
4
+
5
+ #ifndef DESIGNPATTERN_CONCRETE_FACTORY_H
6
+ #define DESIGNPATTERN_CONCRETE_FACTORY_H
7
+
8
+ #include " Factory.h"
9
+ #include " concrete_product.h"
10
+
11
+ // 奔驰工厂
12
+ class BenzFactory : public Factory
13
+ {
14
+ public:
15
+ ICar* CreateCar ()
16
+ {
17
+ return new BenzCar ();
18
+ }
19
+ IBike* CreateBike ()
20
+ {
21
+ return new BenzBike ();
22
+ }
23
+ };
24
+
25
+ // 宝马工厂
26
+ class BmwFactory : public Factory
27
+ {
28
+ public:
29
+ ICar* CreateCar () {
30
+ return new BmwCar ();
31
+ }
32
+
33
+ IBike* CreateBike () {
34
+ return new BmwBike ();
35
+ }
36
+ };
37
+
38
+ // 奥迪工厂
39
+ class AudiFactory : public Factory
40
+ {
41
+ public:
42
+ ICar* CreateCar () {
43
+ return new AudiCar ();
44
+ }
45
+
46
+ IBike* CreateBike () {
47
+ return new AudiBike ();
48
+ }
49
+ };
50
+
51
+ #endif // DESIGNPATTERN_CONCRETE_FACTORY_H
Original file line number Diff line number Diff line change
1
+ //
2
+ // Created by xiemenghui on 2018/7/20.
3
+ //
4
+
5
+ #ifndef DESIGNPATTERN_CONCRETE_PRODUCT_H
6
+ #define DESIGNPATTERN_CONCRETE_PRODUCT_H
7
+
8
+ #include " product.h"
9
+
10
+ /* ********* 汽车 **********/
11
+ // 奔驰
12
+ class BenzCar : public ICar
13
+ {
14
+ public:
15
+ string Name ()
16
+ {
17
+ return " Benz Car" ;
18
+ }
19
+ };
20
+
21
+ // 宝马
22
+ class BmwCar : public ICar
23
+ {
24
+ public:
25
+ string Name ()
26
+ {
27
+ return " Bmw Car" ;
28
+ }
29
+ };
30
+
31
+ // 奥迪
32
+ class AudiCar : public ICar
33
+ {
34
+ public:
35
+ string Name ()
36
+ {
37
+ return " Audi Car" ;
38
+ }
39
+ };
40
+
41
+ /* ********* 自行车 **********/
42
+ // 奔驰
43
+ class BenzBike : public IBike
44
+ {
45
+ public:
46
+ string Name ()
47
+ {
48
+ return " Benz Bike" ;
49
+ }
50
+ };
51
+
52
+ // 宝马
53
+ class BmwBike : public IBike
54
+ {
55
+ public:
56
+ string Name ()
57
+ {
58
+ return " Bmw Bike" ;
59
+ }
60
+ };
61
+
62
+ // 奥迪
63
+ class AudiBike : public IBike
64
+ {
65
+ public:
66
+ string Name ()
67
+ {
68
+ return " Audi Bike" ;
69
+ }
70
+ };
71
+
72
+ #endif // DESIGNPATTERN_CONCRETE_PRODUCT_H
Original file line number Diff line number Diff line change
1
+ //
2
+ // Created by xiemenghui on 2018/7/20.
3
+ //
4
+
5
+ #ifndef DESIGNPATTERN_PRODUCT_H
6
+ #define DESIGNPATTERN_PRODUCT_H
7
+
8
+ #include < string>
9
+ using std::string;
10
+
11
+ // 汽车接口
12
+ class ICar
13
+ {
14
+ public:
15
+ virtual string Name () = 0;
16
+ };
17
+
18
+ // 自行车接口
19
+ class IBike
20
+ {
21
+ public:
22
+ virtual string Name () = 0;
23
+ };
24
+
25
+ #endif // DESIGNPATTERN_PRODUCT_H
Original file line number Diff line number Diff line change
1
+ //
2
+ // Created by xiemenghui on 2018/7/20.
3
+ //
4
+
5
+ #ifndef DESIGNPATTERN_ADAPTERMAIN_H
6
+ #define DESIGNPATTERN_ADAPTERMAIN_H
7
+
8
+ #include "adapter.h"
9
+
10
+ void AdapterMain ()
11
+ {
12
+ // ´´½¨ÊÊÅäÆ÷
13
+ IRussiaSocket * pAdapter = new PowerAdapter ();
14
+
15
+ // ³äµç
16
+ pAdapter -> Charge ();
17
+
18
+ SAFE_DELETE (pAdapter );
19
+ }
20
+
21
+ #endif //DESIGNPATTERN_ADAPTERMAIN_H
Original file line number Diff line number Diff line change
1
+ //
2
+ // Created by xiemenghui on 2018/7/20.
3
+ //
4
+
5
+ #ifndef DESIGNPATTERN_ADAPTEE_H
6
+ #define DESIGNPATTERN_ADAPTEE_H
7
+
8
+ #include < iostream>
9
+
10
+ // 自带的充电器(两脚扁型)
11
+ class OwnCharger
12
+ {
13
+ public:
14
+ void ChargeWithFeetFlat ()
15
+ {
16
+ std::cout << " OwnCharger::ChargeWithFeetFlat\n " ;
17
+ }
18
+ };
19
+
20
+ #endif // DESIGNPATTERN_ADAPTEE_H
Original file line number Diff line number Diff line change
1
+ //
2
+ // Created by xiemenghui on 2018/7/20.
3
+ //
4
+
5
+ #ifndef DESIGNPATTERN_ADAPTER_H
6
+ #define DESIGNPATTERN_ADAPTER_H
7
+
8
+ #include " target.h"
9
+ #include " adaptee.h"
10
+
11
+ #ifndef SAFE_DELETE
12
+ #define SAFE_DELETE (p ) { if (p){delete (p); (p)=NULL ;} }
13
+ #endif
14
+
15
+ // 电源适配器
16
+ class PowerAdapter : public IRussiaSocket
17
+ {
18
+ public:
19
+ PowerAdapter () : m_pCharger(new OwnCharger()){}
20
+ ~PowerAdapter ()
21
+ {
22
+ SAFE_DELETE (m_pCharger);
23
+ }
24
+ void Charge ()
25
+ {
26
+ // 使用自带的充电器(两脚扁形)充电
27
+ m_pCharger->ChargeWithFeetFlat ();
28
+ }
29
+ private:
30
+ // 持有需要被适配的接口对象(自带的充电器)
31
+ OwnCharger* m_pCharger;
32
+ };
33
+
34
+ #endif // DESIGNPATTERN_ADAPTER_H
Original file line number Diff line number Diff line change
1
+ //
2
+ // Created by xiemenghui on 2018/7/20.
3
+ //
4
+
5
+ #ifndef DESIGNPATTERN_TARGET_H
6
+ #define DESIGNPATTERN_TARGET_H
7
+
8
+ // 俄罗斯提供的插座
9
+ class IRussiaSocket
10
+ {
11
+ public:
12
+ // 使用双脚圆形充电(暂不实现)
13
+ virtual void Charge () = 0;
14
+ };
15
+
16
+ #endif // DESIGNPATTERN_TARGET_H
Original file line number Diff line number Diff line change
1
+ //
2
+ // Created by xiemenghui on 2018/7/21.
3
+ //
4
+
5
+ #include " BridgeMain.h"
6
+
7
+ void BridgeMain ()
8
+ {
9
+ // 创建电器(电灯、电风扇)
10
+ IElectricalEquipment * light = new Light ();
11
+ IElectricalEquipment * fan = new Fan ();
12
+
13
+ // 创建开关(拉链式开关、两位开关)
14
+ // 将拉链式开关和电灯关联起来,两位开关和风扇关联起来
15
+ ISwitch * pullChain = new PullChainSwitch (light);
16
+ ISwitch * twoPosition = new TwoPositionSwitch (fan);
17
+
18
+ // 开灯、关灯
19
+ pullChain->On ();
20
+ pullChain->Off ();
21
+
22
+ // 打开风扇、关闭风扇
23
+ twoPosition->On ();
24
+ twoPosition->Off ();
25
+
26
+ SAFE_DELETE (twoPosition);
27
+ SAFE_DELETE (pullChain);
28
+ SAFE_DELETE (fan);
29
+ SAFE_DELETE (light);
30
+ }
You can’t perform that action at this time.
0 commit comments