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 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 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 { 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 ( ) { interpreter w h i l e ( t r u e ) { Calculator Command command = getNewCommand(); Design Rationales i f ( command i n s t a n c e o f Quit ) { Command Pattern r e t u r n ; } e l s e { Command Description command. execute (); Command Structure } Command Participants } Command Consequences } Prototype Pattern p r o t e c t e d Command getNewCommand() { Prototype Description String s = input.getString(); Interpreter Implementation w h i l e (! set.containsKey(s)) { 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 } r e t u r n ((Command)set .get(s)). clone (); Factory Method Description } 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 class Command implements C l o n e a b l e { p u b l i c abstract void e x e c u t e ( ) ; p u b l i c abstract void undo ( ) ; }

c l a s s Undo extends Command { p r o t e c t e d Stack history; p u b l i c Undo(Stack h) { h i s t o r y = h ; } p u b l i c void e x e c u t e ( ) { ((Command) history .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 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 execute() Example calculator design Interpreter Stack * undo() interpreter 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 <> 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 { Interpreter p u b l i c Calculator() { 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 } 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 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() <> 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

<> Command Calculator Design Rationales

Command Pattern <> <> Command Description Command Structure Command Command Participants history Execute() Interpreter Stack Command Consequences * Undo() Prototype Pattern Prototype Description <> Interpreter Implementation valueStack Stack Undo CalculatorCommand Prototype Participants Client Execute() Factory Method Pattern Calculator Factory Method Description Accept Interpreter with Factory Method Execute() Add Factory Method Subtract Participants Execute() Factory Method Execute() Divide Consequences <> Multiply Execute() Execute()

ROO,HOM/FHTenL Example calculator design September 25, 2017 13/30 Example calculator Two command instances in calculator design ROO,HOM

Interpreter Example calculator design interpreter

<> Command Calculator Design Rationales

Command Pattern <> Command Description Command Structure Command Command Participants history Execute() Interpreter Stack * Command Consequences Undo() <> Prototype Pattern Prototype Description Interpreter Implementation valueStack Stack Undo CalculatorCommand Prototype Participants Client Execute() Factory Method <> Pattern Calculator Factory Method Description Accept Interpreter with Factory Method Execute() Add Factory Method Subtract Participants Execute() Factory Method Execute() Divide Consequences <> Multiply Execute() Execute()

ROO,HOM/FHTenL Example calculator design September 25, 2017 13/30 Example calculator Reflections design ROO,HOM The class names in the pattern reflect roles of classes played. The pattern is Interpreter a meta-description of the design. Example calculator design A mapping must be found: interpreter Calculator Concrete command ← Mul, Min, Design Rationales Client ← Calculator Command Pattern Invoke ← Interpreter Command Description Command Structure Receive ← Stack Command Participants Command Consequences

The command interpreter implementation contains several Prototype Pattern (as we will see later). 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 Command design ROO,HOM Participants: Interpreter Command Example calculator design interpreter declares an interface for executing an operation Calculator ConcreteCommand Design Rationales defines a binding between a receiver object and an action Command Pattern Command Description implements Execute() by invoking operation(s) on Receiver Command Structure Command Participants Client Command Consequences

creates a ConcreteCommand object and sets its receiver Prototype Pattern Invoker Prototype Description Interpreter Implementation asks the command to carry out a request. Prototype Participants Factory Method Receiver Pattern knows how to perform the operation associated with carrying out a request. 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 Command design ROO,HOM Consequences Interpreter Command de-couples the object that invokes the operation from the one that Example calculator design interpreter knows how to perform it. Calculator Commands are first class objects. They can be manipulated and extended like Design Rationales any other object. Command Pattern Command Description You can assemble commands into a composite command to obtain a form of Command Structure macro’s. (This can be achieved wit the ). Command Participants Command Consequences It is easy to add new commands. Only the creating client needs to be adapted. 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 Combining Patterns design ROO,HOM

Interpreter Example calculator design AbstractFactoryAbstractFactory interpreter Calculator MakeImplementor1() Design Rationales MakeImplementor2()Mntor() Command Pattern Command Description Command Structure Command Participants ConcreteFactoryAConcreteFactoryA ConcreteFactoryBConcreteFactoryB ClientClient Command Consequences MakeImplementor1() MakeImplementor1() Prototype Pattern MakeImplementor2()MakeIr() MakeImplementor2()Mator() Prototype Description Interpreter Implementation * Abstraction imp Implementor Abstraction Implementor Prototype Participants

Operation()Operation() OperationImp()Omp() Factory Method Pattern Imp.OperationImp()Imp.OperationImp() Factory Method Description Interpreter with Factory Method Factory Method Participants Factory Method ConcreteImplementorA ConcreteImplementorB RefinedAbstraction1RefinedAbstraction1 RefinedAbstraction2RefinedAbstraction2 ConcreteImplementorA ConcreteImplementorB Consequences

OperationImp()OperationImp() OperationImp()OperationImp()

ROO,HOM/FHTenL Example calculator design September 25, 2017 17/30 Example calculator Prototype design ROO,HOM Classification: Creational Interpreter Example calculator design Intent: Specify the kind of objects to create using a prototypical instance, interpreter and creating new objects by copying this prototype. Calculator Design Rationales

Motivation: Command Pattern Command Description In the command-interpreter example, a prototype of each command was Command Structure stored in a table. Command Participants Command Consequences The table provides a link between the strings that can be typed by the user Prototype Pattern and the actual command objects. Prototype Description Because a history is kept, with each command issued, even of the same type, Interpreter Implementation Prototype Participants

state information has to be retained. Factory Method Hence a new command-object has to be created at each command issued by Pattern Factory Method Description the user. Interpreter with Factory Method The command retrieved from the table is copied, to keep the interpreter Factory Method Participants independent from specific command classes. Factory Method Consequences

ROO,HOM/FHTenL Example calculator design September 25, 2017 18/30 Interpreter Implementation The Interpreter.getcommand() method returns a clone of one of the members of the command set. p r o t e c t e d Command getNewCommand() { String s = input.getString(); w h i l e (! set.containsKey(s)) { display.printError(”Not a v a l i d command; t r y a g a i n!”); s = input.getString(); } r e t u r n ((Command)set .get(s)). clone (); } Example calculator Prototype design ROO,HOM Prototype Pattern structure Interpreter Example calculator design prototype interpreter ClientClient Prototype Calculator Design Rationales Operation()n() Clone()Clone() Command Pattern Command Description p=prototype.Clone()p=prototype.Clone() Command Structure Command Participants Command Consequences

Prototype Pattern Prototype Description ConcretePrototype1ConcretePe1 ConcretePrototype2ConcretePrototype2 Interpreter Implementation Prototype Participants clone()Clone() clone()Clone() Factory Method Pattern Factory Method Description Interpreter with Factory Method returnreturn copycopy ofof selfself returnreturn copycopy ofof selfself Factory Method Participants Factory Method Consequences

ROO,HOM/FHTenL Example calculator design September 25, 2017 20/30 Example calculator Prototype design ROO,HOM Participants: Interpreter Prototype: Example calculator design interpreter declares an interface for cloning itself : Calculator Concrete prototype: Design Rationales implements an operation by cloning itself. Command Pattern Command Description Client: Command Structure Command Participants Only couples to abstract Prototype to obtain clones of concrete versions. 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 Prototype design ROO,HOM Instance Interpreter Prototype ← Command Example calculator design interpreter Clone() ← clone() Calculator Concrete prototype ← Plus, Minus, . . . Design Rationales Client ← Interpreter Command Pattern Command Description Operation() ← getNewCommand Command Structure Command Participants Prevents coupling Command Consequences The Concrete Prototype class is invisible to the client 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 Factory Method design ROO,HOM Classification: Creational Interpreter Intent: Define an interface for creating an object, but let subclasses decide Example calculator design interpreter which class to instantiate. Factory method lets a class defer instantiation to Calculator subclasses. Design Rationales Command Pattern Motivation: In the command interpreter example we have a class Command Description (Interpreter) that deals with objects that are subclass-instances of a single Command Structure Command Participants interface (Command). Command Consequences We do not want to have the interpreter know all these subclasses. (We want Prototype Pattern Prototype Description to decouple the class from these concrete command classes for reuse of the Interpreter Implementation Interpreter class). Prototype Participants We want to defer creation of the concrete commands to subclasses of the Factory Method Pattern interpreter for two reasons Factory Method Description Interpreter with Factory 1 Only this subclass knows the ConcreteCommand classes Method 2 Factory Method This subclass must determine the algorithm by which the concrete commands Participants are created Factory Method Consequences

ROO,HOM/FHTenL Example calculator design September 25, 2017 23/30 Example calculator Factory Method example design ROO,HOM Example Interpreter Example calculator design interpreter Interpreter Calculator Design Rationales Interpreter CommandCommand Session() * execute()Execute() Command Pattern getNewCommand() Command Description Command Structure Command Participants Command Consequences

CalculatorCommandd Prototype Pattern Prototype Description ClientClient Calculator Interpreter Implementation Prototype Participants Interpreter Calculator() Factory Method getNewCommand() AcceptAccept Pattern Factory Method Description Add execute()Execute() Add Interpreter with Factory Method SubtractSubtract Factory Method execute()Execute() Participants DivideDivide Factory Method execute()Execute() Consequences <> MultiplyMultiply execute()Execute() execute()Execute()

ROO,HOM/FHTenL Example calculator design September 25, 2017 24/30 Example calculator Interpreter with Factory Method design ROO,HOM a b s t r a c t class Interpreter { 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 interpreter Calculator p u b l i c void s e s s i o n ( ) { Design Rationales w h i l e ( t r u e ) { Command Pattern Command command = getNewCommand(); Command Description i f ( command i n s t a n c e o f Quit ) { Command Structure r e t u r n ; Command Participants } e l s e { Command Consequences command. execute (); Prototype Pattern } Prototype Description Interpreter Implementation } Prototype Participants } Factory Method Pattern a b s t r a c t protected Command getNewCommand (); 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 Calculator with Factory Method design ROO,HOM c l a s s C a l c u l a t o r extends Interpreter { Calculator() { Interpreter s e t . put (”quit”, new Quit ( ) ) ; s e t . put (”undo”, new Undo(history )); Example calculator design s e t . put (”plus”, new Plus(history )); interpreter s e t . put (”min”, new Min(histroy) ); Calculator s e t . put (”mul”, new Mul(history) ); Design Rationales s e t . put (”div”, Command Pattern new Div(history) ); s e t . put (”@accept”, new Accept(history )); Command Description } Command Structure p r o t e c t e d Command getNewCommand() { Command Participants String s = input.getString(); Command Consequences w h i l e (! set.containsKey(s)) { Prototype Pattern display.printError(”Not a v a l i d command; t r y a g a i n!”); s = input.getString(); Prototype Description } Interpreter Implementation r e t u r n ((Command)set .get(s)). clone (); 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 26/30 Example calculator Factory Method design ROO,HOM Structure Interpreter Example calculator design product = factoryMethod() interpreter Calculator Design Rationales

Command Pattern Command Description Creator Command Structure Product Command Participants anOperation() o Command Consequences factoryMethod() Prototype Pattern Prototype Description Interpreter Implementation Prototype Participants

Factory Method Pattern ConcreteCreator <> ConcreteProduct Factory Method Description Interpreter with Factory factoryMethod() o Method Factory Method Participants Factory Method Consequences

return new ConcreteProduct()

ROO,HOM/FHTenL Example calculator design September 25, 2017 27/30 Example calculator Factory Method design ROO,HOM Participants: Interpreter Product: Example calculator design interpreter defines the interface of objects the factory method creates. Calculator ConcreteProduct: Design Rationales implements the product interface. Command Pattern Command Description Creator: Command Structure Command Participants declares the factory method, which returns an object of type Product. Creator Command Consequences may also define a default implementation of the factory method that returns a Prototype Pattern default ConcreteProduct object. Prototype Description Interpreter Implementation may call the factory method to create Product objects. Prototype Participants

ConcreteCreator: Factory Method Pattern Overrides the factory method to return an instance of a ConcreteProduct Factory Method Description Interpreter with Factory Applicability: Method Factory Method Use when a class can’t anticipate the class of objects it must create. Participants Factory Method A class wants its subclasses to specify the objects it wants to create. Consequences Classes delegate responsibility to one of several subclasses, and you want to localize the knowledge of such helper subclass is the delegate.

ROO,HOM/FHTenL Example calculator design September 25, 2017 28/30 Example calculator Factory Method design ROO,HOM Prevents coupling: Interpreter Factory method avoids having to specify the ConcreteProduct creations Example calculator design interpreter The factory method defers decisions on the creation algorithm Calculator Avoids coupling between Creator and the concrete product classes Design Rationales Command Pattern Consequences: Command Description Provides hooks for subclasses.The factory principle (hiding creation in factory Command Structure Command Participants methods (either using abstract factory or factory method) adds flexibility to a Command Consequences design. Prototype Pattern Prototype Description (Note that the AbstractFactory class uses factory methods. It is a class with Interpreter Implementation a factory method for each type of abstract product it is supposed to deliver.) Prototype Participants Connects parallel class hierarchies. 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 Factory Method design ROO,HOM Note on the example Interpreter In the given interpreter/calculator example Prototype and Factory Method are Example calculator design interpreter combined. Prototype is used to dynamically decide which object must be Calculator Design Rationales

created (using dynamic binding rather than explicit selection based on the string Command Pattern entered by the user). Factory Method is used to vary the algorithm by which Command Description Command Structure Command objects are created with the subtype of Interpreter( e.g. a using Command Participants graphical interface for the calculator may change this algorithm). Command Consequences Prototype Pattern Prototype Description A factory method is not required to create a new instance. It may decide to use Interpreter Implementation an existing, if the situation warrants it. See for instance Prototype Participants Factory Method java.lang.Integer.valueOf(int). 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