+ All Categories
Home > Documents > Façade Pattern

Façade Pattern

Date post: 24-Feb-2016
Category:
Upload: dyanne
View: 63 times
Download: 0 times
Share this document with a friend
Description:
Façade Pattern. Your Home theatre. Task list. In terms of classes. But there’s more??. Lets façade it. Façade steps. Create a new class HomeTeathreFacade , which exposes simple methods like WatchMovie (). - PowerPoint PPT Presentation
13
Façade Pattern Your Home theatre
Transcript
Page 1: Façade Pattern

Façade Pattern

Your Home theatre

Page 2: Façade Pattern

Task list

Page 3: Façade Pattern
Page 4: Façade Pattern

In terms of classes

Page 5: Façade Pattern

But there’s more??

Page 6: Façade Pattern

Lets façade it

Page 7: Façade Pattern

Façade steps1. Create a new class HomeTeathreFacade, which exposes simple

methods like WatchMovie().2. The façade class treats home theatre component as sub system,

and calls on the sub system to implement its WatchMovie() method.

3. Your client code now calls methods on the home theatre Façade, not on the subsystem. So now to watch a movie we just call one method, watchMovie(), and it communicates with the lights, DVD player, projector, amp, screen and popcorn maker for us

4. The Façade still leaves the subsystem accessible to be used directly. If you need the advanced functionality of the subsystem classes, they are available for use.

Page 8: Façade Pattern

Home Theater Facadepublic 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) {

Page 9: Façade Pattern

this.amp = amp;this.tuner = tuner;this.dvd = dvd;this.cd = cd;this.projector = projector;this.screen = screen;this.lights = lights;this.popper = popper;}

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

Page 10: Façade Pattern

public class CdPlayer {String description;int currentTrack;Amplifier amplifier;String title;

public CdPlayer(String description, Amplifier amplifier) {this.description = description;this.amplifier = amplifier;

}

public void on() {System.out.println(description + " on");

}

public void off() {System.out.println(description + " off");

}

public void eject() {title = null;System.out.println(description + " eject");

}

public void play(String title) {this.title = title;currentTrack = 0;System.out.println(description + " playing \"" + title + "\"");

}

Page 11: Façade Pattern

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

}

Page 12: Façade Pattern

definitionFaçade pattern provides a unified interface to a set of interfaces

in a subsystem. Façade defines a higher level interface that makes the sub system easier to use.

Page 13: Façade Pattern

Who does what?

Pattern Intent

Decorator Convert one interface to another

Adapter Don’t alter interface, but add responsibility

Façade Make Interface simpler


Recommended