-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Junzhuo Du <[email protected]>
- Loading branch information
Junzhuo Du
committed
Feb 29, 2020
1 parent
8add518
commit 0a068c3
Showing
5 changed files
with
146 additions
and
234 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,61 @@ | ||
/* | ||
* C++ Design Patterns: | ||
* Author: Junzhuo Du [github.com/Junzhuodu] | ||
* 2020 | ||
* | ||
*/ | ||
|
||
#include <iostream> | ||
|
||
class Receiver { | ||
public: | ||
void action() { | ||
std::cout << "Recevier: execute action" << std::endl; | ||
} | ||
}; | ||
|
||
class Command { | ||
public: | ||
virtual ~Command() {} | ||
|
||
virtual void execute() = 0; | ||
}; | ||
|
||
class ConcreteCommand : public Command { | ||
public: | ||
ConcreteCommand(Receiver *r) : receiver_(r) {}; | ||
~ConcreteCommand() { | ||
if (!receiver_) { | ||
delete receiver_; | ||
} | ||
} | ||
|
||
void execute() { | ||
receiver_->action(); | ||
} | ||
private: | ||
Receiver* receiver_; | ||
}; | ||
|
||
class Invoker { | ||
public: | ||
void setCommand(Command* c) { | ||
command_ = c; | ||
} | ||
|
||
void executeCommand() { | ||
if (command_) command_->execute(); | ||
} | ||
|
||
private: | ||
Command* command_; | ||
}; | ||
|
||
int main() { | ||
Receiver* receiver = new Receiver(); | ||
Command *command = new ConcreteCommand(receiver); | ||
|
||
Invoker invoker; | ||
invoker.setCommand(command); | ||
invoker.executeCommand(); | ||
} |
This file was deleted.
Oops, something went wrong.
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,84 @@ | ||
/* | ||
* C++ Design Patterns: | ||
* Author: Junzhuo Du [github.com/Junzhuodu] | ||
* 2020 | ||
* | ||
*/ | ||
|
||
#include <iostream> | ||
#include <unordered_map> | ||
|
||
/* | ||
* Interpreter example: | ||
* A->true, B->false, AB->false, C->true, AC->true | ||
*/ | ||
class Context { | ||
public: | ||
void set(char var, bool value) { | ||
maps.insert(std::make_pair(var, value)); | ||
} | ||
|
||
char get(char var) { | ||
return maps[var]; | ||
} | ||
private: | ||
std::unordered_map<char, bool> maps; | ||
}; | ||
|
||
class AbstractExpression { | ||
public: | ||
virtual ~AbstractExpression() {} | ||
|
||
virtual bool interpret(Context* const context) = 0; | ||
}; | ||
|
||
class TerminalExpression : public AbstractExpression { | ||
public: | ||
TerminalExpression(char var) : var_(var) {} | ||
~TerminalExpression() {} | ||
|
||
bool interpret(Context* const context) { | ||
return context->get(var_); | ||
} | ||
private: | ||
char var_; | ||
}; | ||
|
||
class NonTerminalExpression : public AbstractExpression { | ||
public: | ||
NonTerminalExpression(AbstractExpression* f, AbstractExpression* s) | ||
: first(f), second(s) {} | ||
|
||
~NonTerminalExpression() { | ||
if (first) delete first; | ||
if (second) delete second; | ||
} | ||
|
||
bool interpret(Context* const context) { | ||
return first->interpret(context) && second->interpret(context); | ||
} | ||
private: | ||
AbstractExpression* first; | ||
AbstractExpression* second; | ||
}; | ||
|
||
int main() { | ||
Context context; | ||
context.set('A', true); | ||
context.set('B', false); | ||
context.set('C', true); | ||
|
||
AbstractExpression* expressionA = new TerminalExpression('A'); | ||
AbstractExpression* expressionB = new TerminalExpression('B'); | ||
AbstractExpression* expressionC = new TerminalExpression('C'); | ||
|
||
AbstractExpression* expressionAB = new NonTerminalExpression(expressionA, expressionB); | ||
AbstractExpression* expressionAC = new NonTerminalExpression(expressionA, expressionC); | ||
|
||
std::cout << "A -> " << expressionA->interpret(&context) << std::endl; | ||
std::cout << "B -> " << expressionB->interpret(&context) << std::endl; | ||
std::cout << "C -> " << expressionC->interpret(&context) << std::endl; | ||
|
||
std::cout << "AB -> " << expressionAB->interpret(&context) << std::endl; | ||
std::cout << "AC -> " << expressionAC->interpret(&context) << std::endl; | ||
} |
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