Facade Pattern

What is Facade Pattern

The Facade pattern is a structural pattern that provides an entry point to a subsystem of classes. It provides a simpler interface to one or more interfaces. The salient points of a facade pattern are :

  • A facade hides the complexity of a subsystem by providing an interface that is easy to use. It provides a higher level API.
  • A facade decouples the subsystem from the client. The client does not need to be aware of the subsystem interfaces and hence the subsystem interfaces can be modified without informing the client.
  • A facade promotes ‘modular’ development by fixing the contract for a subsystem and letting the subsystem be developed independently.

Example of Facade Pattern

To explain facade pattern we design a very simplistic Bank account creation system. A new user submits the account opening information, which for our example, is the name and social security number (ssn) of the customer. The account opening client takes in these two parameters and creates an account. Before creating the account the client needs to ensure that the ssn is valid and there is no existing account for the name, ssn combination. If the client directly deals with the Account opening interfaces it needs to be aware of the the account verification as well as the account creation interfaces. Lets look at the various classes first

The Bank Account class

This class has the verification methods for SSN and existing account

This class generates the new account

Lets look at the two clients. The first client is when we have no facade. The second tab shows the facade and the client that uses the facade

This class has the verification methods for SSN and existing account

This class generates the new account

class diagram of Facade Pattern

Facade Design Pattern

If you look at the client without the facade, you will notice that the client has to be aware of the AccountVerifier as well as the Account Generator interface. Also the client needs to be aware of the business logic of account creation. The facade can abstract that out so that the client needs to know only about the Facade. Note that the intent of this pattern is to only hide one or more interfaces to make them easier to use and the client can still control the business logic by passing in appropriate parameters

This finishes our discussion on the facade pattern. One example of real life use of facade pattern is in J2EE where a session facade hides access to multiple session beans.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to top
Bitnami