Facade Pattern is a Structural Pattern in which, a set of interfaces is gathered into only one interface that Client can work easily and simply. Facade Pattern helps us to wrap complexities of components inside and lose coupling between Client and subSystems.
I. OVERVIEW
Facade Pattern defines a higher interface/object – Facade – that wraps some lower interfaces/objects. Whenever Client wants to make a chain of complex actions which require many methods and interaction, it just call one simple Facade method, all operation can be done inside that method.
You can see that Facade object contains other interfaces/objects (A, B, C) and manages their operation by doWork()
method.
II. PRACTICE
1. Project Overview
Assume that we have some operations such as: register, consultation, healthcare that a Customer will be served when applying for our company’s Customer Services.
With Facade Pattern, Client only need to create Customer Information, then calls FacadeService setServiceforCustomer()
method. All works containing register information, Consultation Service and HealhCare Service will be processed behind.
2. Step by Step
2.1- Create Customer
class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package com.javasampleapproach.facade.pattern; public class Customer { private String name; public Customer(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Customer [name=" + name + "]"; } } |
2.2- Create ICustomerService
interface:
1 2 3 4 5 6 |
package com.javasampleapproach.facade.pattern; public interface ICustomerService { public void serve(String name); } |
2.3- Create implementations of ICustomerService
interface:
Consultation.java
1 2 3 4 5 6 7 8 9 |
package com.javasampleapproach.facade.pattern; public class Consultation implements ICustomerService { @Override public void serve(String name) { System.out.println("Consultation for customer: " + name); } } |
HealthCare.java
1 2 3 4 5 6 7 8 9 |
package com.javasampleapproach.facade.pattern; public class HealthCare implements ICustomerService { @Override public void serve(String name) { System.out.println("Healthcare for customer: " + name); } } |
2.4- Create FacadeService
class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package com.javasampleapproach.facade.pattern; public class FacadeService { private ICustomerService consultation; private ICustomerService healthcare; public FacadeService() { consultation = new Consultation(); healthcare = new HealthCare(); } public void setServiceforCustomer(Customer customer) { registerCustomer(customer.getName()); consultation.serve(customer.getName()); healthcare.serve(customer.getName()); } private void registerCustomer(String name) { System.out.println("register for customer: " + name); } } |
2.5- Create Client Test Class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package com.javasampleapproach.facade; import com.javasampleapproach.facade.pattern.Customer; import com.javasampleapproach.facade.pattern.FacadeService; public class MainApp { public static void main(String[] args) { Customer customer = new Customer("Jack Smith"); FacadeService service = new FacadeService(); service.setServiceforCustomer(customer); } } |
2.6- Run the code, the console window shows:
1 2 3 |
register for customer: Jack Smith Consultation for customer: Jack Smith Healthcare for customer: Jack Smith |