This tutorial explains Facade Design Pattern in Java with class diagrams and example code.
Introduction – Facade Design Pattern is a structural design pattern among the Gang Of Four(GOF) Design PatternsArticle on GOF Patterns & their types. Being a structural design pattern, the facade pattern basically deals with how the classes are composed together to get the desired outcome from a system.
What is Facade Design Pattern: The basic design problem which the facade pattern solves is to simplify access to a set of interfaces in a subsystem. Facade solves this problem by providing a higher-level interface one level above all the sub-level interfaces. This higher-level interface then deals with the details and intricacies of the subsystem interface and at the same time provides a simplified interfaces to the clients of the subsystem.
Facade pattern improves decoupling between the clients and subsystems as the higher-level interface can absorb any changes which might happen in the lower subsystems interfaces and provide a consistent interface to the clients. This also has a direct benefit of system portability as the clients only design and code for the higher-level interface defined by the Facade. Porting of the subsystems then becomes easy as all the complexity of porting is hidden from the clients by the Facade layer.
Layered subsystems can have a facade for each layer. Thus communication between layers of the subsystem is much simplified as it happens through the subsystem facades rather than directly deal with the complexities of the subsystem’s individual classes.
Class Diagram for Facade Design Pattern
Explanation of Class Diagram
- The classes to the extreme right belong the Sub-System(depicted by the UML notation of ::).
- There are N subsystem classes-
- Facade layer, or
Facadeclass as per this class diagram, invokes the sub-system classes as per the client request received by it on one of its
SimplifiedMethods()in turn invoke multiple subsystem classes based on the business logic.
SimplifiedMethods()may talk to multiple sub-system classes as per the business logic and in the end come up with the simplified/aggregated response which can be sent back to the client.
Clientclass creates, or holds, an instance of the
Facadeand invokes the specific
simplifiedMethod()as per his needs. He just invokes the simplifiedMethod() and in return gets the simplified/aggregate response. Thus, the
Clientis shielded from the underlying complexities of the sub-system layer by the
Example in Java – Class Diagram
The Java class diagram above depicts facade pattern implemented for a banking system where –
FDIssuerare three classes in the banking sub-system which accomplish a functionality as represented by their name i.e. they credit an account with a specific amount, debit an account with the given amount, or issue an FD for a given amount respectively.
BankingFacadehere holds handles to all the three sub-system classes. It invokes one or more of the sub-system classes based on the banking function requested by the Client.
- For the operation online transfer on
Clientinvokes just one method
onlineTransfer()with debit-account, credit-account and amount to be transferred. Internally
AccountDebitorto debit and then
AccountCreditorto credit the amount. It also takes care of transactions and any exceptions which may happen.(I have not written code for transaction & exception handling in this example as it is not relevant to facade pattern as such).
- Similar to onlineTransfer() method on BankingFacade, atmWithdrawal invokes a method of AccountDebitor. In the same way, issueFD() first invokes AccountDebitor to debit the amount of FD and then calls FDIssuer to issue the FD.
- The important thing to note is that the Client remains unaware of the complexities of
issueFD()operations it invokes on
BankingFacade. This is because the
BankingFacadesimplifies the underlying banking sub-system complexity by having simple input and output. But internally
BankingFacadenavigates through the sub-system complexity to make the banking operations work.
Summary: In the above tutorial we looked at what is facade design pattern including what advantages it provides. We then looked at the class diagram for a facade design pattern & its explanation, class diagram for a Java example of Facade pattern & its explanation. This concludes the tutorial on facade design pattern.