Facade Pattern
Bryan Hansen
twitter: bh5k | [Link]
Concepts
▪ Make an API easier to use
▪ Reduce dependencies on outside code
▪ Simplify the interface or client usage
▪ Usually a refactoring pattern
▪ Examples:
▪ [Link]
▪ [Link]
Design
Class that utilizes composition
Shouldn’t have a need for inheritance
Typically encompasses full lifecycle
UML
Everyday Example - URL
URL url = new URL("http", "[Link]", 80,
"/author/bryan-‐hansen");
BufferedReader in = new BufferedReader(
new InputStreamReader([Link]()));
String inputLine;
while ((inputLine = [Link]()) != null) {
[Link](inputLine);
}
Exercise Facade
Complex Client
Client, Facade, JDBC
Simplified Client Code
Pitfalls
▪ Typically used to clean up code
▪ Should think about API design
▪ Flat problem/structure
▪ The “Singleton” of Structural Pattern
Contrast
Facade Adapter
▪ Simplifies Interface ▪ Also a refactoring pattern
▪ Works with composites ▪ Modifies behavior (adds)
▪ Cleaner API ▪ Provides a different interface
▪ Single Object
Facade Summary
• Simplifies Client Interface
• Easy Pattern to implement
• Refactoring Pattern