Skip to content

Commit

Permalink
Create Interpreter Pattern
Browse files Browse the repository at this point in the history
Signed-off-by: Junzhuo Du <[email protected]>
  • Loading branch information
Junzhuo Du committed Feb 29, 2020
1 parent 8add518 commit 0a068c3
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 234 deletions.
108 changes: 0 additions & 108 deletions BehaviroalPatterns/command/Command.cpp

This file was deleted.

61 changes: 61 additions & 0 deletions BehaviroalPatterns/command/Command.cxx
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();
}
125 changes: 0 additions & 125 deletions BehaviroalPatterns/interpreter/Interpreter.cpp

This file was deleted.

84 changes: 84 additions & 0 deletions BehaviroalPatterns/interpreter/Interpreter.cxx
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;
}
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ foreach(_dir IN ITEMS ${PATTERNS})
set(_project_name "${_file_name}")
message(STATUS " ${_dir}/${_file_name} is going to be built")

add_executable(${_project_name} "${_dir}/${_file_name}" BehaviroalPatterns/chain-of-responsibility/ChainOfResponsibility.cxx)
add_executable(${_project_name} "${_dir}/${_file_name}")
endforeach()

endforeach()

0 comments on commit 0a068c3

Please sign in to comment.