forked from shihyu/DesignPatternExample
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
jason_yao
committed
Dec 31, 2013
1 parent
b4cc3e5
commit 698871f
Showing
21 changed files
with
669 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#include <iostream> | ||
#include <string> | ||
#include <vector> | ||
using namespace std; | ||
|
||
//手機軟體 | ||
class HandsetSoft { | ||
public: | ||
virtual void Run() = 0; | ||
}; | ||
|
||
//遊戲軟體 | ||
class HandsetGame : public HandsetSoft { | ||
public: | ||
virtual void Run() { | ||
cout << "運行手機遊戲" << endl; | ||
} | ||
}; | ||
|
||
//通訊錄軟體 | ||
class HandSetAddressList : public HandsetSoft { | ||
public: | ||
virtual void Run() { | ||
cout << "手機通訊錄" << endl; | ||
} | ||
}; | ||
|
||
//手機品牌 | ||
class HandsetBrand { | ||
protected: | ||
HandsetSoft* m_soft; | ||
public: | ||
void SetHandsetSoft(HandsetSoft* temp) { | ||
m_soft = temp; | ||
} | ||
virtual void Run() = 0; | ||
}; | ||
|
||
//M 品牌 | ||
class HandsetBrandM : public HandsetBrand { | ||
public: | ||
virtual void Run() { | ||
m_soft->Run(); | ||
} | ||
}; | ||
|
||
//N 品牌 | ||
class HandsetBrandN : public HandsetBrand { | ||
public: | ||
virtual void Run() { | ||
m_soft->Run(); | ||
} | ||
}; | ||
|
||
//用戶端 | ||
int main() { | ||
HandsetBrand* brand; | ||
brand = new HandsetBrandM(); | ||
brand->SetHandsetSoft(new HandsetGame()); | ||
brand->Run(); | ||
brand->SetHandsetSoft(new HandSetAddressList()); | ||
brand->Run(); | ||
return 0; | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#include <iostream> | ||
#include <string> | ||
#include <vector> | ||
using namespace std; | ||
|
||
//烤肉師傅 | ||
class Barbucer { | ||
public: | ||
void MakeMutton() { | ||
cout << "烤羊肉" << endl; | ||
} | ||
void MakeChickenWing() { | ||
cout << "烤雞翅膀" << endl; | ||
} | ||
}; | ||
|
||
//抽象命令類 | ||
class Command { | ||
protected: | ||
Barbucer* receiver; | ||
public: | ||
Command(Barbucer* temp) { | ||
receiver = temp; | ||
} | ||
virtual void ExecuteCmd() = 0; | ||
}; | ||
|
||
//烤羊肉命令 | ||
class BakeMuttonCmd : public Command { | ||
public: | ||
BakeMuttonCmd(Barbucer* temp) : Command(temp) {} | ||
virtual void ExecuteCmd() { | ||
receiver->MakeMutton(); | ||
} | ||
}; | ||
|
||
//烤雞翅 | ||
class ChickenWingCmd : public Command { | ||
public: | ||
ChickenWingCmd(Barbucer* temp) : Command(temp) {} | ||
virtual void ExecuteCmd() { | ||
receiver->MakeChickenWing(); | ||
} | ||
}; | ||
|
||
//服務員類 | ||
class Waiter { | ||
protected: | ||
vector<Command*> m_commandList; | ||
public: | ||
void SetCmd(Command* temp) { | ||
m_commandList.push_back(temp); | ||
cout << "增加定單" << endl; | ||
} | ||
//通知執行 | ||
void Notify() { | ||
vector<Command*>::iterator p = m_commandList.begin(); | ||
|
||
while (p != m_commandList.end()) { | ||
(*p)->ExecuteCmd(); | ||
p++; | ||
} | ||
} | ||
}; | ||
|
||
//用戶端 | ||
int main() { | ||
//店裡添加烤肉師傅、功能表、服務員等顧客 | ||
Barbucer* barbucer = new Barbucer(); | ||
Command* cmd = new BakeMuttonCmd(barbucer); | ||
Command* cmd2 = new ChickenWingCmd(barbucer); | ||
Waiter* girl = new Waiter(); | ||
//點菜 | ||
girl->SetCmd(cmd); | ||
girl->SetCmd(cmd2); | ||
//服務員通知 | ||
girl->Notify(); | ||
return 0; | ||
} |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#include <iostream> | ||
#include <string> | ||
#include <vector> | ||
using namespace std; | ||
|
||
class Component { | ||
public: | ||
string m_strName; | ||
Component(string strName) { | ||
m_strName = strName; | ||
} | ||
virtual void Add(Component* com) = 0; | ||
virtual void Display(int nDepth) = 0; | ||
}; | ||
|
||
class Leaf : public Component { | ||
public: | ||
Leaf(string strName): Component(strName) {} | ||
virtual void Add(Component* com) { | ||
cout << "leaf can't add" << endl; | ||
} | ||
virtual void Display(int nDepth) { | ||
string strtemp; | ||
|
||
for (int i = 0; i < nDepth; i++) { | ||
strtemp += "-"; | ||
} | ||
|
||
strtemp += m_strName; | ||
cout << strtemp << endl; | ||
} | ||
}; | ||
|
||
class Composite : public Component { | ||
private: | ||
vector<Component*> m_component; | ||
public: | ||
Composite(string strName) : Component(strName) {} | ||
virtual void Add(Component* com) { | ||
m_component.push_back(com); | ||
} | ||
virtual void Display(int nDepth) { | ||
string strtemp; | ||
|
||
for (int i = 0; i < nDepth; i++) { | ||
strtemp += "-"; | ||
} | ||
|
||
strtemp += m_strName; | ||
cout << strtemp << endl; | ||
vector<Component*>::iterator p = m_component.begin(); | ||
|
||
while (p != m_component.end()) { | ||
(*p)->Display(nDepth + 2); | ||
p++; | ||
} | ||
} | ||
}; | ||
|
||
//用戶端 | ||
int main() { | ||
Composite* p = new Composite("小王"); | ||
p->Add(new Leaf("小李")); | ||
p->Add(new Leaf("小趙")); | ||
Composite* p1 = new Composite("小小五"); | ||
p1->Add(new Leaf("大三")); | ||
p->Add(p1); | ||
p->Display(1); | ||
return 0; | ||
} | ||
|
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
#include <iostream> | ||
#include <string> | ||
#include <vector> | ||
using namespace std; | ||
|
||
class Company { | ||
protected: | ||
string m_strName; | ||
public: | ||
Company(string strName) { | ||
m_strName = strName; | ||
} | ||
virtual void Add(Company* c) = 0; | ||
virtual void Display(int nDepth) = 0; | ||
virtual void LineOfDuty() = 0; | ||
}; | ||
|
||
class ConcreteCompany: public Company { | ||
private: | ||
vector<Company*> m_company; | ||
public: | ||
ConcreteCompany(string strName): Company(strName) {} | ||
virtual void Add(Company* c) { | ||
m_company.push_back(c); | ||
} | ||
virtual void Display(int nDepth) { | ||
string strtemp; | ||
|
||
for (int i = 0; i < nDepth; i++) { | ||
strtemp += "-"; | ||
} | ||
|
||
strtemp += m_strName; | ||
cout << strtemp << endl; | ||
vector<Company*>::iterator p = m_company.begin(); | ||
|
||
while (p != m_company.end()) { | ||
(*p)->Display(nDepth + 2); | ||
p++; | ||
} | ||
} | ||
virtual void LineOfDuty() { | ||
vector<Company*>::iterator p = m_company.begin(); | ||
|
||
while (p != m_company.end()) { | ||
(*p)->LineOfDuty(); | ||
p++; | ||
} | ||
} | ||
}; | ||
|
||
class HrDepartment : public Company { | ||
public: | ||
HrDepartment(string strname) : Company(strname) {} | ||
virtual void Display(int nDepth) { | ||
string strtemp; | ||
|
||
for (int i = 0; i < nDepth; i++) { | ||
strtemp += "-"; | ||
} | ||
|
||
strtemp += m_strName; | ||
cout << strtemp << endl; | ||
} | ||
virtual void Add(Company* c) { | ||
cout << "error" << endl; | ||
} | ||
virtual void LineOfDuty() { | ||
cout << m_strName << ":招聘人才" << endl; | ||
} | ||
}; | ||
|
||
//用戶端: | ||
int main() { | ||
ConcreteCompany* p = new ConcreteCompany("清華大學"); | ||
p->Add(new HrDepartment("清華大學人才部")); | ||
ConcreteCompany* p1 = new ConcreteCompany("數學系"); | ||
p1->Add(new HrDepartment("數學系人才部")); | ||
ConcreteCompany* p2 = new ConcreteCompany("物理系"); | ||
p2->Add(new HrDepartment("物理系人才部")); | ||
p->Add(p1); | ||
p->Add(p2); | ||
p->Display(1); | ||
p->LineOfDuty(); | ||
return 0; | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#include <iostream> | ||
#include <string> | ||
#include <vector> | ||
using namespace std; | ||
|
||
class Context; | ||
class AbstractExpression { | ||
public: | ||
virtual void Interpret(Context* context) = 0; | ||
}; | ||
|
||
class Expression : public AbstractExpression { | ||
public: | ||
virtual void Interpret(Context* context) { | ||
cout << "終端解譯器" << endl; | ||
}; | ||
}; | ||
|
||
class NonterminalExpression : public AbstractExpression { | ||
public: | ||
virtual void Interpret(Context* context) { | ||
cout << "非終端解譯器" << endl; | ||
} | ||
}; | ||
|
||
class Context { | ||
public: | ||
string input; | ||
string output; | ||
}; | ||
|
||
//用戶端 | ||
int main() { | ||
Context* context = new Context(); | ||
vector<AbstractExpression*> express; | ||
express.push_back(new Expression()); | ||
express.push_back(new NonterminalExpression()); | ||
express.push_back(new NonterminalExpression()); | ||
vector<AbstractExpression*>::iterator p = express.begin(); | ||
|
||
while (p != express.end()) { | ||
(*p)->Interpret(context); | ||
p++; | ||
} | ||
|
||
return 0; | ||
} |
Binary file not shown.
Oops, something went wrong.