
Example calculator design Dr. Onno van Roosmalen LATEX 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 Interpreter Implementation executed commands in reverse chronological order. It notifies the user of 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 Example calculator design InterpreterInterpreter + name interpreter { hashed } Calculator * Design Rationales ++ sessionsession ()() { ordered } * + clone() Command Pattern # getNewCommand () ++ executeexecute ()() Command Description -history Command Structure + undo () 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 class Interpreter f p r o t e c t e d Stack history = new Stack ( ) ; Interpreter p r o t e c t e d HashTable set = new HashTable (); Example calculator design p u b l i c void s e s s i o n ( ) f interpreter w h i l e ( t r u e ) f Calculator Command command = getNewCommand(); Design Rationales i f ( command i n s t a n c e o f Quit ) f Command Pattern r e t u r n ; g e l s e f Command Description command. execute (); Command Structure g Command Participants g Command Consequences g Prototype Pattern p r o t e c t e d Command getNewCommand() f Prototype Description String s = input.getString(); Interpreter Implementation w h i l e (! set.containsKey(s)) f Prototype Participants display.printError(”Not a v a l i d command; t r y a g a i n!”); Factory Method s = input.getString(); Pattern g r e t u r n ((Command)set .get(s)). clone (); Factory Method Description g Interpreter with Factory Method g 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 class Command implements C l o n e a b l e f p u b l i c abstract void e x e c u t e ( ) ; p u b l i c abstract void undo ( ) ; g c l a s s Undo extends Command f p r o t e c t e d Stack history; p u b l i c Undo(Stack h) f h i s t o r y = h ; g p u b l i c void e x e c u t e ( ) f ((Command) history .top()).undo(); g p u b l i c void undo ( ) fg g 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 undo Pattern Factory Method Description Interpreter with Factory The reverse polish notation uses the stack concept for prescribing the order 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 Stack execute() Example calculator design Interpreter * interpreter undo() Calculator Design Rationales Command Pattern valueStack Command Description Stack Command Structure Undo CalculatorCommand Command Participants Client execute() Command Consequences Prototype Pattern Prototype Description Calculator Interpreter Implementation Accept Prototype Participants Factory Method execute() Add Pattern Subtract Factory Method Description execute() Interpreter with Factory Method execute() Divide Factory Method <<create>> Participants 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 Interpreter f Interpreter p u b l i c Calculator() f Example calculator design interpreter s e t . put (”quit”, new Quit ( ) ) ; Calculator s e t . put (”undo”, new Undo(history )); Design Rationales s e t . put (”+”, new Plus(history )); Command Pattern s e t . put (” −”, new Min(histroy) ); Command Description s e t . put (” ∗”, new Mul(history) ); Command Structure s e t . put (”/”, new Div(history) ); Command Participants s e t . put (”accept”, new Accept(history )); Command Consequences g Prototype Pattern g 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 Example calculator design Intent: Encapsulate a request as an object, thereby letting you parametrise interpreter clients with different requests, queue or log requests, and support un-doable Calculator Design Rationales operations. Command Pattern Command Description Motivation: Sometimes it’s necessary to issue requests to objects without Command Structure knowing anything about the operation being requested or the receiver of the Command Participants request. Command Consequences 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 The toolkit can’t implement the request explicitly because only applications Pattern 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 Example calculator Command design ROO,HOM Structure Interpreter Example calculator design interpreter Calculator Command Client Invoker Design Rationales Command Pattern Execute() Command Description Command Structure Command Participants Command Consequences Receiver receiver ConcreteCommand Prototype Pattern State Action() receiver.Action() Prototype Description Interpreter Implementation Execute() <<create>> 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 12/30 Example calculator Two command instances in calculator design ROO,HOM Interpreter Example calculator design interpreter <<invoker>> Command Calculator Design Rationales Command Pattern <<Receiver>> <<Command>> Command Description Command Structure Command Command Participants history Execute() Interpreter Stack Command Consequences
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages31 Page
-
File Size-