Unformatted text preview:

1Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 511Centralized vs. DecentralizedInterpreter PatternVisitor PatternObject-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 512Defining a pictureTextFig.DrawingCompFig.CompFig.RectangleFig.TextFig.RectangleFig.2Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 513Defining a Language for PicturesFigure - displayOn: is abstract methodCompositeFigureDrawingRectangleFigureTextFigureObject-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 514Interpreter PatternTree of objects is a kind of programInterpret program by sending message toroot, which is recursively sent to entiretree.Not about parsing!!!3Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 515Object-Oriented InterpretersTo write a little object-oriented interpreterfor a language L:• 1) make a subclass of LParseNode foreach rule in the grammar of L• 2) for each subclass, define aninterpreter method that takes thecurrent context as an argument.Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 516Object-Oriented Interpreters• 3) define protocol for making a tree ofLParseNodes.• 4) define a program for L by buildingan abstract syntax tree.4Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 517Example: ValueModelsValueModelvalueValueHoldervaluevalueBlockValueblockargumentsvalueObject-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 518ValueModels| x y |x := 3 asValue.y := 7 asValue.x + yValueModel>> + aValue^ BlockValueblock: [:a :b | a value * b value]arguments: (Array with: self with:aValue asValue)ValueHolderBlockValueValueHolder5Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 519Example: Regular Expression CheckerGrammar: exp ::= string | exp ‘+’ exp | exp ‘&’ exp | exp‘repeat’Step 1: define RegExpNode, MatchRENode,AlternationRENode, SequenceRENode,RepeatRENodeStep 2: define a match: method for each classthat takes an input state as an argumentRegular Expression ClassesRegExpNodeSequenceRENodeMatchMatchMatchRENodecontentsMatchRepeatRENodeMatchAlternationRENodeMatch6Regular Expression Objects'this is a ' ('very ' *) ('big ’ + 'small ') ('dog' + 'cat' + 'mouse')SequenceRENodeMatchRENode'this is a 'RepeatRENodeMatchRENode'big 'AlternationRENodeAlternationRENodeMatchRENode'small 'MatchRENode'very 'MatchRENode'dog'MatchRENode'mouseMatchRENode'cat'this is a very very very big mouseObject-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 522MatchingSequenceRENode>>match: inputState^componentsinject: inputStateinto: [:nextState :exp |exp match: nextState]7Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 523MatchingRepeatRENode>>match: inputState| aState finalState |aState := inputState.finalState := inputState copy....Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 524(continued)[aState notEmpty]whileTrue:[aState := component match: aState.finalState addAll: aState].^finalState8Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 525MatchingAlternationRENode>>match: inputState| finalState |finalState := REState new.components do: [ :exp | finalStateaddAll: (exp match: inputState)]^finalStateObject-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 526MatchingMatchRENode>>match: inputState| finalState tStream |finalState := REState new....REState is a collection of streams.9Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 527(continued)inputStatedo: [:stream |tStream := stream copy.(tStream nextAvailable:components size) = components ifTrue: [finalState add: tStream]].^finalStateObject-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 528Protocol for Building TreeDefine ”+" and "repeat" and ”&" asmethods on RegExpNodes and anyliterals that are to be treated as patterns,such as strings.Then ((’dog ’ + ’cat ’) repeat & ’weather’)matches: ’dog dog cat weather’ is true.10Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 529Interface methodRegExpNode>>matches: anArg| inputState |inputState := (streamOrCollection isKindOf:Collection)ifTrue: [REState with: (ReadStream on:streamOrCollection)]ifFalse: [REState with:streamOrCollection].(self match: inputState) do: [:stream | streamEdifT[^ ]]Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 530Interface methodsDefine +, &, repeat, and asRENode inRegExpNode and String+ anArg^AlternationRENode newcomponents: (Array with: self with:anArg asRENode)11Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 531Other examples of Interpreter pattern:• • producing Postscript for a document• • figuring out the value of an insurancepolicy• • spreadsheet• • compiling a programC program would use case statementObject-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 532Replacing Cases with SubclassesAdvantages• • instead of modifying casestatements, add a newsubclass• • easier to parameterize• • can use inheritance tomake new options12Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 533Replacing Cases with SubclassesDisadvantages• • program is spread out,• + harder to understand• + harder to replace algorithm• • state of object can change, but classcan notObject-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 534How To Centralize AlgorithmsDefine isAlternative, isRepeat, isSequence,isMatch, childrenDo:fix: anRENodeanRENode isMatchifTrue: [anRENode contents:(anRENode contents capitalize)]ifFalse: [anRENode childrenDo: [:child| self fix: child]]13Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 535When to Centralize AlgorithmUse centralized algorithm when youneed to• • change entire algorithm at once• • look at entire algorithm at once• • change algorithm, but not add newclasses of componentsObject-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 536Visitor patternVisitor lets you centralize algorithm, letsyou create a family of algorithms byinheritance, and makes it easy to createnew algorithms.Major problem is that adding a new kind ofparse node requires adding a newmethod to each visitor.14Object-oriented


View Full Document

UIUC CS 497 - Centralized vs. Decentralized

Download Centralized vs. Decentralized
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Centralized vs. Decentralized and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Centralized vs. Decentralized 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?