CS 635: Advanced Object-Oriented Design & Programming |
---|
References | slide # 1 |
Functor | slide # 2 |
Command | slide # 7 |
...Structure | slide # 7 |
Command Processor | slide # 12 |
...Structure | slide # 13 |
...Dynamics | slide # 13 |
...Consequences | slide # 14 |
Advanced C++: Programming Styles and Idioms, James Coplien, Addison Wesley, 1992, pp 165-170, Functor Pattern
Pattern-Oriented Software: A System of Patterns, Buschman, Meunier, Rohnert, Sommerlad, Stal, 1996, pp 277-290, Command Processor
Command Processor, Sommerlad in Pattern Languages of Program Design 2, Eds Vlissides, Coplien, Kerth, Addison-Wesley, 1996, pp 63-74,
They serve the role of a function, but can be created, passed as parameters, and manipulated like objects
A functor is a class with a single member function
class SortedList { private Object[] listElements; private void sortTheList() { // use fancy sort method like quicksort blah blah if ( listElement[k] > listElement[k+j] ) { swap or something } } }
We may wish to sort the same objects in different ways
abstract class Compare { private static final int LESS_THAN = -1; private static final int GREATER_THAN = 1; private static final int EQUAL_TO = 0; public boolean greaterThan( Object a, Object b ) { int result = compareTo( a, b ); if ( result == GREATER_THAN ) return true; else return false; public boolean lessThan( Object a, Object b ) { int result = compareTo( a, b ); if ( result == LESS_THAN ) return true; else return false; }
public boolean equals( Object a, Object b ) { int result = compareTo(a, b ); if ( result == EQUAL_TO ) return true; else return false; } abstract public int compareTo( Object a, Object b ); } class CompareInt extends Compare { public int compareTo( Object a, Object b ) { if ( (int) a < (int) b ) return LESS_THAN; else if ( (int) a > (int) b ) return GREATER_THAN; else return EQUAL_TO; } }
class CompareString extends Compare { blah }
class CompareStudentRecordByID extends Compare
{ blah }
class CompareStudentRecordByAddress extends Compare
{ blah }
class SortedList { private Object[] listElements; private Compare comparer; public SortedList( Compare function ) { comparer = function; } private void sortTheList() { // use fancy sort method like quicksort blah blah if ( comparer.lessThan(listElement[k], listElement[k+j] ) ) { swap or something } } }
Same run-time flexibility as function pointer
Lends it self to poor abstractions
Other?
Functors are good for callbacks
Give much more power than functor
Let
When you need to support undo
When you need to support logging changes
When you structure a system around high-level operations built on primitive operations
When you need to support a macro language
It is easy to add new commands, because you do not have to change existing classes
You can assemble commands into a composite object
abstract class Command { abstract public void execute(); } class OpenCommand extends Command { private Application opener; public OpenCommand( Application theOpener ) { opener = theOpener; } public void execute() { String documentName = AskUserSomeHow(); if ( name != null ) { Document toOpen = new Document( documentName ); opener.add( toOpen ); opener.open(); } } }
class Menu { private Hashtable menuActions = new Hashtable(); public void addMenuItem( String displayString, Command itemAction ) { menuActions.put( displayString, itemAction ); } public void handleEvent( String itemSelected ) { Command runMe; runMe = (Command) menuActions.get( itemSelected ); runMe.execute(); } // lots of stuff missing }
class MacroCommand extends Command { private Vector commands = new Vector(); public void add( Command toAdd ) { commands.addElement( toAdd ); } public void remove( Command toRemove ) { commands.removeElement( toAdd ); } public void execute() { Enumeration commandList = commands.elements(); while ( commandList.hasMoreElements() ) { Command nextCommand; nextCommand = (Command) commandList.nextElement(); nextCommand.execute(); } } }
Command Processor covers how the command objects are managed
In the Command Processor, a command processor object handles all command objects
The command processor:
Adding new commands and providing for a macro language comes easy
Programming execution-related services
Concurrency
Potential for an excessive number of command classes
Complexity