You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: facade/README.md
+40-13
Original file line number
Diff line number
Diff line change
@@ -10,26 +10,32 @@ tags:
10
10
---
11
11
12
12
## Intent
13
-
Provide a unified interface to a set of interfaces in a subsystem.
14
-
Facade defines a higher-level interface that makes the subsystem easier to use.
13
+
14
+
Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level
15
+
interface that makes the subsystem easier to use.
15
16
16
17
## Explanation
17
18
18
19
Real world example
19
20
20
-
> How does a goldmine work? "Well, the miners go down there and dig gold!" you say. That is what you believe because you are using a simple interface that goldmine provides on the outside, internally it has to do a lot of stuff to make it happen. This simple interface to the complex subsystem is a facade.
21
+
> How does a goldmine work? "Well, the miners go down there and dig gold!" you say. That is what you
22
+
> believe because you are using a simple interface that goldmine provides on the outside, internally
23
+
> it has to do a lot of stuff to make it happen. This simple interface to the complex subsystem is a
24
+
> facade.
21
25
22
26
In plain words
23
27
24
28
> Facade pattern provides a simplified interface to a complex subsystem.
25
29
26
30
Wikipedia says
27
31
28
-
> A facade is an object that provides a simplified interface to a larger body of code, such as a class library.
32
+
> A facade is an object that provides a simplified interface to a larger body of code, such as a
33
+
> class library.
29
34
30
35
**Programmatic Example**
31
36
32
-
Taking our goldmine example from above. Here we have the dwarven mine worker hierarchy
37
+
Let's take our goldmine example from above. Here we have the dwarven mine worker hierarchy. First
38
+
there's a base class `DwarvenMineWorker`:
33
39
34
40
```java
35
41
publicabstractclassDwarvenMineWorker {
@@ -87,7 +93,12 @@ public abstract class DwarvenMineWorker {
87
93
GO_TO_SLEEP, WAKE_UP, GO_HOME, GO_TO_MINE, WORK
88
94
}
89
95
}
96
+
```
90
97
98
+
Then we have the concrete dwarf classes `DwarvenTunnelDigger`, `DwarvenGoldDigger` and
// Dwarf cart operator moves gold chunks out of the mine.
185
202
// Dwarven tunnel digger creates another promising tunnel.
186
-
facade.endDay();
187
203
// Dwarf gold digger goes home.
188
204
// Dwarf gold digger goes to sleep.
189
205
// Dwarf cart operator goes home.
@@ -193,14 +209,25 @@ facade.endDay();
193
209
```
194
210
195
211
## Class diagram
212
+
196
213

197
214
198
215
## Applicability
216
+
199
217
Use the Facade pattern when
200
218
201
-
* you want to provide a simple interface to a complex subsystem. Subsystems often get more complex as they evolve. Most patterns, when applied, result in more and smaller classes. This makes the subsystem more reusable and easier to customize, but it also becomes harder to use for clients that don't need to customize it. A facade can provide a simple default view of the subsystem that is good enough for most clients. Only clients needing more customizability will need to look beyond the facade.
202
-
* there are many dependencies between clients and the implementation classes of an abstraction. Introduce a facade to decouple the subsystem from clients and other subsystems, thereby promoting subsystem independence and portability.
203
-
* you want to layer your subsystems. Use a facade to define an entry point to each subsystem level. If subsystems are dependent, then you can simplify the dependencies between them by making them communicate with each other solely through their facades.
219
+
* You want to provide a simple interface to a complex subsystem. Subsystems often get more complex
220
+
as they evolve. Most patterns, when applied, result in more and smaller classes. This makes the
221
+
subsystem more reusable and easier to customize, but it also becomes harder to use for clients that
222
+
don't need to customize it. A facade can provide a simple default view of the subsystem that is good
223
+
enough for most clients. Only clients needing more customization will need to look beyond the
224
+
facade.
225
+
* There are many dependencies between clients and the implementation classes of an abstraction.
226
+
Introduce a facade to decouple the subsystem from clients and other subsystems, thereby promoting
227
+
subsystem independence and portability.
228
+
* You want to layer your subsystems. Use a facade to define an entry point to each subsystem level.
229
+
If subsystems are dependent, then you can simplify the dependencies between them by making them
230
+
communicate with each other solely through their facades.
0 commit comments