Monday, September 01, 2008

Façade Pattern

  • ­          A facade hides all the complexity of one or more classes
  • ­          Similar to Adapter pattern, it alters an interface, but for a different reason – to simplify the interface
  • ­          Façades and adapters may wrap multiple classes, but a façade's intent is to simplify, while an adapter's is to convert the interface to something different
  • ­          A façade not only simplifies an interface, it decouples a client from a subsystem of components
  • ­          It provides a simplified interface while still exposing the full functionality of the system to those who may need it
  • ­          Façades don't encapsulate the subsystem classes – they merely provide a simplified interface to their functionality
  • ­          The subsystem classes still remain available for direct use by clients that need to use more specific interfaces
  • ­          The façade can also add additional functionality in addition to making use of the subsystem
  • ­          It is not necessary that each subsystem should have only one façade, any number of façades can be created for a given subsystem
  • ­          Following is an example of a façade for a home theater system

public class HomeTheaterFacade {

      Amplifier amp;

      DvdPlayer dvd;

      Projector projector;

      Screen screen;

      Tuner tuner;

 

      public HomeTheaterFacade(Amplifier amp,

                              DvdPlayer dvd,

                              Projector projector,

                              Screen screen,

                              Tuner tuner) {

            this.amp = amp;

            this.dvd = dvd;

            this.projector = projector;

            this.screen = screen;

            this.tuner = tuner;

      }

 

      public void watchMovie() {

      }

 

      public void endMovie() {

      }

}

­           

  • ­          The 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 subsystem easier to user
  • ­          To use the façade pattern, we create a class that simplifies and unifies a set of more complex classes that belong to some subsystem
  • ­          It allows us to avoid tight coupling between clients and subsystems­     

No comments: