Wednesday, January 9, 2008

Facade Pattern (Part 1): What? and When?

Facade is a structural pattern whose purpose is to provide a unified and simplified interface to the complex sub-system.

The dictionary meaning of facade is:
  1. "A face or front of a building"
  2. "A showy misrepresentation intended to conceal something unpleasant"

In our day to day life we come across architecturally sound buildings. Now a days the trend is to have the front or the face of the building made up of glasses which give an elegant look. But the internal architecture of the buildings are found to be complex and distracting.

Facade is used when you want to hide something complex behind a simple and unified interface.

For e.g.

Assume that you are a master of OCR technology and have developed some complex OCR sub-system or package or library. This sub-system consists of about 50 odd classes. This package is to be consumed by some application, which is being developed by the application developers. These application developers do not have much knowledge about OCR technology. They just want to perform an OCR on some images and generate a word document out of it.

In these days of RAD and competitive market it is not feasible to first learn the OCR technology and then understand the complete OCR sub-system and then start consuming it. It is always better if the simplified and unified interface for the complex sub-system exists so that RAD can be achieved.

Hence to support RAD and to hide the underlying complexity of the sub-system, Facade can be used. Here facade provides a simplified interface to accomplish trivial tasks. To accomplish difficult or non-trivial tasks, the application programmer has to rely on the sub-system.

The crux of the matter is "Facade hides the complexity of the sub-system from the consumer".

No comments: