Example calculator design Dr. Onno van Roosmalen L A T EX version: - - PowerPoint PPT Presentation

example calculator design
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Example calculator design

  • Dr. Onno van Roosmalen

L

AT

EX version: Pieter van den Hombergh

Fontys Hogeschool voor Techniek en Logistiek

September 25, 2017

slide-2
SLIDE 2

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

Content

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

slide-3
SLIDE 3

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

Example Design

Part 1 Implement a command interpreter. Introduce a set of commands that are accepted during a command-interpreter session. The effect of each command can vary with the application. Two special command names are Quit and Undo. These must be accepted. Quit stops the session. Undo removes commands from the history of executed commands in reverse chronological order. It notifies the user of the name of the undone command. The number of executed commands is thus decremented.

ROO,HOM/FHTenL Example calculator design September 25, 2017 3/30

slide-4
SLIDE 4

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

Interpreter Design

+ execute () Interpreter + session () Command + name + clone() + execute () + undo () Interpreter + session () # getNewCommand ()

  • set
  • history

Quit Undo { hashed } { ordered } * *

ROO,HOM/FHTenL Example calculator design September 25, 2017 4/30

slide-5
SLIDE 5

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

Interpreter Implementation

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

slide-6
SLIDE 6

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 () {} }

slide-7
SLIDE 7

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

Calculator

Part 2 Using the interpreter, build a calculator with Reverse Polish Notation (RPN). It uses commands like

accept “n” * / +

  • quit

undo

The reverse polish notation uses the stack concept for prescribing the order

  • f executing the commands.

The calculator commands must be “un-doable”.

ROO,HOM/FHTenL Example calculator design September 25, 2017 7/30

slide-8
SLIDE 8

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

Calculator Design

Add execute() Subtract execute() Divide execute() Accept execute() Multiply execute() CalculatorCommand Stack valueStack Client Calculator <<create>> Undo execute() Interpreter Command execute() Stack history

* undo()

ROO,HOM/FHTenL Example calculator design September 25, 2017 8/30

slide-9
SLIDE 9

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

Calculator Implementation

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 { p u b l i c 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 ( ”+” , new Plus ( h i s t o r y ) ) ; s e t . put ( ”−” , new Min ( h i s t r o y ) ) ; s e t . put ( ”∗” , new Mul ( h i s t o r y ) ) ; s e t . put ( ”/” , new Div ( h i s t o r y ) ) ; s e t . put ( ” accept ” ,new Accept ( h i s t o r y ) ) ; } }

ROO,HOM/FHTenL Example calculator design September 25, 2017 9/30

slide-10
SLIDE 10

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

Design Rationales

Use specialization of abstract classes to avoid unnecessary coupling between classes Obtain extendibility with regard the type of requested commands Obtain reusability of certain complex or carefully optimized algorithms Obtain a readable and communicable design through proper separation of concerns

The essence of this can be captured in a design pattern

ROO,HOM/FHTenL Example calculator design September 25, 2017 10/30

slide-11
SLIDE 11

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

Command

Classification: Behavioral Intent: Encapsulate a request as an object, thereby letting you parametrise clients with different requests, queue or log requests, and support un-doable

  • perations.

Motivation: Sometimes it’s necessary to issue requests to objects without knowing anything about the operation being requested or the receiver of the request.

E.g. user interface toolkits include objects that carry out a user request like

buttons menus in response to user input.

The toolkit can’t implement the request explicitly because only applications know what should be done on.

ROO,HOM/FHTenL Example calculator design September 25, 2017 11/30

slide-12
SLIDE 12

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

Command

Structure

Client ConcreteCommand State Execute() Receiver Action() receiver Command Execute() Invoker receiver.Action() <<create>>

ROO,HOM/FHTenL Example calculator design September 25, 2017 12/30

slide-13
SLIDE 13

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

Two command instances in calculator

Add Execute() Subtract Execute() Divide Execute() Accept Execute() Multiply Execute() CalculatorCommand Stack valueStack Client Calculator <<create>> Undo Execute() Interpreter Command Execute() Stack history * Undo()

Command

<<invoker>> <<Receiver>> <<Command>> <<ConcreteCommand>> ROO,HOM/FHTenL Example calculator design September 25, 2017 13/30

slide-14
SLIDE 14

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

Two command instances in calculator

Add Execute() Subtract Execute() Divide Execute() Accept Execute() Multiply Execute() CalculatorCommand Stack valueStack Client Calculator <<create>> Undo Execute() Interpreter Command Execute() Stack history * Undo()

Command

<<invoker>> <<Command>> <<Receiver>> <<ConcreteCommand>> ROO,HOM/FHTenL Example calculator design September 25, 2017 13/30

slide-15
SLIDE 15

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

Reflections

The class names in the pattern reflect roles of classes played. The pattern is a meta-description of the design.

A mapping must be found:

Concrete command ← Mul, Min, Client ← Calculator Invoke ← Interpreter Receive ← Stack

The command interpreter implementation contains several design patterns (as we will see later).

ROO,HOM/FHTenL Example calculator design September 25, 2017 14/30

slide-16
SLIDE 16

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

Command

Participants:

Command

declares an interface for executing an operation

ConcreteCommand

defines a binding between a receiver object and an action implements Execute() by invoking operation(s) on Receiver

