Example calculator design Dr. Onno van Roosmalen L A T EX version: Pieter van den Hombergh Fontys Hogeschool voor Techniek en Logistiek September 25, 2017
Example calculator Content design ROO,HOM Command Consequences Interpreter Interpreter Example calculator design Prototype Pattern interpreter Example calculator design Calculator Prototype Description Design Rationales interpreter Interpreter Implementation Command Pattern Calculator Command Description Prototype Participants Command Structure Design Rationales Command Participants Factory Method Pattern Command Consequences Command Pattern Factory Method Description Prototype Pattern Prototype Description Command Description Interpreter with Factory Method Interpreter Implementation Command Structure Factory Method Participants Prototype Participants Factory Method Command Participants Factory Method Consequences Pattern Factory Method Description Interpreter with Factory Method Factory Method Participants Factory Method Consequences ROO,HOM/FHTenL Example calculator design September 25, 2017 2/30
Example calculator Example Design design ROO,HOM Part 1 Interpreter Example calculator design interpreter Calculator Implement a command interpreter. Introduce a set of commands that are Design Rationales Command Pattern accepted during a command-interpreter session. The effect of each Command Description command can vary with the application. Command Structure Command Participants Two special command names are Quit and Undo. These must be accepted. Command Consequences Prototype Pattern Quit stops the session. Undo removes commands from the history of Prototype Description executed commands in reverse chronological order. It notifies the user of Interpreter Implementation Prototype Participants the name of the undone command. The number of executed commands is Factory Method Pattern thus decremented. Factory Method Description Interpreter with Factory Method Factory Method Participants Factory Method Consequences ROO,HOM/FHTenL Example calculator design September 25, 2017 3/30
Example calculator Interpreter Design design ROO,HOM Command Interpreter -set Interpreter Example calculator design Interpreter + name interpreter { hashed } Calculator * Design Rationales + clone() + session () + session () { ordered } * Command Pattern + execute () # getNewCommand () + execute () Command Description -history + undo () Command Structure Command Participants Command Consequences Prototype Pattern Prototype Description Interpreter Implementation Prototype Participants Factory Method Quit Undo Pattern Factory Method Description Interpreter with Factory Method Factory Method Participants Factory Method Consequences ROO,HOM/FHTenL Example calculator design September 25, 2017 4/30
Example calculator Interpreter Implementation design ROO,HOM p u b l i c c l a s s I n t e r p r e t e r { protected Stack h i s t o r y = new Stack ( ) ; Interpreter protected HashTable s e t = new HashTable ( ) ; Example calculator design p u b l i c void s e s s i o n () { interpreter while ( true ) { Calculator Command command = getNewCommand ( ) ; Design Rationales i f (command i n s t a n c e o f Quit ) { Command Pattern return ; } e l s e { Command Description command . execute ( ) ; Command Structure } Command Participants } Command Consequences } Prototype Pattern Prototype Description protected Command getNewCommand () { S t r i n g s = i n p u t . g e t S t r i n g ( ) ; Interpreter Implementation while ( ! s e t . containsKey ( s )) { Prototype Participants d i s p l a y . p r i n t E r r o r ( ”Not a v a l i d command ; t r y again ! ” ) ; Factory Method s = i np u t . g e t S t r i n g ( ) ; Pattern } Factory Method Description return ((Command) s e t . get ( s ) ) . c l o n e ( ) ; Interpreter with Factory } Method } Factory Method Participants Factory Method Consequences ROO,HOM/FHTenL Example calculator design September 25, 2017 5/30
Undo Implementation a b s t r a c t c l a s s Command implements Cloneable { p u b l i c a b s t r a c t void execute ( ) ; p u b l i c a b s t r a c t void undo ( ) ; } c l a s s Undo extends Command { protected Stack h i s t o r y ; p u b l i c Undo ( Stack h ) { h i s t o r y = h ; } p u b l i c void execute () { ((Command) h i s t o r y . top ( ) ) . undo ( ) ; } p u b l i c void undo () {} }
Example calculator Calculator design ROO,HOM Part 2 Interpreter Example calculator design interpreter Calculator Using the interpreter, build a calculator with Reverse Polish Notation Design Rationales (RPN). It uses commands like Command Pattern Command Description accept “n” Command Structure Command Participants * Command Consequences / Prototype Pattern + Prototype Description Interpreter Implementation - Prototype Participants quit Factory Method Pattern undo Factory Method Description The reverse polish notation uses the stack concept for prescribing the order Interpreter with Factory Method Factory Method of executing the commands. Participants Factory Method The calculator commands must be “un-doable”. Consequences ROO,HOM/FHTenL Example calculator design September 25, 2017 7/30
Example calculator Calculator Design design ROO,HOM Command Interpreter history execute() Stack Example calculator design Interpreter * undo() interpreter Calculator Design Rationales Command Pattern valueStack Command Description Stack Command Structure CalculatorCommand Undo Command Participants Client execute() Command Consequences Prototype Pattern Prototype Description Calculator Interpreter Implementation Accept Prototype Participants Factory Method Add execute() Pattern Subtract Factory Method Description execute() Interpreter with Factory Method Divide execute() Factory Method Participants <<create>> Multiply Factory Method execute() Consequences execute() ROO,HOM/FHTenL Example calculator design September 25, 2017 8/30
Example calculator Calculator Implementation design ROO,HOM c l a s s C a l c u l a t o r extends I n t e r p r e t e r { Interpreter p u b l i c C a l c u l a t o r () { Example calculator design interpreter s e t . put ( ” q u i t ” , new Quit ( ) ) ; Calculator s e t . put ( ”undo” , new Undo ( h i s t o r y ) ) ; Design Rationales s e t . put ( ”+” , new Plus ( h i s t o r y ) ) ; Command Pattern s e t . put ( ” − ” , new Min ( h i s t r o y ) ) ; Command Description s e t . put ( ” ∗ ” , new Mul ( h i s t o r y ) ) ; Command Structure s e t . put ( ”/” , new Div ( h i s t o r y ) ) ; Command Participants Command Consequences s e t . put ( ” accept ” , new Accept ( h i s t o r y ) ) ; Prototype Pattern } Prototype Description } Interpreter Implementation Prototype Participants Factory Method Pattern Factory Method Description Interpreter with Factory Method Factory Method Participants Factory Method Consequences ROO,HOM/FHTenL Example calculator design September 25, 2017 9/30
Example calculator Design Rationales design ROO,HOM Use specialization of abstract classes to avoid unnecessary coupling between Interpreter classes Example calculator design interpreter Obtain extendibility with regard the type of requested commands Calculator Design Rationales Obtain reusability of certain complex or carefully optimized algorithms Command Pattern Command Description Obtain a readable and communicable design through proper separation of Command Structure Command Participants concerns Command Consequences Prototype Pattern Prototype Description The essence of this can be captured in a design pattern Interpreter Implementation Prototype Participants Factory Method Pattern Factory Method Description Interpreter with Factory Method Factory Method Participants Factory Method Consequences ROO,HOM/FHTenL Example calculator design September 25, 2017 10/30
Example calculator Command design ROO,HOM Classification : Behavioral Interpreter Intent: Encapsulate a request as an object, thereby letting you parametrise Example calculator design interpreter clients with different requests, queue or log requests, and support un-doable Calculator Design Rationales operations. Command Pattern Motivation: Sometimes it’s necessary to issue requests to objects without Command Description Command Structure knowing anything about the operation being requested or the receiver of the Command Participants Command Consequences request. Prototype Pattern E.g. user interface toolkits include objects that carry out a user request like Prototype Description Interpreter Implementation buttons Prototype Participants menus in response to user input. Factory Method Pattern The toolkit can’t implement the request explicitly because only applications Factory Method Description know what should be done on. Interpreter with Factory Method Factory Method Participants Factory Method Consequences ROO,HOM/FHTenL Example calculator design September 25, 2017 11/30
Recommend
More recommend