Example calculator design
- Dr. Onno van Roosmalen
Example calculator design Dr. Onno van Roosmalen L A T EX version: - - PowerPoint PPT Presentation
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
Example calculator design ROO,HOM Interpreter
Example calculator design interpreter Calculator Design Rationales
Command Pattern
Command Description Command Structure Command Participants Command Consequences
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 2/30
Example calculator design ROO,HOM Interpreter
Example calculator design interpreter Calculator Design Rationales
Command Pattern
Command Description Command Structure Command Participants Command Consequences
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 3/30
Example calculator design ROO,HOM Interpreter
Example calculator design interpreter Calculator Design Rationales
Command Pattern
Command Description Command Structure Command Participants Command Consequences
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 4/30
Example calculator design ROO,HOM Interpreter
Example calculator design interpreter Calculator Design Rationales
Command Pattern
Command Description Command Structure Command Participants Command Consequences
Prototype Pattern
Prototype Description Interpreter Implementation Prototype Participants
Factory Method Pattern
Factory Method Description Interpreter with Factory Method Factory Method Participants Factory Method Consequences
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 ( ) ; protected HashTable s e t = new HashTable ( ) ; p u b l i c void s e s s i o n () { while ( true ) { Command command = getNewCommand ( ) ; i f (command i n s t a n c e o f Quit ) { return ; } e l s e { command . execute ( ) ; } } } protected Command getNewCommand () { S t r i n g s = i n p u t . g e t S t r i n g ( ) ; while ( ! s e t . containsKey ( s )) { 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 ! ” ) ; s = i np u t . g e t S t r i n g ( ) ; } return ((Command) s e t . get ( s ) ) . c l o n e ( ) ; } }
ROO,HOM/FHTenL Example calculator design September 25, 2017 5/30
Example calculator design ROO,HOM Interpreter
Example calculator design interpreter Calculator Design Rationales
Command Pattern
Command Description Command Structure Command Participants Command Consequences
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 7/30
Example calculator design ROO,HOM Interpreter
Example calculator design interpreter Calculator Design Rationales
Command Pattern
Command Description Command Structure Command Participants Command Consequences
Prototype Pattern
Prototype Description Interpreter Implementation Prototype Participants
Factory Method Pattern
Factory Method Description Interpreter with Factory Method Factory Method Participants Factory Method Consequences
Add execute() Subtract execute() Divide execute() Accept execute() Multiply execute() CalculatorCommand Stack valueStack Client Calculator <<create>> Undo execute() Interpreter Command execute() Stack history
ROO,HOM/FHTenL Example calculator design September 25, 2017 8/30
Example calculator design ROO,HOM Interpreter
Example calculator design interpreter Calculator Design Rationales
Command Pattern
Command Description Command Structure Command Participants Command Consequences
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 ROO,HOM Interpreter
Example calculator design interpreter Calculator Design Rationales
Command Pattern
Command Description Command Structure Command Participants Command Consequences
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 10/30
Example calculator design ROO,HOM Interpreter
Example calculator design interpreter Calculator Design Rationales
Command Pattern
Command Description Command Structure Command Participants Command Consequences
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 11/30
Example calculator design ROO,HOM Interpreter
Example calculator design interpreter Calculator Design Rationales
Command Pattern
Command Description Command Structure Command Participants Command Consequences
Prototype Pattern
Prototype Description Interpreter Implementation Prototype Participants
Factory Method Pattern
Factory Method Description Interpreter with Factory Method Factory Method Participants Factory Method Consequences
Client ConcreteCommand State Execute() Receiver Action() receiver Command Execute() Invoker receiver.Action() <<create>>
ROO,HOM/FHTenL Example calculator design September 25, 2017 12/30
Example calculator design ROO,HOM Interpreter
Example calculator design interpreter Calculator Design Rationales
Command Pattern
Command Description Command Structure Command Participants Command Consequences
Prototype Pattern
Prototype Description Interpreter Implementation Prototype Participants
Factory Method Pattern
Factory Method Description Interpreter with Factory Method Factory Method Participants Factory Method Consequences
Add Execute() Subtract Execute() Divide Execute() Accept Execute() Multiply Execute() CalculatorCommand Stack valueStack Client Calculator <<create>> Undo Execute() Interpreter Command Execute() Stack history * Undo()
<<invoker>> <<Receiver>> <<Command>> <<ConcreteCommand>> ROO,HOM/FHTenL Example calculator design September 25, 2017 13/30
Example calculator design ROO,HOM Interpreter
Example calculator design interpreter Calculator Design Rationales
Command Pattern
Command Description Command Structure Command Participants Command Consequences
Prototype Pattern
Prototype Description Interpreter Implementation Prototype Participants
Factory Method Pattern
Factory Method Description Interpreter with Factory Method Factory Method Participants Factory Method Consequences
Add Execute() Subtract Execute() Divide Execute() Accept Execute() Multiply Execute() CalculatorCommand Stack valueStack Client Calculator <<create>> Undo Execute() Interpreter Command Execute() Stack history * Undo()
<<invoker>> <<Command>> <<Receiver>> <<ConcreteCommand>> ROO,HOM/FHTenL Example calculator design September 25, 2017 13/30
Example calculator design ROO,HOM Interpreter
Example calculator design interpreter Calculator Design Rationales
Command Pattern
Command Description Command Structure Command Participants Command Consequences
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 14/30
Example calculator design ROO,HOM Interpreter
Example calculator design interpreter Calculator Design Rationales
Command Pattern
Command Description Command Structure Command Participants Command Consequences
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 15/30
Example calculator design ROO,HOM Interpreter
Example calculator design interpreter Calculator Design Rationales
Command Pattern
Command Description Command Structure Command Participants Command Consequences
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 16/30
Example calculator design ROO,HOM Interpreter
Example calculator design interpreter Calculator Design Rationales
Command Pattern
Command Description Command Structure Command Participants Command Consequences
Prototype Pattern
Prototype Description Interpreter Implementation Prototype Participants
Factory Method Pattern
Factory Method Description Interpreter with Factory Method Factory Method Participants Factory Method Consequences
ConcreteImplementorA OperationImp() ConcreteImplementorB OperationImp() RefinedAbstraction1 RefinedAbstraction2 Imp.OperationImp() Implementor Omp() Client Abstraction Operation() ConcreteImplementorA OperationImp() ConcreteImplementorB OperationImp() RefinedAbstraction1 RefinedAbstraction2 Imp.OperationImp() Implementor OperationImp() Client Abstraction Operation() imp AbstractFactory Mntor() AbstractFactory MakeImplementor1() * MakeImplementor2() ConcreteFactoryA MakeIr() ConcreteFactoryB Mator() ConcreteFactoryA MakeImplementor1() ConcreteFactoryB MakeImplementor1() MakeImplementor2() MakeImplementor2()
ROO,HOM/FHTenL Example calculator design September 25, 2017 17/30
Example calculator design ROO,HOM Interpreter
Example calculator design interpreter Calculator Design Rationales
Command Pattern
Command Description Command Structure Command Participants Command Consequences
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 18/30
Example calculator design ROO,HOM Interpreter
Example calculator design interpreter Calculator Design Rationales
Command Pattern
Command Description Command Structure Command Participants Command Consequences
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 20/30
Example calculator design ROO,HOM Interpreter
Example calculator design interpreter Calculator Design Rationales
Command Pattern
Command Description Command Structure Command Participants Command Consequences
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 21/30
Example calculator design ROO,HOM Interpreter
Example calculator design interpreter Calculator Design Rationales
Command Pattern
Command Description Command Structure Command Participants Command Consequences
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 22/30
Example calculator design ROO,HOM Interpreter
Example calculator design interpreter Calculator Design Rationales
Command Pattern
Command Description Command Structure Command Participants Command Consequences
Prototype Pattern
Prototype Description Interpreter Implementation Prototype Participants
Factory Method Pattern
Factory Method Description Interpreter with Factory Method Factory Method Participants Factory Method Consequences
1
2
ROO,HOM/FHTenL Example calculator design September 25, 2017 23/30
Example calculator design ROO,HOM Interpreter
Example calculator design interpreter Calculator Design Rationales
Command Pattern
Command Description Command Structure Command Participants Command Consequences
Prototype Pattern
Prototype Description Interpreter Implementation Prototype Participants
Factory Method Pattern
Factory Method Description Interpreter with Factory Method Factory Method Participants Factory Method Consequences
Add Execute() Subtract Execute() Divide Execute() Accept Execute() Multiply Execute() d Command Execute() Add execute() Subtract execute() Divide execute() Accept execute() Multiply execute() CalculatorCommand Client Client <<create>> Command execute()
*
Interpreter Interpreter Session() getNewCommand() Interpreter Calculator Calculator() getNewCommand()
ROO,HOM/FHTenL Example calculator design September 25, 2017 24/30
Example calculator design ROO,HOM Interpreter
Example calculator design interpreter Calculator Design Rationales
Command Pattern
Command Description Command Structure Command Participants Command Consequences
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 25/30
Example calculator design ROO,HOM Interpreter
Example calculator design interpreter Calculator Design Rationales
Command Pattern
Command Description Command Structure Command Participants Command Consequences
Prototype Pattern
Prototype Description Interpreter Implementation Prototype Participants
Factory Method Pattern
Factory Method Description Interpreter with Factory Method Factory Method Participants Factory Method Consequences
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 { C a l c u l a t o r () { s e t . put ( ” q u i t ” , new Quit ( ) ) ; s e t . put ( ”undo” , new Undo ( h i s t o r y ) ) ; s e t . put ( ” p l u s ” , new Plus ( h i s t o r y ) ) ; s e t . put ( ”min” , new Min ( h i s t r o y ) ) ; s e t . put ( ”mul” , new Mul ( h i s t o r y ) ) ; s e t . put ( ” d i v ” , new Div ( h i s t o r y ) ) ; s e t . put ( ” @accept ” ,new Accept ( h i s t o r y ) ) ; } protected Command getNewCommand () { S t r i n g s = i n p u t . g e t S t r i n g ( ) ; while ( ! s e t . containsKey ( s )) { 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 ! ” ) ; s = i np u t . g e t S t r i n g ( ) ; } return ((Command) s e t . get ( s ) ) . c l o n e ( ) ; } }
ROO,HOM/FHTenL Example calculator design September 25, 2017 26/30
Example calculator design ROO,HOM Interpreter
Example calculator design interpreter Calculator Design Rationales
Command Pattern
Command Description Command Structure Command Participants Command Consequences
Prototype Pattern
Prototype Description Interpreter Implementation Prototype Participants
Factory Method Pattern
Factory Method Description Interpreter with Factory Method Factory Method Participants Factory Method Consequences
Product ConcreteProduct Creator anOperation() o factoryMethod() ConcreteCreator factoryMethod() o <<create>> return new ConcreteProduct() product = factoryMethod()
ROO,HOM/FHTenL Example calculator design September 25, 2017 27/30
Example calculator design ROO,HOM Interpreter
Example calculator design interpreter Calculator Design Rationales
Command Pattern
Command Description Command Structure Command Participants Command Consequences
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 28/30
Example calculator design ROO,HOM Interpreter
Example calculator design interpreter Calculator Design Rationales
Command Pattern
Command Description Command Structure Command Participants Command Consequences
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 29/30
Example calculator design ROO,HOM Interpreter
Example calculator design interpreter Calculator Design Rationales
Command Pattern
Command Description Command Structure Command Participants Command Consequences
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 30/30