0% found this document useful (0 votes)
74 views

GoF Patterns Part 8a

The document describes using command and factory patterns to encapsulate user operations on an expression tree processing application. It presents the problem of consolidating different user operations like formatting, evaluating, printing, and quitting an expression tree. The solution uses command objects to encapsulate each operation and a factory to create different command object types.

Uploaded by

Hello
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views

GoF Patterns Part 8a

The document describes using command and factory patterns to encapsulate user operations on an expression tree processing application. It presents the problem of consolidating different user operations like formatting, evaluating, printing, and quitting an expression tree. The solution uses command objects to encapsulate each operation and a factory to create different command object types.

Uploaded by

Hello
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

A Case Study of “Gang of Four”

(GoF) Patterns : Part 8


Douglas C. Schmidt
[email protected]
www.dre.vanderbilt.edu/~schmidt

Professor of Computer Science


Institute for Software
Integrated Systems

Vanderbilt University
Nashville, Tennessee, USA
GoF Patterns Expression Tree Case Study Douglas C. Schmidt

Topics Covered in this Part of the Module


• Describe the object-oriented
(OO) expression tree case study
ET_Command
ET_Command
• Evaluate the limitations with _Factory
algorithmic design techniques
ET_Command
• Present an OO design for the _Factory_Impl ET_Command_Impl
expression tree processing app
• Summarize the patterns in
the expression tree design Expr_
Command
Format_
• Explore patterns for Command

• Tree structure & access Eval_


Macro_ Command
• Tree creation Command

• Tree traversal
Quit_
• Commands & factories Print_ Command
Command

2
GoF Patterns Expression Tree Case Study Douglas C. Schmidt

Overview of Command & Factory Patterns


P urpose: Define operations that can users can perform on an expression
tree processing app & centralize extensible creation of these operations
Abstract Factory &
ET_Command
ET_Event_Handler ET_Command_Factory
_Factory_Impl Factory Method

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

Format_Command Expr_Command Eval_Command

These patterns decouple creation from3 use & provide a uniform command API
GoF Patterns Expression Tree Case Study Douglas C. Schmidt

Problem: Consolidating User Operations


Goals
% tree-traversal -v
• Support execution of format [in-order]
user operations Verbose mode
expr [expression]
print [in-order|pre-order|post-
order|level-order]
eval [post-order]
quit
> format in-order
> expr 1+4*3/2
> eval post-order
7
> quit

% tree-traversal
> 1+4*3/2
7

4
GoF Patterns Expression Tree Case Study Douglas C. Schmidt

Problem: Consolidating User Operations


Goals
% tree-traversal -v
• Support execution of format [in-order]
user operations expr [expression]
• Support macro print [in-order|pre-order|post-
operations order|level-order]
eval [post-order]
quit
> format in-order
> expr 1+4*3/2
> eval post-order
7
> quit

% tree-traversal Succinct mode


> 1+4*3/2
7

5
GoF Patterns Expression Tree Case Study Douglas C. Schmidt

Problem: Consolidating User Operations


Goals
• Support execution of
user operations
• Support macro
operations

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

Solution: Encapsulate an Operation w/Command


• A Command encapsulates ET_Command
• An operation method
(execute())
• An inverse operation method ET_Command_Impl
(unexecute())
• A test for reversibility (boolean
Expr_
reversible())
Format_ Command
• State for (un)doing the operation Command

Eval_
Macro_ Command
Command

Quit_
Print_ Command
Command

7
GoF Patterns Expression Tree Case Study Douglas C. Schmidt

Solution: Encapsulate an Operation w/Command


• A Command encapsulates ET_Command
• An operation method
(execute())
• An inverse operation method ET_Command_Impl
(unexecute())
• A test for reversibility (boolean
Expr_
reversible())
Format_ Command
• State for (un)doing the operation Command
• A Command may Eval_
Macro_
• Implement the operation itself or Command
Command

• Forward the operation


implementation to other Quit_
object(s) Print_ Command
Command

