Facade Design Pattern
Structural pattern. Structural design patterns are concerned with composing classes and objects into larger structures.
The Facade design pattern solves problems like: How can a simple interface be provided for a complex subsystem?
Open in the interactive referenceDownload the PDF reference
Solution
The Facade design pattern provides a solution:
Define a separate
Facade
object that provides
an unified interface for a set of interfaces
in a subsystem.
Work through a Facade
to minimize dependencies on a subsystem.
Describing the Facade design in more detail is the theme
of the following sections.
See Applicability section for all problems Facade can solve.
-
The key idea in this pattern is to
work through a separate
Facadeobject that provides a simple interface for (already existing) objects in a subsystem.
Clients can either work with a subsystem directly or itsFacade. -
Define a separate
Facadeobject:-
Define an unified interface for a set of
interfaces in a subsystem
(
Facade). -
Implement the
Facadeinterface
in terms of (by delegating to) the interfaces in the subsystem.
-
Define an unified interface for a set of
interfaces in a subsystem
(
-
Working through a
Facadeobject minimizes dependencies on a subsystem (loose coupling), which makes clients easier to implement, change, test, and reuse.
Clients that need more lower-level functionalities can access the objects in the subsystem directly.
Applicability
Design Problems
-
Making Complex Subsystems Easier to Use
- How can a simple interface be provided for a complex subsystem?
- How can a single entry point be provided for a subsystem?
-
Avoiding Tight Coupling Between Subsystems
- How can dependencies on a subsystem be minimized?
-
How can tight coupling between
clients and the objects in a subsystem be avoided?
Sample Code 1
Basic Java code for implementing the sample UML diagrams.
1 package com.sample.facade.basic;
2 public class Client {
3 // Running the Client class as application.
4 public static void main(String[] args) {
5 // Creating a facade for a subsystem.
6 Facade facade = new Facade1
7 (new Class1(), new Class2(), new Class3());
8 // Working through the facade.
9 System.out.println(facade.operation());
10 }
11 }
Facade forwards to ... Class1 Class2 Class3
1 package com.sample.facade.basic;
2 public abstract class Facade {
3 public abstract String operation();
4 }
1 package com.sample.facade.basic;
2 public class Facade1 extends Facade {
3 private Class1 object1;
4 private Class2 object2;
5 private Class3 object3;
6
7 public Facade1(Class1 object1, Class2 object2, Class3 object3) {
8 this.object1 = object1;
9 this.object2 = object2;
10 this.object3 = object3;
11 }
12 public String operation() {
13 return "Facade forwards to ... "
14 + object1.operation1()
15 + object2.operation2()
16 + object3.operation3();
17 }
18 }
1 package com.sample.facade.basic;
2 public class Class1 {
3 public String operation1() {
4 return "Class1 ";
5 }
6 }
1 package com.sample.facade.basic;
2 public class Class2 {
3 public String operation2() {
4 return "Class2 ";
5 }
6 }
1 package com.sample.facade.basic;
2 public class Class3 {
3 public String operation3() {
4 return "Class3 ";
5 }
6 }
Not included on this page: Intent, Problem, Motivation, Structure, Consequences, Implementation, Related Patterns. See the GoF Design Patterns Reference (PDF) for the complete description.