Skip to content

Commit 52d9d20

Browse files
committed
add code for facade
1 parent e7f79a6 commit 52d9d20

File tree

14 files changed

+545
-1
lines changed

14 files changed

+545
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
[组合模式 Composite](doc/10-组合.md)
2929

30-
装饰模式 Decorator
30+
[装饰模式 Decorator](doc/11-装饰.md)
3131

3232
外观模式 Facade
3333

code/10_decorator/10_decorator.sln

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.29215.179
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "10_decorator", "10_decorator.vcxproj", "{B4B5A849-0BA5-4D7B-A8B3-D80CE8B60CD7}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|x64 = Debug|x64
11+
Debug|x86 = Debug|x86
12+
Release|x64 = Release|x64
13+
Release|x86 = Release|x86
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{B4B5A849-0BA5-4D7B-A8B3-D80CE8B60CD7}.Debug|x64.ActiveCfg = Debug|x64
17+
{B4B5A849-0BA5-4D7B-A8B3-D80CE8B60CD7}.Debug|x64.Build.0 = Debug|x64
18+
{B4B5A849-0BA5-4D7B-A8B3-D80CE8B60CD7}.Debug|x86.ActiveCfg = Debug|Win32
19+
{B4B5A849-0BA5-4D7B-A8B3-D80CE8B60CD7}.Debug|x86.Build.0 = Debug|Win32
20+
{B4B5A849-0BA5-4D7B-A8B3-D80CE8B60CD7}.Release|x64.ActiveCfg = Release|x64
21+
{B4B5A849-0BA5-4D7B-A8B3-D80CE8B60CD7}.Release|x64.Build.0 = Release|x64
22+
{B4B5A849-0BA5-4D7B-A8B3-D80CE8B60CD7}.Release|x86.ActiveCfg = Release|Win32
23+
{B4B5A849-0BA5-4D7B-A8B3-D80CE8B60CD7}.Release|x86.Build.0 = Release|Win32
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {C7A2490F-C80E-4C09-A991-E017B2F54014}
30+
EndGlobalSection
31+
EndGlobal

code/10_decorator/cartypes.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include "cartypes.h"
2+
3+
void BaoJunCar510::drive()
4+
{
5+
cout << "Could drive." << endl;
6+
}
7+
8+
CarWarpper::CarWarpper(Car* car) :m_car(car)
9+
{
10+
}
11+
12+
Car4MainAirbg::Car4MainAirbg(Car* car) : CarWarpper(car)
13+
{
14+
}
15+
16+
void Car4MainAirbg::drive()
17+
{
18+
this->m_car->drive();
19+
this->deployPrimaryAirBag();
20+
}
21+
22+
void Car4MainAirbg::deployPrimaryAirBag()
23+
{
24+
cout << "Could deploy the primary airbag." << endl;
25+
}
26+
27+
Car4SencondAirbg::Car4SencondAirbg(Car* car) :CarWarpper(car)
28+
{
29+
}
30+
31+
void Car4SencondAirbg::drive()
32+
{
33+
this->m_car->drive();
34+
this->deploySecondAirBag();
35+
}
36+
37+
void Car4SencondAirbg::deploySecondAirBag()
38+
{
39+
cout << "Could deploy the secondary airbag." << endl;
40+
}
41+
42+
Car4ThirdAirbg::Car4ThirdAirbg(Car* car) :CarWarpper(car)
43+
{
44+
}
45+
46+
void Car4ThirdAirbg::drive()
47+
{
48+
this->m_car->drive();
49+
this->deployThirdAirBag();
50+
}
51+
52+
void Car4ThirdAirbg::deployThirdAirBag()
53+
{
54+
cout << "Could deploy the third airbag." << endl;
55+
}

code/10_decorator/cartypes.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#pragma once
2+
#include <string>
3+
#include <iostream>
4+
using namespace std;
5+
6+
class Car {
7+
public:
8+
virtual void drive() = 0;
9+
};
10+
11+
class BaoJunCar510 :public Car {
12+
public:
13+
void drive() override;
14+
};
15+
16+
class CarWarpper :public Car {
17+
public:
18+
explicit CarWarpper(Car* car);
19+
virtual void drive() = 0;
20+
protected:
21+
Car* m_car;
22+
};
23+
24+
class Car4MainAirbg :public CarWarpper {
25+
public:
26+
explicit Car4MainAirbg(Car* car);
27+
void drive() override;
28+
void deployPrimaryAirBag();
29+
};
30+
31+
class Car4SencondAirbg :public CarWarpper {
32+
public:
33+
explicit Car4SencondAirbg(Car* car);
34+
void drive() override;
35+
void deploySecondAirBag();
36+
};
37+
38+
class Car4ThirdAirbg :public CarWarpper {
39+
public:
40+
explicit Car4ThirdAirbg(Car* car);
41+
void drive() override;
42+
void deployThirdAirBag();
43+
};