Bridge pattern encapsulates variability


8 & simplifies memory management
GoF Patterns Expression Tree Case Study Douglas C. Schmidt

ET_Command Class Interface


• Interface for defining a command that—when executed—performs an
operation on an expression tree
This class plays the role of the abstraction in
the Bridge pattern
Interface ET_Command(ET_Command_Impl *=0)
ET_Command(const ET_Command &)
ET_Command & operator=(const ET_Command &)
~ET_Command()
bool execute() These methods forward to
bool unexecute() the implementor subclass
We don’t use this method but it’s
a common command feature

• Commonality: Provides common interface for expression tree commands


• Variability: Implementations of expression tree commands can vary
depending on the operations requested by user input

9
GoF Patterns Expression Tree Case Study Douglas C. Schmidt

ET_Command_Impl Class Interface


• Base class of an implementor hierarchy used to define commands that
perform operations on an expression tree when they are executed

This class is the base class of the implementor


Interface hierarchy in the Bridge pattern
ET_Command_Impl(ET_Context &)
~ET_Command_Impl()= 0
...
virtual bool execute()=0 The bulk of the work is
typically done here by
virtual bool unexecute()=0
subclasses

• Commonality: Provides a common base class for implementations of


expression tree commands
• Variability: Subclasses of this base class implement different expression
tree commands depending on the operations requested by user input

10
GoF Patterns Expression Tree Case Study Douglas C. Schmidt

Command GoF Object Behavioral


Intent
• Encapsulate the request for a service as an object
Applicability
• Want to parameterize objects with an action to perform
• Want to specify, queue, & execute requests at different times
• For multilevel undo/redo e.g., ET_Command_Impl
Structure

e.g., Eval_Command,
Format_Command,
Print_Command,
Quit_Command,
Macro_Command, etc.

11
GoF Patterns Expression Tree Case Study Douglas C. Schmidt

Command GoF Object Behavioral


