Skip to content

Commit 4f63bc3

Browse files
author
jason_yao
committed
update
1 parent 3f6defc commit 4f63bc3

22 files changed

Lines changed: 2664 additions & 0 deletions

File tree

大話/cpp/adapter/adapter.cpp

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#include <string>
2+
#include <iostream>
3+
4+
class Player {
5+
private:
6+
std::string name;
7+
8+
public:
9+
virtual std::string getName();
10+
11+
virtual void setName(std::string name);
12+
13+
virtual void attrack();
14+
15+
virtual void defence();
16+
};
17+
18+
class Center : public Player {
19+
20+
public:
21+
Center(std::string name);
22+
23+
virtual void jinggong();
24+
25+
virtual void fangshou();
26+
};
27+
28+
29+
class Guard : public Player {
30+
31+
public:
32+
Guard(std::string name);
33+
34+
virtual void attrack();
35+
36+
virtual void defence();
37+
};
38+
39+
class Translator : public Player {
40+
public:
41+
Center* wjzf;
42+
43+
Translator(std::string name);
44+
45+
virtual void attrack();
46+
47+
virtual void defence();
48+
};
49+
50+
class AdapterMain {
51+
52+
///
53+
/// * <param name="args"> </param>
54+
///
55+
static void main(std::string args[]);
56+
57+
};
58+
59+
Translator::Translator(std::string name) {
60+
wjzf = new Center(name);
61+
}
62+
63+
void Translator::attrack() {
64+
wjzf->jinggong();
65+
}
66+
67+
void Translator::defence() {
68+
wjzf->fangshou();
69+
}
70+
71+
Center::Center(std::string name) {
72+
this->setName(name);
73+
}
74+
75+
void Center::jinggong() {
76+
std::cout << this->getName() + std::string(" jinggong") << std::endl;
77+
}
78+
79+
void Center::fangshou() {
80+
std::cout << this->getName() + std::string(" fangshou") << std::endl;
81+
}
82+
83+
std::string Player::getName() {
84+
return name;
85+
}
86+
87+
void Player::setName(std::string name) {
88+
this->name = name;
89+
}
90+
91+
void Player::attrack() {
92+
}
93+
94+
void Player::defence() {
95+
}
96+
97+
Guard::Guard(std::string name) {
98+
this->setName(name);
99+
}
100+
101+
void Guard::attrack() {
102+
std::cout << this->getName() + std::string(" attrack") << std::endl;
103+
}
104+
105+
void Guard::defence() {
106+
std::cout << this->getName() + std::string(" defence") << std::endl;
107+
}
108+
109+
int main() {
110+
Player* guard = new Guard("Alston");
111+
guard->attrack();
112+
guard->defence();
113+
Player* center = new Translator("YM");
114+
center->attrack();
115+
center->defence();
116+
return 0;
117+
}
118+

大話/cpp/brige/brige.cpp

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#include <string>
2+
#include <iostream>
3+
4+
class MobileSoft {
5+
public:
6+
virtual void run() = 0;
7+
};
8+
9+
class Mobile {
10+
private:
11+
MobileSoft* soft;
12+
std::string brand;
13+
14+
public:
15+
Mobile(std::string brand);
16+
17+
virtual MobileSoft* getSoft();
18+
19+
virtual void setSoft(MobileSoft* soft);
20+
21+
virtual std::string getBrand();
22+
23+
virtual void setBrand(std::string brand);
24+
25+
virtual void run() = 0;
26+
};
27+
28+
class MobileMp3 : public MobileSoft {
29+
public:
30+
virtual void run();
31+
32+
};
33+
34+
class MotoMible : public Mobile {
35+
public:
36+
MotoMible(std::string brand);
37+
38+
virtual void run();
39+
40+
};
41+
42+
class MobileGame : public MobileSoft {
43+
public:
44+
virtual void run();
45+
46+
};
47+
48+
class NokiaMobile : public Mobile {
49+
public:
50+
NokiaMobile(std::string brand);
51+
52+
virtual void run();
53+
54+
};
55+
56+
MotoMible::MotoMible(std::string brand)
57+
: Mobile(brand) {
58+
}
59+
60+
void MotoMible::run() {
61+
std::cout << "Moto Mobile: " << std::endl;
62+
this->getSoft()->run();
63+
}
64+
65+
NokiaMobile::NokiaMobile(std::string brand)
66+
: Mobile(brand) {
67+
}
68+
69+
void NokiaMobile::run() {
70+
std::cout << "Nokia Mobile: " << std::endl;
71+
this->getSoft()->run();
72+
}
73+
74+
void MobileGame::run() {
75+
std::cout << "run mobile game!" << std::endl;
76+
}
77+
78+
void MobileMp3::run() {
79+
std::cout << "run mobile mp3!" << std::endl;
80+
}
81+
82+
Mobile::Mobile(std::string brand) {
83+
this->brand = brand;
84+
}
85+
86+
MobileSoft* Mobile::getSoft() {
87+
return soft;
88+
}
89+
90+
void Mobile::setSoft(MobileSoft* soft) {
91+
this->soft = soft;
92+
}
93+
94+
std::string Mobile::getBrand() {
95+
return brand;
96+
}
97+
98+
void Mobile::setBrand(std::string brand) {
99+
this->brand = brand;
100+
}
101+
102+
int main() {
103+
Mobile* nokia = new NokiaMobile("Nokia");
104+
MobileSoft* game = new MobileGame();
105+
nokia->setSoft(game);
106+
nokia->run();
107+
Mobile* moto = new MotoMible("Moto");
108+
MobileSoft* mp3 = new MobileMp3();
109+
moto->setSoft(mp3);
110+
moto->run();
111+
return 0;
112+
}
113+
114+