code/10_decorator/client.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include "cartypes.h"
2+
3+
int main() {
4+
cout << "Baojun 510 Basic car" << endl;
5+
6+
Car* baseCar = new BaoJunCar510();
7+
baseCar->drive();
8+
9+
cout << "\nBaojun 510 L1 car" << endl;
10+
Car* type1 = new Car4MainAirbg(baseCar);
11+
type1->drive();
12+
cout << endl;
13+
14+
cout << "Baojun 510 L2 car" << endl;
15+
Car* type2 = new Car4SencondAirbg(type1);
16+
type2->drive();
17+
cout << endl;
18+
19+
cout << "Baojun 510 L3 car" << endl;
20+
Car* type3 = new Car4ThirdAirbg(type2);
21+
type3->drive();
22+
23+
return 0;
24+
}

code/11_facade/11_facade.sln

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.29215.179
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "11_facade", "11_facade.vcxproj", "{A920AAF1-B5A6-4612-A5DC-2D064AAE2FAD}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|x64 = Debug|x64
11+
Debug|x86 = Debug|x86
12+
Release|x64 = Release|x64
13+
Release|x86 = Release|x86
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{A920AAF1-B5A6-4612-A5DC-2D064AAE2FAD}.Debug|x64.ActiveCfg = Debug|x64
17+
{A920AAF1-B5A6-4612-A5DC-2D064AAE2FAD}.Debug|x64.Build.0 = Debug|x64
18+
{A920AAF1-B5A6-4612-A5DC-2D064AAE2FAD}.Debug|x86.ActiveCfg = Debug|Win32
19+
{A920AAF1-B5A6-4612-A5DC-2D064AAE2FAD}.Debug|x86.Build.0 = Debug|Win32
20+
{A920AAF1-B5A6-4612-A5DC-2D064AAE2FAD}.Release|x64.ActiveCfg = Release|x64
21+
{A920AAF1-B5A6-4612-A5DC-2D064AAE2FAD}.Release|x64.Build.0 = Release|x64
22+
{A920AAF1-B5A6-4612-A5DC-2D064AAE2FAD}.Release|x86.ActiveCfg = Release|Win32
23+
{A920AAF1-B5A6-4612-A5DC-2D064AAE2FAD}.Release|x86.Build.0 = Release|Win32
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {BB829526-7545-40F3-8407-FB5377B48A54}
30+
EndGlobalSection
31+
EndGlobal

code/11_facade/client.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include "dining.h"
2+
3+
int main() {
4+
5+
Waitress* waitress = new Waitress();
6+
waitress->order();
7+
waitress->pay();
8+
9+
return 0;
10+
}

code/11_facade/dining.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#pragma once
2+
#include <iostream>
3+
#include <string>
4+
using namespace std;
5+
6+
class Cook {
7+
public:
8+
inline void cook() {
9+
cout << "Cook is cooking" << endl;
10+
}
11+
};
12+
13+
class BusBoy {
14+
public:
15+
inline void delivery() {
16+
cout << "Busboy is delivering" << endl;
17+
}
18+
};
19+
20+
class Cashier {
21+
public:
22+
inline void checkout() {
23+
cout << "Cashier is checkouting" << endl;
24+
}
25+
};
26+
27+
class Waitress {
28+
public:
29+
explicit Waitress() {
30+
m_cook = new Cook();
31+
m_boy = new BusBoy();
32+
m_cashier = new Cashier();
33+
}
34+
void order() {
35+
m_cook->cook();
36+
m_boy->delivery();
37+
}
38+
void pay() {
39+
m_cashier->checkout();
40+
}
41+
private:
42+
Cook* m_cook;
43+
BusBoy* m_boy;
44+
Cashier* m_cashier;
45+
};

0 commit comments

Comments
 (0)