Facade Design Pattern in Java
Facade Design Pattern provides a unified interface to a set of interfaces in a subsystem. And defines higher level interface that makes subsystem easier to use.
Structure Summary
- Take a subsystem with a complex interface and wrap it with a new class that provides a simplified interface
- Identify a simpler, unified interface for the subsystem or component.
- Design a “wrapper” class that encapsulates the subsystem.
- The facade/wrapper captures the complexity and collaborations of the component, and delegates to the appropriate methods.
- The client uses (is coupled to) the Façade only.
The classes and objects participating in this pattern are:
Subsystem classes
Subsystem classes forms the complex interface of the existing subsystem
Facade
Façade provides the simplified interface which delegates calls to the classes in the subsystem
Client
Client invokes the Façade which shields away complex interface from the client
UML Diagram
Implementation steps
Step 1 — Analyze the existing complex subsystems or components
Step 2 — Create a facade/wrapper that simplifies the complex interactions with the existing subsystems or components
Step 3 — Create a client that interacts with Facade
Code Example
Step 1:
public class BillingSystem {public Bill createBill(Integer amount){// Let’s assume some advanced logic happens here;return new Bill(amount);}}public class Bill {private Integer amount;public Bill(Integer amount){this.amount = amount;}public Integer getAmount() {return amount;}}public class InvoiceCustomerSystem {public void createInvoiceForBill(Bill bill){System.out.println(“Creating invoice for bill with amount: “+bill.getAmount());}}public class Client {public static void main(String [] args){BillingSystem = new BillingSystem();InvoiceCustomerSystem = new InvoiceCustomerSystem();Bill = billingSystem.createBill(1000);invoiceCustomerSystem.createInvoiceForBill(bill);}}
Here the client should have the knowledge of internal behavior of the subsystem classes means client should know that the createBill and createInvoiceForBill methods of BillingSystem and invoiceCustomerSystem respectively exists.
Step 2:
public class FinancialSystemFacade {private BillingSystem;private InvoiceCustomerSystem;public void createInvoice(Integer amount){Bill = billingSystem.createBill(amount);invoiceCustomerSystem.createInvoiceForBill(bill);}public void setBillingSystem(BillingSystem billingSystem) {this.billingSystem = billingSystem;}public void setInvoiceCustomerSystem(InvoiceCustomerSystem invoiceCustomerSystem) {this.invoiceCustomerSystem = invoiceCustomerSystem;}}
Step 3:
public class Client {public static void main(String [] args){// initial setupBillingSystem = new BillingSystem();InvoiceCustomerSystem = new InvoiceCustomerSystem();FinancialSystemFacade = new FinancialSystemFacade();financialSystemFacade.setBillingSystem(billingSystem);financialSystemFacade.setInvoiceCustomerSystem(invoiceCustomerSystem);financialSystemFacade.createInvoice(1000);}}
Here the client can directly create the Façade class and can invoke the createInvoice method. He does not have to know about the internal behaviors like createBill and createInvoiceForBill methods of the subsystem classes namely BillingSystem and InvoiceCustomerSystem.
Summary
The Facade is a Structural Design Pattern
Use Facade when we have to deal with a complex system/subsystem having lots of functionalities
I hope this explanation and Strategy Pattern code example have been helpful. If you like this tutorial please give a clap.