What is Façade Pattern?
Façade 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 use. Have a look into following diagram
Consequences of façade-:
- Façade shields the clients from complex subsystem components , and prevents a simpler programming interface for general users.
- However it doesn’t prevent advance users from going deeper to more complex classes when needed.
- Façade pattern provides us the flexibility to change under laying subsystem without effecting the client code.
Examples of façade pattern
- Now let’s suppose we have a client who may want to register, update, login, logout a user into our system.
- All these processes need complex operations to be excuted e.g
- In registration process we may need to do following things before registering a user
Ø User’s country is in our allowed countries list.
Ø User name is valid.
Ø Password is valid.
Ø User with same name doesn’t already exist.
Ø Credit card validation, if user is supposed to pay for the registration.
Ø Required amount available in the card.
e.t.c.
- Now without using façade pattern our client needs to first call all those complex subsystem methods , he/she has to remember about all the parameters in those methods , values they return what the returned values mean etc. This will actually create a messy and complex code at client end. Flow chart client has to follow will look like this
- You can clearly see how the code will become complex in this case and with the addition of any new thing in the registration process we will also need to update the client code.
- It become even worst in case of client/server application as you can clearly see how many numbers of network calls will be there before the actual operation i.e. user registration ( assuming that all the validation and other complex operations are to be performed on server end) is performed.
- Let’s implement façade pattern in this scenario , according to façade pattern we should provide simple programming interface to the user and shield other complexities from him/her, so on server end we can create a single method and pass it all the required parameters as
registerUser(String username, String userPassword, String creditCardNo,String countryCode){
if user doesn’t belong to an allowed country
return error code;
if user name not valid
return error code;
if user password not valid
return error code;
if user already exists
return error code;
if credit card not allowed
return error code;
if required Amount is not available
return error code;
//if the above validations are ok then
· Amount deduction from credit card.
· Register user into the system (database).
· Return a success code to the client.
}
Note: Body of the above method is in pseudo code, we will look into a practical example soon.
- Similarly we can implement other complex operations such as login. Updating, logout, buy something, sell something, logout.
- And in the end we may have a full fledge class of all those methods. All those methods will be shielding complex business operations and provide a simple interface to client to call.
- Lets call the class having those methods as MyFacadeWithCommandPattern.