Wednesday, August 4, 2010

Facade Design Pattern

Façade Pattern

It is used to hide the complexities of system behind a simple interface. Generally with time, the simple system grows to complex systems having a number of different APIs and objects to interact with. It happens especially if we are following the design patterns. Due to this increased complexities, it becomes very difficult for client to remember the exact flow of API and the right way to use it. Façade comes here and provides a simple interface to user, thus hides the complete complexity behind it. 

Example

  • One way to access database is to use the raw API of JDBC which may not be very convenient for every user or user may need to repeat same code again and again to access the database. Other alternative is to create a ‘Data Access Object’ which can hide the complexities of JDBC API and can provide a simple and convenient interfaced to user for most of the required operations. So we can define a SQL Data Access Manager which can provide methods like execute query, get table names etc. Here SQL Data Access Manager is working as façade.
  • Any session bean on server most of the time works as façade.
  • An Object Manager on server which allows the user to save any object and fire the relevant events in a single call.


When to Use

  • When the underlying system is complex in nature and it needs more than one calls to complete any operation
  • When we want to hide the complexities of a system behind a simple interface so that bugs due to wrong flow of calls can be avoided.
  • When we want to provide convenient methods to users so that duplicate code can be avoided.


Usage

  • Helps to avoid the coding bugs due to wrong flow of calls for a complex system
  • Helps to avoid duplicate code which may need to be written for implementing different functionalities using some common flow or system calls.
  • It does not prevent the advance user from going into deeper to more complex classes or flow.

No comments:

Post a Comment