Presentation is loading. Please wait.

Presentation is loading. Please wait.

In the name of God Fa ç ade Design Pattern Amin Mozhgani Software engineering(II)

Similar presentations


Presentation on theme: "In the name of God Fa ç ade Design Pattern Amin Mozhgani Software engineering(II)"— Presentation transcript:

1 In the name of God Fa ç ade Design Pattern Amin Mozhgani Software engineering(II)

2 Abstract The Facade design pattern provides an easy- to-use interface to an otherwise complicated collection of interfaces or subsystems. It makes things easier by hiding the details of its implementation. It provide a unified interface to a set of interfaces in a subsystem. Façade defines a higher-level interface that makes the subsystem easier to use.

3 Introduction The Facade design pattern connects the code we write for applications, which do specific tasks, such as creating a report, and the low level implementation that handle the details, such as reading file, interacting with the network, and creating output. The facade is an interface that an application can use to get things done without worrying about the details. The facade decouples these layers so that they don’t depend on each other, which makes each easier to develop, easier to use, and promotes code re-use.

4 Motivation Structuring a system into subsystems helps reduce complexity Subsystems are groups of classes, or groups of classes and other subsystems The interface exposed by the classes in a subsystem or set of subsystems can become quite complex One way to reduce this complexity is to introduce a facade object that provides a single, simplified interface to the more general facilities of a subsystem

5 Motivation(II)

6 Applicability Use the Facade pattern: To provide a simple interface to a complex subsystem. This interface is good enough for most clients; more sophisticated clients can look beyond the facade. To decouple the classes of the subsystem from its clients and other subsystems, thereby promoting subsystem independence and portability

7 Consequences Benefits It hides the implementation of the subsystem from clients, making the subsystem easier to use It promotes weak coupling between the subsystem and its clients. This allows you to change the classes the comprise the subsystem without affecting the clients. It reduces compilation dependencies in large software systems It simplifies porting systems to other platforms, because it's less likely that building one subsystem requires building all others It does not prevent sophisticated clients from accessing the underlying classes Note that Facade does not add any functionality, it just simplifies interfaces Liabilities It does not prevent clients from accessing the underlying classes!

8 An example : Compiler

9 The Façade Pattern Head First Design Pattern O’Reilly, First Ed. Oct 2004 Eric Freeman & Elisabeth Freeman With Kathy Sierra & Bert Bates

10 Home Sweet Home Theater

11 Watching the movie (the hard way) 1. Turn on the popcorn popper 2. Start the popper popping 3. Dim the lights 4. Put the screen down 5. Turn the projector on 6. Set the projector input to DVD 7. Put the projector on a wide-screen mode 8. Turn the sound amplifier on 9. Set the amplifier to DVD input 10. Set the amplifier to surround sound 11. Set the amplifier volume to medium (5) 12. Turn the DVD Player on 13. Start the DVD Player playing And there’s more….When movie is over, you have to do all of this over again, in reverse...Wouldn’t it be as complex to listen to a CD or the.If you decide to upgrade your system, you’re probably going to have to learn a slighty different So what to do ? The complexity of using your home theater is becoming apparent!

12 The code… popper.on(); popper.pop(); lights.dim(10); screen.down(); projector.on(); projector.setInput(dvd); projector.wideScreenMode(); amp.on(); amp.setDVD(dvd); amp.setSurroundSound(); amp.setVolume(5); dvd.on(); dvd.play(movie);

13 The Façade is just what you need… With the Façade Pattern, you can take a complex subsystem and make it easier to use by implementing a Façade class that provide one, more reasonable interface.

14 How the façade operates.. Create a façade for the home theater system.Create a new class HomeTheatreFacade which exposes a few simple methods such as watchMovie().The façade class treats the home theater components as a subsystem, and calls on the subsystem to implement its watchMovie() method.Your client code now calls methods on the home theater façade, not on the subsystem.The façade still leaves the subsystem accessible to be used directly.

15 Constructing the home theater façade 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; } // other methods here }

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

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

18 HomeTheaterFacade (continue) public void listenToCd(String cdTitle) { System.out.println("Get ready for an audiopile experence..."); lights.on(); amp.on(); amp.setVolume(5); amp.setCd(cd); amp.setStereoSound(); cd.on(); cd.play(cdTitle);}

19 HomeTheaterFacade (continue) public void endCd() { System.out.println("Shutting down CD..."); amp.off(); amp.setCd(cd); cd.eject(); cd.off();}

20 HomeTheaterFacade (continue) public void listenToRadio(double frequency) { System.out.println("Tuning in the airwaves..."); tuner.on(); tuner.setFrequency(frequency); amp.on(); amp.setVolume(5); amp.setTuner(tuner);}

21 HomeTheaterFacade (continue) public void endRadio() { System.out.println("Shutting down the tuner..."); tuner.off(); amp.off();}

22 Time to watch 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();}}

23 References Design Patterns, Erich Gamma, Richard Helm, Ralph Johnson, Jon Vlissides, Addison Wesley, The Facade Design Pattern:brian d foy. The Façade Pattern,Head First Design Pattern:O’Reilly, First Ed. Oct Design Patterns In Java, The Façade Pattern: Bob Tarr.


Download ppt "In the name of God Fa ç ade Design Pattern Amin Mozhgani Software engineering(II)"

Similar presentations


Ads by Google