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

tddb84 design patterns lecture 08 interpreter facade
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Peter Bunus Department of Computer and Information Science Linköping University, Sweden peter.bunus@liu.se

TDDB84 Design Patterns Lecture 08 Interpreter, Facade

slide-2
SLIDE 2

TDDB84 Design Patterns Lecture 08 Slide 2

2

Interpreter

slide-3
SLIDE 3

TDDB84 Design Patterns Lecture 08 Slide 3

3

The Interpreter – Non Software Example

  • 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

Musical Notation (Abstract Expression) Notes (Terminal Expression) Signatures

#

slide-4
SLIDE 4

TDDB84 Design Patterns Lecture 08 Slide 4

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
  • f these classes
slide-5
SLIDE 5

TDDB84 Design Patterns Lecture 08 Slide 5

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;

slide-6
SLIDE 6

TDDB84 Design Patterns Lecture 08 Slide 6

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

slide-7
SLIDE 7

TDDB84 Design Patterns Lecture 08 Slide 7

7

The Ugly Dukly Language Classes

  • We define a class based representation for the grammar along with an

interpreter to interpret the sentences

+interpret(in context) Expression +interpret(in context)

  • variable
  • expression

Repetition +interpret(in context)

  • expression1
  • expression2

Expression +interpret(in context) Variable +interpret(in context) QuackCommand +interpret(in context) RightCommand +interpret(in context) FlightCommand * * * * * * expression ::= <command> | <sequence> | <repetition> sequence ::= <expression> ’;’ <expression> command ::= right | quack | fly vepetition := while ’(’ <variable> ’)’ <expression> variable :=[A-Z,a-z];

slide-8
SLIDE 8

TDDB84 Design Patterns Lecture 08 Slide 8

8

Interpreter UML

Client AbstractExpression Interpret(Context) TerminalExpression Interpret(Context) NonterminalExpression Interpret(Context) Context

Builds abstract syntax tree representing a particular sentence in the language and invokes the interpret

  • peration.

Declares an abstract Interpret

  • peration that is

common to all nodes in the abstract syntax tree. Implements an Interpret operation associated with terminal symbols in the grammar. Implements an Interpret

  • peration for

nonterminal symbols in the grammar. Contains information that’s global to the interpreter

slide-9
SLIDE 9

TDDB84 Design Patterns Lecture 08 Slide 9

9

Interpreter – Looks familiar?

Client AbstractExpression Interpret(Context) TerminalExpression Interpret(Context) NonterminalExpression Interpret(Context) Context

slide-10
SLIDE 10

TDDB84 Design Patterns Lecture 08 Slide 10

10

Composite (Structural Pattern)

Client Component Operation() Leaf Operation() Composite Operation()

slide-11
SLIDE 11

TDDB84 Design Patterns Lecture 08 Slide 11

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
  • r 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.

slide-12
SLIDE 12

TDDB84 Design Patterns Lecture 08 Slide 12

12

Facade

slide-13
SLIDE 13

TDDB84 Design Patterns Lecture 08 Slide 13

13

Facade – Non Software Example

slide-14
SLIDE 14

TDDB84 Design Patterns Lecture 08 Slide 14

14

Joe installs a Home Theater

slide-15
SLIDE 15

TDDB84 Design Patterns Lecture 08 Slide 15

15

Joe’s Home Theater

+on() +off() +setCd() +setDvd() +setStereoSound() +setSurroundSound() +setTuner() +setVolume()

  • tuner
  • dvdPlayer
  • cdPlayer

Amplifier +on() +off() +setAm() +setFm() +setFrequency()

  • amplifier

Tuner +on() +off() +eject() +pause() +play() +setSurroundAudio() +setTwoChannelAudio() +stop()

  • amplifier

DvdPlayer +on() +off() +eject() +pause() +play() +stop()

  • amplifier

CDPlayer +on() +off() +tvMode() +wideScreenMode()

  • dvdPlayer

Projector +on() +off() +dim() TheaterLights +up() +down() Screen +on() +off() +pop() PoppcornPopper

slide-16
SLIDE 16

TDDB84 Design Patterns Lecture 08 Slide 16

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

TDDB84 Design Patterns Lecture 08 Slide 17

17

Watching a movie – The Java Way

popper.on(); popper.pop(); lights.dim(10); screen.down(); projector.on(); projector.setInput(); projector.wideScreenMode(); amp.on(); amp.setDvD(dvd); amp.setSurroundSound(); amp.setVolume(); dvd.on(); dvd.play(movie);

When the movie is over how do you turn everything off ? If Joe decides to upgrade the system he will probably going to learn a slightly different procedure ? What is the procedure when you

  • nly want to listen to the Radio or

a CD.

slide-18
SLIDE 18

TDDB84 Design Patterns Lecture 08 Slide 18

18

Time for Facade

+on() +off() +setCd() +setDvd() +setStereoSound() +setSurroundSound() +setTuner() +setVolume()

  • tuner
  • dvdPlayer
  • cdPlayer

Amplifier +on() +off() +setAm() +setFm() +setFrequency()

  • amplifier

