Sei sulla pagina 1di 18

Facade Design Pattern

By VIPIN T.M 2013H112049G

Structural Patterns

Structural patterns are concerned with how classes and objects are composed to form larger structures.Structural class patterns use inheritance to compose interfaces or implementations Eg . Facade,Adapter,Decorator

Structure

Purpose
Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher higher-level interface that makes the subsystem easier to use.

Motivation
Reduce Complexity: Introduce a facade object that

provides a single, simplified interface to a more general facilities of a subsystem.

Applicability

You want to provide a simple interface to a complex subsystem.A facade can provide a simple default view of the subsystem that is good enough for most clients. Only clients needing more customizability will need to look beyond the facade. You want to layer your subsystems.

Participants
Facade Knows which subsystem classes are responsible for a request. Delegates client requests to appropriate subsystem objects. Subsystem classes Implement subsystem functionality. Handle work assigned by the Facade object. Have no knowledge of the facade; that is, they keep no references to it.

Collabarations
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.

Sub classes
class A: def Do_A_Task(self): print "Executing A" class B: def Do_B_Task(self): print "Executing B" class C: def Do_C_Task(self): print "Executing C"

class Facade: def doAllTask(self): #Creates objects of A,B and C.( C.(Aobj,Bobj,Cobj) Aobj.Do_A_Task() Bobj.Do_B_Task() Cobj.Do_C_Task() if __name__=="__main__": Facadeobj=Facade() Facadeobj.doAllTask()

Real Life Examples

Compiler
Consistes of components such as a lexical analyzer,parser etc. In most cases client is least concerned about these individual components.It would like to have a interface compile() which does all the tasks

Online Travel Booking Website

public class HotelBooker { public ArrayList<Hotel> getHotelNamesFor(Date from, Date to) { //returns hotels available in the particular date range } } public class FlightBooker { public ArrayList<Flight> getFlightsFor(Date from, Date to) { //returns flights available in the particular date range } }

public class TravelFacade { private HotelBooker hotelBooker; private FlightBooker flightBooker; public void getFlightsAndHotels(Date from, Data to) { ArrayList<Flight> flights = flightBooker.getFlightsFor(from, to); ArrayList<Hotel> hotels = hotelBooker.getHotelsFor(from, to); //process and return }} public class Client { public static void main(String[] args) { TravelFacade facade = new TravelFacade(); facade.getFlightsAndHotels(from, to); }}

Consequences
Shielding clients from subsystem promotes weak coupling b/w subsystem and the clients. It shields clients from subsystem components, thereby reducing the number of objects that clients deal with and making the subsystem easier to use Does not prevent clients from accessing the underlying classes

References
Object Design Patterns: Elements of Reusable Object-Oriented Software,Eric Gama et al, ISBN: 0201633612 http://javapapers.com/design-patterns/facade-designhttp://javapapers.com/design pattern/
http://en.wikipedia.org/wiki/Facade_pattern http://www.tutorialspoint.com/design_pattern/facade_

pattern.htm

Potrebbero piacerti anche