Skip to content

Commit 0ee03db

Browse files
committed
Update README.md
1 parent c541176 commit 0ee03db

File tree

1 file changed

+40
-13
lines changed

1 file changed

+40
-13
lines changed

facade/README.md

+40-13
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,32 @@ tags:
1010
---
1111

1212
## 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.
1516

1617
## Explanation
1718

1819
Real world example
1920

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.
2125
2226
In plain words
2327

2428
> Facade pattern provides a simplified interface to a complex subsystem.
2529
2630
Wikipedia says
2731

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.
2934
3035
**Programmatic Example**
3136

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`:
3339

3440
```java
3541
public abstract class DwarvenMineWorker {
@@ -87,7 +93,12 @@ public abstract class DwarvenMineWorker {
8793
GO_TO_SLEEP, WAKE_UP, GO_HOME, GO_TO_MINE, WORK
8894
}
8995
}
96+
```
9097

98+
Then we have the concrete dwarf classes `DwarvenTunnelDigger`, `DwarvenGoldDigger` and
99+
`DwarvenCartOperator`:
100+
101+
```java
91102
public class DwarvenTunnelDigger extends DwarvenMineWorker {
92103

93104
private static final Logger LOGGER = LoggerFactory.getLogger(DwarvenTunnelDigger.class);
@@ -135,7 +146,7 @@ public class DwarvenCartOperator extends DwarvenMineWorker {
135146

136147
```
137148

138-
To operate all these goldmine workers we have the facade
149+
To operate all these goldmine workers we have the `DwarvenGoldmineFacade`:
139150

140151
```java
141152
public class DwarvenGoldmineFacade {
@@ -168,22 +179,27 @@ public class DwarvenGoldmineFacade {
168179
}
169180
```
170181

171-
Now to use the facade
182+
Now let's use the facade:
172183

173184
```java
174-
DwarvenGoldmineFacade facade = new DwarvenGoldmineFacade();
185+
var facade = new DwarvenGoldmineFacade();
175186
facade.startNewDay();
187+
facade.digOutGold();
188+
facade.endDay();
189+
```
190+
191+
Program output:
192+
193+
```java
176194
// Dwarf gold digger wakes up.
177195
// Dwarf gold digger goes to the mine.
178196
// Dwarf cart operator wakes up.
179197
// Dwarf cart operator goes to the mine.
180198
// Dwarven tunnel digger wakes up.
181199
// Dwarven tunnel digger goes to the mine.
182-
facade.digOutGold();
183200
// Dwarf gold digger digs for gold.
184201
// Dwarf cart operator moves gold chunks out of the mine.
185202
// Dwarven tunnel digger creates another promising tunnel.
186-
facade.endDay();
187203
// Dwarf gold digger goes home.
188204
// Dwarf gold digger goes to sleep.
189205
// Dwarf cart operator goes home.
@@ -193,14 +209,25 @@ facade.endDay();
193209
```
194210

195211
## Class diagram
212+
196213
![alt text](./etc/facade.urm.png "Facade pattern class diagram")
197214

198215
## Applicability
216+
199217
Use the Facade pattern when
200218

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.
204231

205232
## Credits
206233

0 commit comments

Comments
 (0)