Facade Design Pattern is nothing but it simply interface of interfaces to simplify interactions between the client code and subsystem classes. This design comes under the GOF Structural Design Pattern. Facade provides clients with access to the system but conceals the working of the system and its complexities. The pattern creates one class consisting of user functions and delegates provide calling facilities to the classes belonging to the systems.
According to the Gang of Four:
Provide 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.
This pattern involves a single class which provides simplified methods required by client and delegates calls to methods of existing system classes.
There are following classes and objects participating in this pattern are:
Facade (BankingServiceFacade)
Subsystem classes (AccountService, TransferService, PaymentService)
also read:
There are following pros of using this facade pattern.
There are following cons of using this facade pattern.
Suppose you are designing a system, this system has very large number of independent classes and also has set of services to be implementing. This system is going to be very complex, Facade pattern comes into picture and reduce complexities of the larger system and simplifies interactions of the client code with the set of classes of subsystem of the large complex system.
Suppose you want to develop an bank enterprise application with the large number services some of them as AccountService for getting the Account by accountId, PaymentService for payment gateway service and TransferService is using for actually amount transfer from one account to another account. A client code of the application interacts with these services for a amount transfer from one account to another account. This is how different clients interact with the amount transfer process of the bank system as below:
This interface is know as Facade interface, it is based the Facade pattern, it is a simple way to interact with the subsystems.
Account.java
/** * */ package com.doj.patterns.structural.facade; /** * @author Dinesh.Rajput * */ public interface Account { void accountType(); }
SavingAccount.java
/** * */ package com.doj.patterns.structural.facade; /** * @author Dinesh.Rajput * */ public class SavingAccount implements Account { @Override public void accountType() { System.out.println("SAVING ACCOUNT"); } }
BankingServiceFacade.java
package com.doj.patterns.structural.facade; public interface BankingServiceFacade { void moneyTransfer(); }
BankingServiceFacadeImpl.java
package com.doj.patterns.structural.facade; /** * @author Dinesh.Rajput * */ public class BankingServiceFacadeImpl implements BankingServiceFacade{ @Override public void moneyTransfer() { if(PaymentService.doPayment()){ Account fromAccount = AccountService.getAccount("1"); Account toAccount = AccountService.getAccount("2"); TransferService.transfer(1000, fromAccount, toAccount); } } }
AccountService.java
package com.doj.patterns.structural.facade; /** * @author Dinesh.Rajput * */ public class AccountService { public static Account getAccount(String accountId) { return new SavingAccount(); } }
PaymentService.java
package com.doj.patterns.structural.facade; /** * @author Dinesh.Rajput * */ public class PaymentService { public static boolean doPayment(){ return true; } }
TransferService.java
package com.doj.patterns.structural.facade; /** * @author Dinesh.Rajput * */ public class TransferService { public static void transfer(int amount, Account fromAccount, Account toAccount) { System.out.println("Transfering Money"); } }
FacadePatternDemo.java
package com.doj.patterns.structural.facade; /** * @author Dinesh.Rajput * */ public class FacadePatternDemo{ public static void main(String[] args) { BankingServiceFacade serviceFacade = new BankingServiceFacadeImpl(); serviceFacade.moneyTransfer(); } }
Transfering Money
Mediator design pattern may look very similar to facade design pattern in terms of abstraction. Mediator abstracts the functionality of the subsystems in this way it is similar to the facade pattern. In the implementation of mediator pattern, subsystem or peers components are aware of the mediator and that interact with it. In the case of facade pattern, subsystems are not aware of the existence of facade. Only facade talks to the subsystems.
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…