Download presentation
Presentation is loading. Please wait.
Published byCharles Floyd Modified over 8 years ago
1
JAVA DESIGN PATTERN Structural Patterns - Facade Pattern Presented by: Amit kumar narela Ise2007001 Ise2007001
2
Motivation Structuring a system into subsystems helps reduce complexity Structuring a system into subsystems helps reduce complexity Subsystems are groups of classes, or groups of classes and other subsystems 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 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 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
3
Brief Description >Facade as the name suggests means the face of the building. >It hides the complexities of the system and provides an interface to the client from where the client can access the system. > The facade pattern is a software engineering design pattern commonly used with Object-oriented programming.
4
A facade can : make a software library easier to use and understand, since the facade has convenient methods for common tasks; make a software library easier to use and understand, since the facade has convenient methods for common tasks; make code that uses the library more readable, for the same reason; make code that uses the library more readable, for the same reason; reduce dependencies of outside code on the inner workings of a library, since most code uses the facade, thus allowing more flexibility in developing the system; reduce dependencies of outside code on the inner workings of a library, since most code uses the facade, thus allowing more flexibility in developing the system;
5
Intent 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. 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.
6
Collaborations Clients communicate with the subsystem by sending requests to Facade, which forwards them to the appropriate subsystem object(s). Although the subsystem objects perform the actual work, the facade may have to do work of its own to translate its interface to subsystem interfaces. Clients communicate with the subsystem by sending requests to Facade, which forwards them to the appropriate subsystem object(s). Although the subsystem objects perform the actual work, the facade may have to do work of its own to translate its interface to subsystem interfaces. Clients that use the facade don't have to access its subsystem objects directly Clients that use the facade don't have to access its subsystem objects directly
7
Implementation Consider the following issues when implementing a facade Reducing client-subsystem coupling. The coupling between clients and the subsystem can be reduced even further by making Facade an abstract class with concrete subclasses for different implementations of a subsystem. Then clients can communicate with the subsystem through the interface of the abstract Facade class. This abstract coupling keeps clients from knowing which implementation of a subsystem is used. An alternative to subclassing is to configure a Facade object with different subsystem objects. To customize the facade, simply replace one or more of its subsystem objects.
8
Applicability Use the Facade pattern : To provide a simple interface to a complex subsystem. This interface is good enough for most clients. To provide a simple interface to a complex subsystem. This interface is good enough for most clients. To decouple the classes of the subsystem from its clients and other subsystems. To decouple the classes of the subsystem from its clients and other subsystems.
9
Structure
10
Facade Facade The facade class interacts Packages 1, 2, and 3 with the rest of the application. The facade class interacts Packages 1, 2, and 3 with the rest of the application. Clients Clients The objects using the Facade Pattern to access resources from the Packages. The objects using the Facade Pattern to access resources from the Packages. Packages Packages Software library / API collection accessed through the Facade Class. Software library / API collection accessed through the Facade Class.
11
Consequences Benefits: It hides the implementation of the subsystem from clients, making the subsystem easier to use. 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 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 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 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 It does not prevent sophisticated clients from accessing the underlying classes Facade does not add any functionality, it just simplifies interfaces Facade does not add any functionality, it just simplifies interfaces
12
Example: Home Theater System Imagine a home theater system represented as a bunch of objects > You might have objects like Amplifier, tuner, DVDPlayer, Projector, CDPlayer, TheaterLights, Screen,and PopcornPopper >To watch a DVD, you might have to: Turn the popcorn popper on Start making popcorn Dim the lights Put the screen down Turn the projector on Set the projector input to DVD
16
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();}}
17
OUTPUT Get ready to watch a movie... Get ready to watch a movie... Popcorn Popper on Popcorn Popper on Popcorn Popper popping popcorn! Popcorn Popper popping popcorn! Theater Ceiling Lights dimming to 10% Theater Ceiling Lights dimming to 10% Theater Screen going down Theater Screen going down Top-O-Line Projector on Top-O-Line Projector on Top-O-Line Projector in widescreen mode (16x9 aspect ratio) Top-O-Line Projector in widescreen mode (16x9 aspect ratio) Top-O-Line Amplifier on Top-O-Line Amplifier on Top-O-Line Amplifier setting DVD player to Top-O-Line DVD Player Top-O-Line Amplifier setting DVD player to Top-O-Line DVD Player Top-O-Line Amplifier surround sound on (5 speakers, 1 subwoofer) Top-O-Line Amplifier surround sound on (5 speakers, 1 subwoofer) Top-O-Line Amplifier setting volume to 5 Top-O-Line Amplifier setting volume to 5 Top-O-Line DVD Player on Top-O-Line DVD Player on Top-O-Line DVD Player playing "Raiders of the Lost Ark" Top-O-Line DVD Player playing "Raiders of the Lost Ark" Shutting movie theater down... Shutting movie theater down... Popcorn Popper off Popcorn Popper off Theater Ceiling Lights on Theater Ceiling Lights on Theater Screen going up Theater Screen going up Top-O-Line Projector off Top-O-Line Projector off Top-O-Line Amplifier off Top-O-Line Amplifier off Top-O-Line DVD Player stopped "Raiders of the Lost Ark" Top-O-Line DVD Player stopped "Raiders of the Lost Ark" Top-O-Line DVD Player eject Top-O-Line DVD Player eject Top-O-Line DVD Player off Top-O-Line DVD Player off
18
Related Patterns Abstract Factory can be used with Facade to provide an interface for creating subsystem objects in a subsystem- independent way. Abstract Factory can be used with Facade to provide an interface for creating subsystem objects in a subsystem- independent way. Mediator is similar to Facade in that it abstracts functionality of existing classes. However, Mediator's purpose is to abstract arbitrary communication between colleague objects Mediator is similar to Facade in that it abstracts functionality of existing classes. However, Mediator's purpose is to abstract arbitrary communication between colleague objects
19
QUESTIONS?? ?
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.