Client

creates a ConcreteCommand object and sets its receiver

Invoker

asks the command to carry out a request.

Receiver

knows how to perform the operation associated with carrying out a request.

ROO,HOM/FHTenL Example calculator design September 25, 2017 15/30

slide-17
SLIDE 17

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

Command

Consequences

Command de-couples the object that invokes the operation from the one that knows how to perform it. Commands are first class objects. They can be manipulated and extended like any other object. You can assemble commands into a composite command to obtain a form of macro’s. (This can be achieved wit the composite pattern). It is easy to add new commands. Only the creating client needs to be adapted.

ROO,HOM/FHTenL Example calculator design September 25, 2017 16/30

slide-18
SLIDE 18

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

Combining Patterns

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

slide-19
SLIDE 19

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

Prototype

Classification: Creational Intent: Specify the kind of objects to create using a prototypical instance, and creating new objects by copying this prototype. Motivation:

In the command-interpreter example, a prototype of each command was stored in a table. The table provides a link between the strings that can be typed by the user and the actual command objects. Because a history is kept, with each command issued, even of the same type, state information has to be retained. Hence a new command-object has to be created at each command issued by the user. The command retrieved from the table is copied, to keep the interpreter independent from specific command classes.

ROO,HOM/FHTenL Example calculator design September 25, 2017 18/30

slide-20
SLIDE 20

Interpreter Implementation

The Interpreter.getcommand() method returns a clone of one of the members of the command set.

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 n p u t . g e t S t r i n g ( ) ; } return ((Command) s e t . get ( s ) ) . c l o n e ( ) ; }

slide-21
SLIDE 21

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

Prototype

Prototype Pattern structure

ConcretePe1 Clone() ConcretePrototype2 Clone() Client n() Clone() p=prototype.Clone() return copy of self return copy of self ConcretePrototype1 clone() ConcretePrototype2 clone() Client Operation() Prototype Clone() prototype p=prototype.Clone() return copy of self return copy of self

ROO,HOM/FHTenL Example calculator design September 25, 2017 20/30

slide-22
SLIDE 22

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

Prototype

Participants:

Prototype:

declares an interface for cloning itself :

Concrete prototype:

implements an operation by cloning itself.

Client:

Only couples to abstract Prototype to obtain clones of concrete versions.

ROO,HOM/FHTenL Example calculator design September 25, 2017 21/30

slide-23
SLIDE 23

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

Prototype

Instance

Prototype ← Command

Clone() ← clone()

Concrete prototype ← Plus, Minus, . . . Client ← Interpreter

Operation() ← getNewCommand

Prevents coupling

The Concrete Prototype class is invisible to the client

ROO,HOM/FHTenL Example calculator design September 25, 2017 22/30

slide-24
SLIDE 24

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

Factory Method

Classification: Creational Intent: Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory method lets a class defer instantiation to subclasses. Motivation: In the command interpreter example we have a class (Interpreter) that deals with objects that are subclass-instances of a single interface (Command).

We do not want to have the interpreter know all these subclasses. (We want to decouple the class from these concrete command classes for reuse of the Interpreter class). We want to defer creation of the concrete commands to subclasses of the interpreter for two reasons

1

Only this subclass knows the ConcreteCommand classes

2

This subclass must determine the algorithm by which the concrete commands are created

ROO,HOM/FHTenL Example calculator design September 25, 2017 23/30

slide-25
SLIDE 25

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

Factory Method example

Example

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

slide-26
SLIDE 26

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

Interpreter with Factory Method

a b s t r a c t 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 ( ) ; } } } a b s t r a c t protected Command getNewCommand ( ) ; }

ROO,HOM/FHTenL Example calculator design September 25, 2017 25/30

slide-27
SLIDE 27

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

Calculator with Factory Method

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

slide-28
SLIDE 28

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

Factory Method

Structure

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

slide-29
SLIDE 29

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

Factory Method

Participants:

Product:

defines the interface of objects the factory method creates.

ConcreteProduct:

implements the product interface.

Creator:

declares the factory method, which returns an object of type Product. Creator may also define a default implementation of the factory method that returns a default ConcreteProduct object. may call the factory method to create Product objects.

ConcreteCreator:

Overrides the factory method to return an instance of a ConcreteProduct

Applicability:

Use when a class can’t anticipate the class of objects it must create. A class wants its subclasses to specify the objects it wants to create. 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

slide-30
SLIDE 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

Factory Method

Prevents coupling:

Factory method avoids having to specify the ConcreteProduct creations The factory method defers decisions on the creation algorithm Avoids coupling between Creator and the concrete product classes

Consequences:

Provides hooks for subclasses.The factory principle (hiding creation in factory methods (either using abstract factory or factory method) adds flexibility to a design. (Note that the AbstractFactory class uses factory methods. It is a class with a factory method for each type of abstract product it is supposed to deliver.) Connects parallel class hierarchies.

ROO,HOM/FHTenL Example calculator design September 25, 2017 29/30

slide-31
SLIDE 31

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

Factory Method

Note on the example

In the given interpreter/calculator example Prototype and Factory Method are

  • combined. Prototype is used to dynamically decide which object must be

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

ROO,HOM/FHTenL Example calculator design September 25, 2017 30/30