Command example in C++
• Encapsulate execution of a sequence of commands as an object
class Macro_Command : public ET_Command_Impl {
public:
... Executes a sequence of commands (used to
bool execute() { implement “succinct mode”)
std::for_each (macro_commands_.begin(),
macro_commands_.end(),
std::mem_fun_ref(&ET_Command::execute));
return true;
}

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

Command GoF Object Behavioral


Command example in C++
• Encapsulate execution of a sequence of commands as an object
class Macro_Command : public ET_Command_Impl {
public:
... STL for_each() algorithm
bool execute() {
std::for_each (macro_commands_.begin(),
macro_commands_.end(),
std::mem_fun_ref(&ET_Command::execute));
return true;
} Application of the Adapter pattern
private:
std::vector <ET_Command> macro_commands_;
...
Vector of commands to execute as a macro
13
GoF Patterns Expression Tree Case Study Douglas C. Schmidt

Command GoF Object Behavioral


Command example in C++
• Encapsulate execution of a sequence of subcommands as an object
class Macro_Command : public ET_Command_Impl {
public:
...
bool execute() {
std::for_each (macro_commands_.begin(),
macro_commands_.end(),
[](ET_Command &c){ c.execute(); });
return true;
} C++11 lambda expression
private:
std::vector <ET_Command> macro_commands_;
...

14
GoF Patterns Expression Tree Case Study Douglas C. Schmidt

Command GoF Object Behavioral


Command example in C++
• Encapsulate execution of a sequence of subcommands as an object
class Macro_Command : public ET_Command_Impl {
public:
...
bool execute() {
for (auto &iter = macro_commands_)
iter.execute();

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

Command GoF Object Behavioral


Consequences
+ Abstracts executor of a service
+ Supports arbitrary-level undo-redo
+ Composition yields macro-commands
– Might result in lots of trivial command
subclasses
– Excessive memory may be needed to
support undo/redo operations

16
GoF Patterns Expression Tree Case Study Douglas C. Schmidt

Command GoF Object Behavioral


Consequences
+ Abstracts executor of a service
+ Supports arbitrary-level undo-redo
+ Composition yields macro-commands
– Might result in lots of trivial command
subclasses
– Excessive memory may be needed to
support undo/redo operations
Implementation
• Copying a command before putting it on
a history list
• Avoiding error accumulation during undo/redo
• Supporting transactions
17
GoF Patterns Expression Tree Case Study Douglas C. Schmidt

Command GoF Object Behavioral


Consequences Known Uses
+ Abstracts executor of a service • InterViews Actions
+ Supports arbitrary-level undo-redo • MacApp, Unidraw
Commands
+ Composition yields macro-commands
• JDK’s UndoableEdit,
– Might result in lots of trivial command AccessibleAction
subclasses
• Emacs
– Excessive memory may be needed to • Microsoft Office tools
support undo/redo operations
See Also
Implementation
• Command Processor pattern
• Copying a command before putting it on in POSA1
a history list
• Avoiding error accumulation during undo/redo
• Supporting transactions
18
GoF Patterns Expression Tree Case Study Douglas C. Schmidt

Problem: Consolidating Creation of Variabilities


Goals
• Simplify & centralize the creation of all
variabilities in the expression tree application
to ensure semantic compatibility
• Be extensible for future variabilities

ET_Command ET_Command_Impl

Format_Command Print_Command Eval_Command Quit_Command

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

Problem: Consolidating Creation of Variabilities


Goals Constraints/forces
• Simplify & centralize the creation of all • Don’t recode existing
variabilities in the expression tree application clients
to ensure semantic compatibility • Add new variabilities
• Be extensible for future variabilities without recompiling

ET_Command ET_Command_Impl

Format_Command Print_Command Eval_Command Quit_Command

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

Solution: Abstract Object Creation


Factory Hard-codes a lexical
dependency on
• Instead of
Print_Command
ET_Command command(new Print_Command());
Use No lexical dependency
on any concrete class
ET_Command command
(command_factory.make_command("print"));
where command_factory is an instance of ET_Command_Factory

21
GoF Patterns Expression Tree Case Study Douglas C. Schmidt

Solution: Abstract Object Creation


Factory
• Instead of
ET_Command command(new Print_Command());
Use
ET_Command command
(command_factory.make_command("print"));
where command_factory is an instance of ET_Command_Factory
Factory structure

ET_Command_Factory ET_Command_Factory_Impl

Concrete_ET_Command
_Factory_Impl

Bridge pattern encapsulates variability


22 & simplifies memory management
GoF Patterns Expression Tree Case Study Douglas C. Schmidt

ET_Command_Factory Class Interface


• Interface used to create appropriate command based on string supplied
by caller This class plays the role of the abstraction in
the Bridge pattern
Interface ET_Command_Factory(ET_Context &tree_context)
ET_Command_Factory(ET_Command_Factory_Impl *)
ET_Command make_command(const std::string &s)
...
This is the main factory method
These are helper
factory methods
ET_Command make_format_command(const std::string &)
ET_Command make_expr_command(const std::string &)
ET_Command make_print_command(const std::string &)
ET_Command make_eval_command(const std::string &)
ET_Command make_quit_command(const std::string &)
ET_Command make_macro_command(const std::string &)
• Commonality: Provides a common interface to create commands
• Variability: Implementations of expression tree command factory
methods can vary depending on the23requested commands
GoF Patterns Expression Tree Case Study Douglas C. Schmidt

ET_Command_Factory_Impl Class Interface


• Base class of an implementor hierarchy used to create appropriate commands
based on string supplied by caller
This class is base class of implementor
Interface hierarchy in the Bridge pattern
ET_Command_Factory_Impl(ET_Context &context)
~ET_Command_Factory_Impl()
virtual ET_Command make_command(const std::string &s)=0
...
Pure virtual
methods
virtual ET_Command make_format_command(const std::string &)=0
virtual ET_Command make_expr_command(const std::string &)=0
virtual ET_Command make_print_command(const std::string &)=0
virtual ET_Command make_eval_command(const std::string &)=0
virtual ET_Command make_quit_command(const std::string &)=0
virtual ET_Command make_macro_command(const std::string &)=0

• Commonality: Provides a common interface to create commands


• Variability: Subclasses of this base class define expression tree
command factory methods vary depending
24 on the requested commands
GoF Patterns Expression Tree Case Study Douglas C. Schmidt

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

Each factory method


creates a different type
of concrete command

Bridge pattern encapsulates variability


25 & simplifies memory management
GoF Patterns Expression Tree Case Study Douglas C. Schmidt

Factory Method GoF Class Creational


Intent
• Provide an interface for creating an object, but leave choice of object’s
concrete type to a subclass
Applicability
• When a class cannot anticipate the objects it must create or a class
wants its subclasses to specify the objects it creates
e.g., ET_Command
Structure e.g., ET_Command_Impl
_Factory_Impl

e.g., Concrete_ET_
Command_Factory_Impl

e.g., Eval_Command, Print_


Command, Macro_Command, etc. 26
GoF Patterns Expression Tree Case Study Douglas C. Schmidt

Factory Method GoF Class Creational


Factory Method example in C++
• An interface for creating a command, letting subclass chose concrete type
class ET_Command_Factory_Impl { Pure virtual method
public:
virtual ET_Command make_macro_command(const std::string &) = 0;
...

27
GoF Patterns Expression Tree Case Study Douglas C. Schmidt

Factory Method GoF Class Creational


Factory Method example in C++
• An interface for creating a command, letting subclass chose concrete type
class ET_Command_Factory_Impl {
public:
virtual ET_Command make_macro_command(const std::string &) = 0;
...
class Concrete_ET_Command_Factory_Impl
Used to implement : public ET_Command_Factory_Impl {
public: “succinct mode”
virtual ET_Command make_macro_command(const std::string &expr) {
std::vector <ET_Command> commands;
Create vector of commands that are executed as a macro
commands.push_back(make_format_command("in-order"));
commands.push_back(make_expr_command(expr));
commands.push_back(make_eval_command("post-order"));
return ET_Command(new Macro_Command(tree_context_, commands));
}
28 within Bridge abstraction object
Encapsulates command
GoF Patterns Expression Tree Case Study Douglas C. Schmidt

Factory Method GoF Class Creational


Consequences
+ Flexibility: The client becomes more flexible
by not specifying the class name of the
concrete class & the details of its creation
+ Decoupling: The client only depends on the
interface
– More classes: Construction of objects
requires an additional class in some cases

29
GoF Patterns Expression Tree Case Study Douglas C. Schmidt

Factory Method GoF Class Creational


Consequences
+ Flexibility: The client becomes more flexible
by not specifying the class name of the
concrete class & the details of its creation
+ Decoupling: The client only depends on the
interface
– More classes: Construction of objects
requires an additional class in some cases
Implementation
• There are two choices
• The creator class is abstract & does not implement creation methods
(then it must be subclassed)
• The creator class is concrete & provides a default implementation (then
it can be subclassed)
• If a factory method can create different variants the method should be
passed a parameter to designate the 30variant
GoF Patterns Expression Tree Case Study Douglas C. Schmidt

Factory Method GoF Class Creational


Consequences Known Uses
+ Flexibility: The client becomes more flexible • InterViews Kits
by not specifying the class name of the • ET++
concrete class & the details of its creation WindowSystem
+ Decoupling: The client only depends on the • AWT Toolkit
interface
• The ACE ORB
– More classes: Construction of objects (TAO)
requires an additional class in some cases
• BREW feature
Implementation phone frameworks
• There are two choices
• The creator class is abstract & does not implement creation methods
(then it must be subclassed)
• The creator class is concrete & provides a default implementation (then
it can be subclassed)
• If a factory method can create different variants the method must be
provided with a parameter 31

You might also like