GoF Patterns Part 8a
GoF Patterns Part 8a
Vanderbilt University
Nashville, Tennessee, USA
GoF Patterns Expression Tree Case Study Douglas C. Schmidt
• Tree traversal
Quit_
• Commands & factories Print_ Command
Command
2
GoF Patterns Expression Tree Case Study Douglas C. Schmidt
Concrete_ET_
<< create >> ET_Context
Command_Factory_Impl
* ET_Command ET_Command_Impl
Command
1
Macro_Command Print_Command Set_Command Quit_Command Null_Command
These patterns decouple creation from3 use & provide a uniform command API
GoF Patterns Expression Tree Case Study Douglas C. Schmidt
% tree-traversal
> 1+4*3/2
7
4
GoF Patterns Expression Tree Case Study Douglas C. Schmidt
5
GoF Patterns Expression Tree Case Study Douglas C. Schmidt
Constraints/forces
• Avoid scattering the
implementation of
operations throughout
the source code
• Ensure consistent
memory management
6
GoF Patterns Expression Tree Case Study Douglas C. Schmidt
Eval_
Macro_ Command
Command
Quit_
Print_ Command
Command
7
GoF Patterns Expression Tree Case Study Douglas C. Schmidt
9
GoF Patterns Expression Tree Case Study Douglas C. Schmidt
10
GoF Patterns Expression Tree Case Study Douglas C. Schmidt
e.g., Eval_Command,
Format_Command,
Print_Command,
Quit_Command,
Macro_Command, etc.
11
GoF Patterns Expression Tree Case Study Douglas C. Schmidt
private:
std::vector <ET_Command> macro_commands_;
...
Vector of commands to execute as a macro
12
GoF Patterns Expression Tree Case Study Douglas C. Schmidt
14
GoF Patterns Expression Tree Case Study Douglas C. Schmidt
return true;
C++11 range-based for loop
}
private:
std::vector <ET_Command> macro_commands_;
...
15
GoF Patterns Expression Tree Case Study Douglas C. Schmidt
16
GoF Patterns Expression Tree Case Study Douglas C. Schmidt
ET_Command ET_Command_Impl
Expr_Command Macro_Command
ET_Iterator ET_Iterator_Impl
In_Order_ET_Iterator_Impl Post_Order_ET_Iterator_Impl
Level_Order_ET_Iterator_Impl Pre_Order_ET_Iterator_Impl
19
GoF Patterns Expression Tree Case Study Douglas C. Schmidt
ET_Command ET_Command_Impl
Expr_Command Macro_Command
ET_Iterator ET_Iterator_Impl
In_Order_ET_Iterator_Impl Post_Order_ET_Iterator_Impl
Level_Order_ET_Iterator_Impl Pre_Order_ET_Iterator_Impl
20
GoF Patterns Expression Tree Case Study Douglas C. Schmidt
21
GoF Patterns Expression Tree Case Study Douglas C. Schmidt
ET_Command_Factory ET_Command_Factory_Impl
Concrete_ET_Command
_Factory_Impl
Factory Structure
ET_Command_Factory ET_Command
ET_Command_Factory_Impl ET_Command_Impl
Concrete_ET_Command
_Factory_Impl
Expr_
+make_command() Command
Format_
#make_expr_command() Command
#make_format_command()
Eval_
#make_eval_command()
Macro_ Command
#make_macro_command()
#make_quit_command() Command
#make_print_command() Quit_
Print_ Command
Command
e.g., Concrete_ET_
Command_Factory_Impl
27
GoF Patterns Expression Tree Case Study Douglas C. Schmidt
29
GoF Patterns Expression Tree Case Study Douglas C. Schmidt