Façade pattern hides the complexities of the system, and provides a simpler interface to clients. Façade wraps a complicated subsystem with a simpler interface, and decouples the subsystems from its clients.
Example:
In the class diagram below, we can see: When a client needs to retrieve user address information, or salaray information, it will talks to UserFacade class only. UserFacade will provide a simple interface to the client, and communicate with its sub systems: UserProfileManager, Salarymanager etc.
Check List
Here is the check list to apply/implement Façade Pattern.
- Define a simpler interface for the subsystems
- Design a facade class that encapsulates the subsystem
- The facade captures the complexity of the subsystem, and delegates to the appropriate methods
- The client uses the Facade
Implementation
Step 1 – create subsystem interface
1 2 3 4 5 6 7 |
package net.tecbar.designpattern.facade; public interface UserProfileManager { String getAddress(String id); } |
1 2 3 4 5 6 7 8 9 |
package net.tecbar.designpattern.facade; import java.math.BigDecimal; public interface SalaryManager { BigDecimal getSalary(String id); } |
Step 2 – Create classes which implement the above interface
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package net.tecbar.designpattern.facade; public class UserProfileManagerImpl implements UserProfileManager { @Override public String getAddress(String id) { // put your logic to find data (for example: search database etc.) return "850 Julia Dr, Santa Clara, CA 95114"; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package net.tecbar.designpattern.facade; import java.math.BigDecimal; public class SalaryManagerImpl implements SalaryManager { @Override public BigDecimal getSalary(String id) { // put your logic to find data (for example: search database etc.) return new BigDecimal("6500.08"); } } |
step 3 – create façade class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package net.tecbar.designpattern.facade; import java.math.BigDecimal; public class UserFacade { private final static UserFacade instance = new UserFacade(); private UserProfileManager profileManager; private SalaryManager salaryManager; private UserFacade() { profileManager = new UserProfileManagerImpl(); salaryManager = new SalaryManagerImpl(); } public static UserFacade getInstance() { return instance; } BigDecimal getSalary(String id) { return salaryManager.getSalary(id); } String getAddress(String id) { return profileManager.getAddress(id); } } |
Step 3 – write a client to talk to façade
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package net.tecbar.designpattern.facade; import java.math.BigDecimal; public class FacadeDemo { public static void main(String[] args) { BigDecimal salary = UserFacade.getInstance().getSalary("W76530"); String address = UserFacade.getInstance().getAddress("W76530"); System.out.println("Salary: " + salary); System.out.println("Address: " + address); } } |
output:
1 2 3 4 |
Salary: 6500.08 Address: 850 Julia Dr, Santa Clara, CA 95114 |
Pingback: Software Design Patterns