Facade design pattern

Type: Structural design pattern

Description:

‘Facade’ means ‘outward appearance

Lets take a simple example. When you visit any house, from outside its look really nice. Colorful walls, garden etc. But when you step in inside the house, you can see that all household items are not at their place. Some items are here and some items are there. There is full mess in the house. So just looking at the facade of the house, viewer would never know the internal structure of house, building blocks etc.

So similarly in software development also, a client should access a system and that system should internally access other multiple sub systems. So in short, facade provides a simplified interface which has all the capabilities to complete a task instead of accessing multiple sub systems by a client.

Example:

If a user wants to open his / her bank account in a bank then series of actions needs to be performed like needs to validate user identity, needs to create user, needs to create saving / current account, send notification via email / SMS etc. So client needs to perform all these actions and needs to interact with all these sub systems.

sub system access
Client accessing all various components of system

Instead of accessing all sub systems / components of whole system, if we make wrapper around this and create a single service which is responsible for performing all these steps. See below.

accountservice
Account service
facade
Facade

So now, client just needs to interact with single service called IAccountService.CreateBankAccount(…) and this service is responsible to interact with all other sub systems.

Structure:

facade structure

Advantages:

1. Client needs to deal with only one interface to access system instead of multiple sub systems.

2. This pattern decouples client from sub systems.

Leave a Reply

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