File tree 21 files changed +78
-78
lines changed
21 files changed +78
-78
lines changed Original file line number Diff line number Diff line change @@ -9,13 +9,13 @@ Factory* Factory::CreateFactory(FACTORY_TYPE factory)
9
9
{
10
10
Factory *pFactory = nullptr ;
11
11
switch (factory) {
12
- case FACTORY_TYPE::BENZ_FACTORY: // 奔驰工厂
12
+ case FACTORY_TYPE::BENZ_FACTORY: // Benz factory
13
13
pFactory = new BenzFactory ();
14
14
break ;
15
- case FACTORY_TYPE::BMW_FACTORY: // 宝马工厂
15
+ case FACTORY_TYPE::BMW_FACTORY: // BMW factory
16
16
pFactory = new BmwFactory ();
17
17
break ;
18
- case FACTORY_TYPE::AUDI_FACTORY: // 奥迪工厂
18
+ case FACTORY_TYPE::AUDI_FACTORY: // Audi factory
19
19
pFactory = new AudiFactory ();
20
20
break ;
21
21
default :
Original file line number Diff line number Diff line change 7
7
8
8
#include " product.h"
9
9
10
- // 抽象工厂模式
10
+ // Abstract factory pattern
11
11
class Factory {
12
12
public:
13
13
enum FACTORY_TYPE {
14
- BENZ_FACTORY, // 奔驰工厂
15
- BMW_FACTORY, // 宝马工厂
16
- AUDI_FACTORY // 奥迪工厂
14
+ BENZ_FACTORY, // Benz factory
15
+ BMW_FACTORY, // BMW factory
16
+ AUDI_FACTORY // Audi factory
17
17
};
18
18
19
- virtual ICar* CreateCar () = 0; // 生产汽车
20
- virtual IBike* CreateBike () = 0; // 生产自行车
21
- static Factory * CreateFactory (FACTORY_TYPE factory); // 创建工厂
19
+ virtual ICar* CreateCar () = 0; // Production car
20
+ virtual IBike* CreateBike () = 0; // Production bicycle
21
+ static Factory * CreateFactory (FACTORY_TYPE factory); // Create factory
22
22
};
23
23
24
24
#endif // DESIGNPATTERN_FACTORY_H
Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ using namespace std;
10
10
11
11
void FactoryMain ()
12
12
{
13
- // ąźłŰ
13
+ // Benz
14
14
Factory * pFactory = Factory::CreateFactory (Factory::FACTORY_TYPE::BENZ_FACTORY);
15
15
ICar * pCar = pFactory->CreateCar ();
16
16
IBike * pBike = pFactory->CreateBike ();
@@ -22,7 +22,7 @@ void FactoryMain()
22
22
SAFE_DELETE (pBike);
23
23
SAFE_DELETE (pFactory);
24
24
25
- // ąŚÂí
25
+ // BMW
26
26
pFactory = Factory::CreateFactory (Factory::FACTORY_TYPE::BMW_FACTORY);
27
27
pCar = pFactory->CreateCar ();
28
28
pBike = pFactory->CreateBike ();
@@ -33,7 +33,7 @@ void FactoryMain()
33
33
SAFE_DELETE (pBike);
34
34
SAFE_DELETE (pFactory);
35
35
36
- // °ÂľĎ
36
+ // Audi
37
37
pFactory = Factory::CreateFactory (Factory::FACTORY_TYPE::AUDI_FACTORY);
38
38
pCar = pFactory->CreateCar ();
39
39
pBike = pFactory->CreateBike ();
Original file line number Diff line number Diff line change 8
8
#include " Factory.h"
9
9
#include " concrete_product.h"
10
10
11
- // 奔驰工厂
11
+ // Benz factory
12
12
class BenzFactory : public Factory
13
13
{
14
14
public:
@@ -22,7 +22,7 @@ class BenzFactory : public Factory
22
22
}
23
23
};
24
24
25
- // 宝马工厂
25
+ // BMW factory
26
26
class BmwFactory : public Factory
27
27
{
28
28
public:
@@ -35,7 +35,7 @@ class BmwFactory : public Factory
35
35
}
36
36
};
37
37
38
- // 奥迪工厂
38
+ // Audi factory
39
39
class AudiFactory : public Factory
40
40
{
41
41
public:
Original file line number Diff line number Diff line change 7
7
8
8
#include " product.h"
9
9
10
- /* ********* 汽车 **********/
11
- // 奔驰
10
+ /* ********* Car **********/
11
+ // Benz
12
12
class BenzCar : public ICar
13
13
{
14
14
public:
@@ -18,7 +18,7 @@ class BenzCar : public ICar
18
18
}
19
19
};
20
20
21
- // 宝马
21
+ // BMW
22
22
class BmwCar : public ICar
23
23
{
24
24
public:
@@ -28,7 +28,7 @@ class BmwCar : public ICar
28
28
}
29
29
};
30
30
31
- // 奥迪
31
+ // Audi
32
32
class AudiCar : public ICar
33
33
{
34
34
public:
@@ -38,8 +38,8 @@ class AudiCar : public ICar
38
38
}
39
39
};
40
40
41
- /* ********* 自行车 **********/
42
- // 奔驰
41
+ /* ********* Bicycle **********/
42
+ // Benz
43
43
class BenzBike : public IBike
44
44
{
45
45
public:
@@ -49,7 +49,7 @@ class BenzBike : public IBike
49
49
}
50
50
};
51
51
52
- // 宝马
52
+ // BMW
53
53
class BmwBike : public IBike
54
54
{
55
55
public:
@@ -59,7 +59,7 @@ class BmwBike : public IBike
59
59
}
60
60
};
61
61
62
- // 奥迪
62
+ // Audi
63
63
class AudiBike : public IBike
64
64
{
65
65
public:
Original file line number Diff line number Diff line change 8
8
#include < string>
9
9
using std::string;
10
10
11
- // 汽车接口
11
+ // Car Interface
12
12
class ICar
13
13
{
14
14
public:
15
15
virtual string Name () = 0;
16
16
};
17
17
18
- // 自行车接口
18
+ // Bike Interface
19
19
class IBike
20
20
{
21
21
public:
Original file line number Diff line number Diff line change 9
9
10
10
void AdapterMain ()
11
11
{
12
- // ´´½¨ÊÊÅäÆ÷
12
+ // Create a power adapter
13
13
IRussiaSocket * pAdapter = new PowerAdapter ();
14
14
15
- // ³äµç
15
+ // Recharge
16
16
pAdapter -> Charge ();
17
17
18
18
SAFE_DELETE (pAdapter );
Original file line number Diff line number Diff line change 7
7
8
8
#include < iostream>
9
9
10
- // 自带的充电器(两脚扁型)
10
+ // Built-in charger (two-leg flat type)
11
11
class OwnCharger
12
12
{
13
13
public:
Original file line number Diff line number Diff line change 12
12
#define SAFE_DELETE (p ) { if (p){delete (p); (p)=NULL ;} }
13
13
#endif
14
14
15
- // 电源适配器
15
+ // Power Adapter
16
16
class PowerAdapter : public IRussiaSocket
17
17
{
18
18
public:
@@ -23,11 +23,11 @@ class PowerAdapter : public IRussiaSocket
23
23
}
24
24
void Charge ()
25
25
{
26
- // 使用自带的充电器(两脚扁形)充电
26
+ // Use the built-in charger (two-pin flat) to charge
27
27
m_pCharger->ChargeWithFeetFlat ();
28
28
}
29
29
private:
30
- // 持有需要被适配的接口对象(自带的充电器)
30
+ // Hold the interface object that needs to be adapted (the built-in charger)
31
31
OwnCharger* m_pCharger;
32
32
};
33
33
Original file line number Diff line number Diff line change 5
5
#ifndef DESIGNPATTERN_TARGET_H
6
6
#define DESIGNPATTERN_TARGET_H
7
7
8
- // 俄罗斯提供的插座
8
+ // Sockets provided by Russia
9
9
class IRussiaSocket
10
10
{
11
11
public:
12
- // 使用双脚圆形充电(暂不实现)
12
+ // Use both feet to charge in a round shape (not implemented yet)
13
13
virtual void Charge () = 0;
14
14
};
15
15
Original file line number Diff line number Diff line change 6
6
7
7
void BridgeMain ()
8
8
{
9
- // 创建电器(电灯、电风扇)
9
+ // Create electrical appliances (electric lights, electric fans)
10
10
IElectricalEquipment * light = new Light ();
11
11
IElectricalEquipment * fan = new Fan ();
12
12
13
- // 创建开关(拉链式开关、两位开关)
14
- // 将拉链式开关和电灯关联起来,两位开关和风扇关联起来
13
+ // Create switch (pull chain switch, two-position switch)
14
+ // Associating a pull chain switch with a light and a two-position switch with a fan
15
15
ISwitch * pullChain = new PullChainSwitch (light);
16
16
ISwitch * twoPosition = new TwoPositionSwitch (fan);
17
17
18
- // 开灯、关灯
18
+ // Lights on, lights off
19
19
pullChain->On ();
20
20
pullChain->Off ();
21
21
22
- // 打开风扇、关闭风扇
22
+ // Turn on the fan, turn off the fan
23
23
twoPosition->On ();
24
24
twoPosition->Off ();
25
25
Original file line number Diff line number Diff line change 7
7
8
8
#include " implementor.h"
9
9
10
- // 开关
10
+ // Switch
11
11
class ISwitch
12
12
{
13
13
public:
14
14
ISwitch (IElectricalEquipment *ee){ m_pEe = ee; }
15
15
virtual ~ISwitch (){}
16
- virtual void On () = 0; // 打开电器
17
- virtual void Off () = 0; // 关闭电器
16
+ virtual void On () = 0; // Turn on the appliance
17
+ virtual void Off () = 0; // Turn off the appliance
18
18
19
19
protected:
20
20
IElectricalEquipment * m_pEe;
Original file line number Diff line number Diff line change 8
8
#include " implementor.h"
9
9
#include < iostream>
10
10
11
- // 电灯
11
+ // Electric lights
12
12
class Light : public IElectricalEquipment
13
13
{
14
14
public:
15
- // 开灯
15
+ // Turn on the lights
16
16
virtual void PowerOn () override
17
17
{
18
18
std::cout << " Light is on." << std::endl;
19
19
}
20
- // 关灯
20
+ // Turn off the lights
21
21
virtual void PowerOff () override
22
22
{
23
23
std::cout << " Light is off." << std::endl;
24
24
}
25
25
};
26
26
27
- // 风扇
27
+ // Electric Fan
28
28
class Fan : public IElectricalEquipment
29
29
{
30
30
public:
31
- // 打开风扇
31
+ // Turn on the fan
32
32
virtual void PowerOn () override
33
33
{
34
34
std::cout << " Fan is on." << std::endl;
35
35
}
36
- // 关闭风扇
36
+ // Turn off the fan
37
37
virtual void PowerOff () override
38
38
{
39
39
std::cout << " Fan is off." << std::endl;
Original file line number Diff line number Diff line change 5
5
#ifndef DESIGNPATTERN_IMPLEMENTOR_H
6
6
#define DESIGNPATTERN_IMPLEMENTOR_H
7
7
8
- // 电器
8
+ // Electric equipment
9
9
class IElectricalEquipment
10
10
{
11
11
public:
12
12
virtual ~IElectricalEquipment (){}
13
- virtual void PowerOn () = 0; // 打开
14
- virtual void PowerOff () = 0; // 关闭
13
+ virtual void PowerOn () = 0;
14
+ virtual void PowerOff () = 0;
15
15
};
16
16
17
17
#endif // DESIGNPATTERN_IMPLEMENTOR_H
Original file line number Diff line number Diff line change 8
8
#include " abstraction.h"
9
9
#include < iostream>
10
10
11
- // 拉链式开关
11
+ // Zipper switch
12
12
class PullChainSwitch : public ISwitch
13
13
{
14
14
public:
15
15
PullChainSwitch (IElectricalEquipment *ee) : ISwitch(ee) {}
16
16
17
- // 用拉链式开关打开电器
17
+ // Turn on the equipment with a zipper switch
18
18
virtual void On () override
19
19
{
20
- std::cout << " Switch on the equipment with a pull chain switch." << std::endl;
20
+ std::cout << " Turn on the equipment with a zipper switch." << std::endl;
21
21
m_pEe->PowerOn ();
22
22
}
23
23
24
- // 用拉链式开关关闭电器
24
+ // Turn off the equipment with a zipper switch
25
25
virtual void Off () override
26
26
{
27
- std::cout << " Switch off the equipment with a pull chain switch." << std::endl;
27
+ std::cout << " Turn off the equipment with a zipper switch." << std::endl;
28
28
m_pEe->PowerOff ();
29
29
}
30
30
};
31
31
32
- // 两位开关
32
+ // Two-position switch
33
33
class TwoPositionSwitch : public ISwitch
34
34
{
35
35
public:
36
36
TwoPositionSwitch (IElectricalEquipment *ee) : ISwitch(ee) {}
37
37
38
- // 用两位开关打开电器
38
+ // Turn on the equipment with a two-position switch
39
39
virtual void On () override
40
40
{
41
- std::cout << " Switch on the equipment with a two-position switch." << std::endl;
41
+ std::cout << " Turn on the equipment with a two-position switch." << std::endl;
42
42
m_pEe->PowerOn ();
43
43
}
44
44
45
- // 用两位开关关闭电器
45
+ // Turn off the equipment with a two-position switch
46
46
virtual void Off () override {
47
- std::cout << " Switch off the equipment with a two-position switch." << std::endl;
47
+ std::cout << " Turn off the equipment with a two-position switch." << std::endl;
48
48
m_pEe->PowerOff ();
49
49
}
50
50
};
You can’t perform that action at this time.
0 commit comments