tddb84 design patterns lecture 08 interpreter facade
play

TDDB84 Design Patterns Lecture 08 Interpreter, Facade Peter Bunus - PowerPoint PPT Presentation

TDDB84 Design Patterns Lecture 08 Interpreter, Facade Peter Bunus Department of Computer and Information Science Linkping University, Sweden peter.bunus@liu.se Interpreter TDDB84 Design Patterns 2 Lecture 08 Slide 2 The Interpreter


  1. TDDB84 Design Patterns Lecture 08 Interpreter, Facade Peter Bunus Department of Computer and Information Science Linköping University, Sweden peter.bunus@liu.se

  2. Interpreter TDDB84 Design Patterns 2 Lecture 08 Slide 2

  3. The Interpreter – Non Software Example Musical Notation (Abstract Expression) Notes (Terminal Expression) Signatures # The Interpreter pattern defines a grammatical representation for a language and an � interpreter to interpret the grammar. Musicians are examples of Interpreters. � The pitch of a sound and its duration can be represented in musical notation on a staff � Musicians playing the music from the score are able to reproduce the original pitch and � duration of each sound represented TDDB84 Design Patterns 3 Lecture 08 Slide 3

  4. The Interpreter Use the Interpreter to build and interpreter for a language � Define a language grammar � • Each grammar rule is represented by a class Represent sentences � • Sentences within language can be represented by abstract syntax trees of instances of these classes TDDB84 Design Patterns 4 Lecture 08 Slide 4

  5. The Interpreter – How it Works We need to implement an educational tool for children to � learn programming. Using our tool, each child gets to control Ugly Duckly � with the help of a simple language. Here is a example of the language: � right; while (daylight) fly; quack; TDDB84 Design Patterns 5 Lecture 08 Slide 5

  6. The Ugly Dukly Language right; while (daylight) fly; quack; expression ::= <command> | <sequence> | <repetition> sequence ::= <expression> ’;’ <expression> command ::= right | quack | fly repetition := while ’(’ <variable> ’)’ <expression> variable :=[A-Z,a-z]; We got the grammar, now all we need is a way to represent and � interpret sentences in the grammar TDDB84 Design Patterns 6 Lecture 08 Slide 6

  7. The Ugly Dukly Language Classes We define a class based representation for the grammar along with an � interpreter to interpret the sentences expression ::= <command> | <sequence> | <repetition> sequence ::= <expression> ’;’ <expression> command ::= right | quack | fly vepetition := while ’(’ <variable> ’)’ <expression> variable :=[A-Z,a-z]; Expression * +interpret(in context) * * Repetition Expression * * -variable -expression1 -expression -expression2 +interpret(in context) +interpret(in context) FlightCommand Variable QuackCommand RightCommand * +interpret(in context) +interpret(in context) +interpret(in context) +interpret(in context) TDDB84 Design Patterns 7 Lecture 08 Slide 7

  8. Interpreter UML Contains information Builds abstract syntax tree that ’ s global to the Declares an representing a particular interpreter abstract Interpret sentence in the language operation that is and invokes the interpret common to all operation. nodes in the Context abstract syntax tree. Client AbstractExpression Interpret(Context) TerminalExpression NonterminalExpression Interpret(Context) Interpret(Context) Implements an Interpret operation Implements an Interpret associated with operation for terminal symbols in nonterminal symbols in TDDB84 Design Patterns 8 the grammar. the grammar. Lecture 08 Slide 8

  9. Interpreter – Looks familiar? Context Client AbstractExpression Interpret(Context) TerminalExpression NonterminalExpression Interpret(Context) Interpret(Context) TDDB84 Design Patterns 9 Lecture 08 Slide 9

  10. Composite (Structural Pattern) Client Component Operation() Leaf Composite Operation() Operation() TDDB84 Design Patterns 10 Lecture 08 Slide 10

  11. Interpretor Advantages and Disadvantages Representing each grammar rule in a class makes the language easy to � implement Because the grammar is represented by classes, you can easily change � or extend the language By adding additional methods to the class structure, you can add new � behaviors beyond interpretation (pretty printing) Use for scripting and programming languages � Use Interpreter when you need to implement a simple language � Appropriate when you have a simple grammar and simplicity is more � important than efficiency It can become cumbersome when the number of grammar rule is large. In � this cases a parser/compiler generator may be more appropriate. TDDB84 Design Patterns 11 Lecture 08 Slide 11

  12. Facade TDDB84 Design Patterns 12 Lecture 08 Slide 12

  13. Facade – Non Software Example TDDB84 Design Patterns 13 Lecture 08 Slide 13

  14. Joe installs a Home Theater TDDB84 Design Patterns 14 Lecture 08 Slide 14

  15. Joe’s Home Theater Amplifier -tuner -dvdPlayer -cdPlayer +on() +off() +setCd() +setDvd() +setStereoSound() +setSurroundSound() +setTuner() +setVolume() DvdPlayer Tuner -amplifier -amplifier +on() +on() +off() +off() +eject() +setAm() +pause() +setFm() CDPlayer +play() +setFrequency() +setSurroundAudio() -amplifier +setTwoChannelAudio() +on() +stop() +off() +eject() Screen +pause() +play() Projector +up() +stop() +down() -dvdPlayer +on() PoppcornPopper TheaterLights +off() +tvMode() +wideScreenMode() +on() +on() +off() +off() +pop() +dim() TDDB84 Design Patterns 15 Lecture 08 Slide 15

  16. Watching a movie Turn on the popcorn popper � Start the popper popping � Dim the lights � Put the screen down � Turn the projector on � Set the projector input to DVD � Put the projector on wide-screen mode � Turn the sound amplifier on � Set the amplifier to DVD input � Set the amplifier to surround sound � Set the amplifier volume to medium � Turn the DVD player on � Start the DVD player playing � TDDB84 Design Patterns 16 Lecture 08 Slide 16

  17. Watching a movie – The Java Way popper.on(); popper.pop(); When the movie is over how do you turn everything off ? lights.dim(10); screen.down(); projector.on(); If Joe decides to upgrade the projector.setInput(); projector.wideScreenMode(); system he will probably going to learn a slightly different amp.on(); amp.setDvD(dvd); procedure ? amp.setSurroundSound(); amp.setVolume(); dvd.on(); What is the procedure when you dvd.play(movie); only want to listen to the Radio or a CD. TDDB84 Design Patterns 17 Lecture 08 Slide 17

  18. Time for Facade HomeTheaterFacade watchMovie() +watchMovie() +endMovie() +listenToCd() +endCd() +listenToRadio() +endRadio() Amplifier -tuner -dvdPlayer -cdPlayer +on() +off() +setCd() +setDvd() +setStereoSound() +setSurroundSound() +setTuner() +setVolume() DvdPlayer Tuner -amplifier -amplifier +on() +on() +off() +off() +eject() +setAm() +pause() +setFm() CDPlayer +play() +setFrequency() +setSurroundAudio() -amplifier +setTwoChannelAudio() +on() +stop() +off() +eject() Screen +pause() +play() Projector +up() +stop() +down() -dvdPlayer +on() PoppcornPopper TheaterLights +off() +tvMode() +wideScreenMode() +on() +on() +off() +off() +pop() +dim() TDDB84 Design Patterns 18 Lecture 08 Slide 18

  19. The Home Theater Facade public class HomeTheaterFacade { Amplifier amp; Tuner tuner; DvdPlayer dvd; CdPlayer cd; Projector projector; TheaterLights lights; Screen screen; PopcornPopper popper; public HomeTheaterFacade(Amplifier amp, Tuner tuner, DvdPlayer dvd, CdPlayer cd, Projector projector, Screen screen, TheaterLights lights, PopcornPopper popper){ this.amp = amp; this.tuner = tuner; this.dvd = dvd; this.cd = cd; this.projector = projector; this.screen = screen; this.lights = lights; this.popper = popper; } TDDB84 Design Patterns 19 Lecture 08 Slide 19

  20. The Home Theater Facade public void watchMovie(String movie) { System.out.println("Get ready to watch a movie..."); popper.on(); popper.pop(); lights.dim(10); screen.down(); projector.on(); projector.wideScreenMode(); amp.on(); amp.setDvd(dvd); amp.setSurroundSound(); amp.setVolume(5); dvd.on(); dvd.play(movie); } public void endMovie() { System.out.println("Shutting movie theater down..."); popper.off(); lights.on(); screen.up(); projector.off(); amp.off(); dvd.stop(); dvd.eject(); dvd.off(); } TDDB84 Design Patterns 20 Lecture 08 Slide 20

  21. Time to Watch a Movie public class HomeTheaterTestDrive { public static void main(String[] args) { Amplifier amp = new Amplifier("Top-O-Line Amplifier"); Tuner tuner = new Tuner("Top-O-Line AM/FM Tuner", amp); DvdPlayer dvd = new DvdPlayer("Top-O-Line DVD Player", amp); CdPlayer cd = new CdPlayer("Top-O-Line CD Player", amp); Projector projector = new Projector("Top-O-Line Projector", dvd); TheaterLights lights = new TheaterLights("Theater Ceiling Lights"); Screen screen = new Screen("Theater Screen"); PopcornPopper popper = new PopcornPopper("Popcorn Popper"); HomeTheaterFacade homeTheater = new HomeTheaterFacade(amp, tuner, dvd, cd, projector, screen, lights, popper); homeTheater.watchMovie("Raiders of the Lost Ark"); homeTheater.endMovie(); } } TDDB84 Design Patterns 21 Lecture 08 Slide 21

  22. The Facade Pattern The Facade Pattern provides an unified interface to set of interfaces in a subystem. Facade defines a higher level interface that make the subsytem easier to use TDDB84 Design Patterns 22 Lecture 08 Slide 22

  23. Design Principle Talk only to your immediate friends TDDB84 Design Patterns 23 Lecture 08 Slide 23

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend