1 / 13

Façade Pattern

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 ().

dyanne
Télécharger la présentation

Façade Pattern

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Façade Pattern Your Home theatre

  2. Task list

  3. In terms of classes

  4. But there’s more??

  5. Lets façade it

  6. Façade steps • Create a new class HomeTeathreFacade, which exposes simple methods like WatchMovie(). • The façade class treats home theatre component as sub system, and calls on the sub system to implement its WatchMovie() method. • 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 • 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.

  7. Home Theater Facade public class HomeTheaterFacade { Amplifier amp; Tuner tuner; DvdPlayerdvd; CdPlayercd; Projector projector; TheaterLights lights; Screen screen; PopcornPopper popper; public HomeTheaterFacade(Amplifier amp, Tuner tuner, DvdPlayerdvd, CdPlayercd, Projector projector, Screen screen, TheaterLights lights, PopcornPopper popper) {

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

  9. public class CdPlayer { String description; intcurrentTrack; 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 + "\""); }

  10. 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); DvdPlayerdvd = new DvdPlayer("Top-O-Line DVD Player", amp); CdPlayercd = 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"); HomeTheaterFacadehomeTheater = new HomeTheaterFacade(amp, tuner, dvd, cd, projector, screen, lights, popper); homeTheater.watchMovie("Raiders of the Lost Ark"); homeTheater.endMovie(); } }

  11. definition Faç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.

  12. Who does what? Pattern Intent Decorator Convert one interface to another Adapter Don’t alter interface, but add responsibility Façade Make Interface simpler

More Related