Tuner +on() +off() +eject() +pause() +play() +setSurroundAudio() +setTwoChannelAudio() +stop()

  • amplifier

DvdPlayer +on() +off() +eject() +pause() +play() +stop()

  • amplifier

CDPlayer +on() +off() +tvMode() +wideScreenMode()

  • dvdPlayer

Projector +on() +off() +dim() TheaterLights +up() +down() Screen +on() +off() +pop() PoppcornPopper

+watchMovie() +endMovie() +listenToCd() +endCd() +listenToRadio() +endRadio() HomeTheaterFacade

watchMovie()

slide-19
SLIDE 19

TDDB84 Design Patterns Lecture 08 Slide 19

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; }

slide-20
SLIDE 20

TDDB84 Design Patterns Lecture 08 Slide 20

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

slide-21
SLIDE 21

TDDB84 Design Patterns Lecture 08 Slide 21

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

slide-22
SLIDE 22

TDDB84 Design Patterns Lecture 08 Slide 22

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

slide-23
SLIDE 23

TDDB84 Design Patterns Lecture 08 Slide 23

23

Design Principle

Talk only to your immediate friends

slide-24
SLIDE 24

TDDB84 Design Patterns Lecture 08 Slide 24

24

Principle of Least Knowledge

  • What does it mean?
  • When you designing a system, be careful on the number of classes it interacts

with and how it comes to interact with thse classes

  • When you build a lot of dependencies between many classes you are building a

fragile system: changes in one part cascade in other part

  • What I should do?
  • It’s easy. Invoke only methods that belong to:

– The object itself – Objects passed in as parameters to the method – Any object the method creates or instantiates – Any components of the object

slide-25
SLIDE 25

TDDB84 Design Patterns Lecture 08 Slide 25

25

Without The Principle

Tone, What is the

  • utside temperature

now? Well Joe, I can give a thermometer and you can measure the temperature yourself?

public float getTemperature(){ Thermometer thermometer = tone.getThermometer(); return thermometer.getTemperature(); }

slide-26
SLIDE 26

TDDB84 Design Patterns Lecture 08 Slide 26

26

Using The Principle of Least Knowledge

Tone, What is the

  • utside temperature

now? It is 32 degrees Celsius?

public float getTemperature(){ return = tone.getTemperature(); }

When we apply the principle, we add a method to Tone that makes the request to the thermometer for Joe. This reduces the number of classes Joe is dependent on

slide-27
SLIDE 27

TDDB84 Design Patterns Lecture 08 Slide 27

27 public class Car{ Engine engine; // other instance variables public Car(){ // initialize engine, etc } public void start(Key key) { Doors doors = new Doors(); boolean authorized = key.turns(); if (authorized){ engine.start(); updateDashboeardDisplay(); doors.lock(); } } public void updateDashboardDisplay(){ // update display } }

Here’s a component of this class. We can call its methods Here we are creating a new object, it’s methods are legal You can call a method on an object passed as a parameter You can call a method on a component of the object You can call a local method within the

  • bject

You can call a method on an object you create or instantiate

Using The Principle of Least Knowledge

slide-28
SLIDE 28

TDDB84 Design Patterns Lecture 08 Slide 28

28

The Constitution of Software Architects

Encapsulate what varies Program through an interface not to an implementation Favor Composition over Inheritance Classes should be open for extension but closed for modification Don’t call us, we’ll call you A Class should have only one reason to change Depend upon abstractions. Do not depend upon concrete classes. Strive for loosely coupled designs between

  • bjects that interact

Only talk to your friends

slide-29
SLIDE 29

TDDB84 Design Patterns Lecture 08 Slide 29

29

Facade – Compiler example

slide-30
SLIDE 30

TDDB84 Design Patterns Lecture 08 Slide 30

30

Mediator versus Facade

Mediator

Façade abstracts a subsystem of objects to provide a convenient interface. Unidirectional. Enables cooperative behaviour, that colleagues don’t or can’t provide. Multidirectional. Façade objects make requests of the subsystem, but not vice- versa.

Façade

slide-31
SLIDE 31

Amazon shall use SOA

31

SOA: SW architecture where all components are designed to be services

  • 1. All teams will henceforth expose their data and functionality through service

interfaces

  • 2. Teams must communicate with each other through these interfaces
  • 3. There will be no other form of interprocess communication allowed: no direct

linking, no direct reads of another team's data store, no shared-memory model, no back-doors whatsoever. The only communication allowed is via service interface calls over the network.

Taken from: CS 169 Software Engineering: Armando Fox, David Patterson, 
 and Koushik Sen

slide-32
SLIDE 32

Amazon shall use SOA

32

4. It doesn't matter what [API protocol] technology you use. 5. Service interfaces, without exception, must be designed from the ground up to be externalizable. That is to say, the team must plan and design to be able to expose the interface to developers in the outside world. No exceptions. 6. Anyone who doesn't do this will be fired. 7. Thank you; have a nice day!“

Taken from: CS 169 Software Engineering: Armando Fox, David Patterson, 
 and Koushik Sen