大話/cpp/builder/builder.cpp

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#include <string>
2+
#include <iostream>
3+
4+
class PersonBuilder {
5+
6+
public:
7+
virtual void createHead() = 0;
8+
virtual void createBody() = 0;
9+
virtual void createArm() = 0;
10+
virtual void createLeg() = 0;
11+
};
12+
13+
class PersonDirector {
14+
15+
private:
16+
PersonBuilder* pb;
17+
18+
public:
19+
PersonDirector(PersonBuilder* pb);
20+
21+
virtual void createPerson();
22+
};
23+
24+
class ThinPersonBuilder : public PersonBuilder {
25+
26+
public:
27+
virtual void createArm();
28+
29+
virtual void createBody();
30+
31+
virtual void createHead();
32+
33+
virtual void createLeg();
34+
35+
};
36+
37+
class FatPersonBuilder : public PersonBuilder {
38+
39+
public:
40+
virtual void createArm();
41+
42+
virtual void createBody();
43+
44+
virtual void createHead();
45+
46+
virtual void createLeg();
47+
48+
};
49+
50+
51+
class BuilderMain {
52+
53+
///
54+
/// * <param name="args"> </param>
55+
///
56+
static void main(std::string args[]);
57+
58+
};
59+
60+
void ThinPersonBuilder::createArm() {
61+
std::cout << "Create A Thin Arm!" << std::endl;
62+
}
63+
64+
void ThinPersonBuilder::createBody() {
65+
std::cout << "Create A Thin Body!" << std::endl;
66+
}
67+
68+
void ThinPersonBuilder::createHead() {
69+
std::cout << "Create A Thin Head!" << std::endl;
70+
}
71+
72+
void ThinPersonBuilder::createLeg() {
73+
std::cout << "Create A Thin Leg!" << std::endl;
74+
}
75+
76+
77+
void FatPersonBuilder::createArm() {
78+
std::cout << "Create A Fat Arm!" << std::endl;
79+
}
80+
81+
void FatPersonBuilder::createBody() {
82+
std::cout << "Create A Fat Arm!" << std::endl;
83+
}
84+
85+
void FatPersonBuilder::createHead() {
86+
std::cout << "Create A Fat Arm!" << std::endl;
87+
}
88+
89+
void FatPersonBuilder::createLeg() {
90+
std::cout << "Create A Fat Arm!" << std::endl;
91+
}
92+
93+
94+
PersonDirector::PersonDirector(PersonBuilder* pb) {
95+
this->pb = pb;
96+
}
97+
98+
void PersonDirector::createPerson() {
99+
pb->createHead();
100+
pb->createBody();
101+
pb->createArm();
102+
pb->createLeg();
103+
}
104+
105+
int main() {
106+
PersonBuilder* thin = new ThinPersonBuilder();
107+
PersonBuilder* fat = new FatPersonBuilder();
108+
PersonDirector* pd = new PersonDirector(thin);
109+
pd->createPerson();
110+
pd = new PersonDirector(fat);
111+
pd->createPerson();
112+
return 0;
113+
}
114+

0 commit comments

Comments
 (0)