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
+38-29Lines changed: 38 additions & 29 deletions
Original file line number
Diff line number
Diff line change
@@ -3,9 +3,13 @@ title: Facade
3
3
category: Structural
4
4
language: en
5
5
tag:
6
+
- Abstraction
7
+
- API design
6
8
- Code simplification
9
+
- Decoupling
7
10
- Encapsulation
8
11
- Gang Of Four
12
+
- Interface
9
13
- Object composition
10
14
---
11
15
@@ -17,7 +21,7 @@ Provide a unified interface to a set of interfaces in a subsystem. Facade define
17
21
18
22
Real-world example
19
23
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.
24
+
> Imagine a home theater system with multiple components: a DVD player, projector, surround sound system, and lights. Each component has a complex interface with numerous functions and settings. To simplify the use of the home theater system, a remote control (the Facade) is provided. The remote control offers a unified interface with simple buttons like "Play Movie," "Stop," "Pause," and "Volume Up/Down," which internally communicate with the various components, managing their interactions. This makes the system easier to use without needing to understand the detailed operations of each component.
21
25
22
26
In plain words
23
27
@@ -29,7 +33,9 @@ Wikipedia says
29
33
30
34
**Programmatic Example**
31
35
32
-
Let's take our goldmine example from above. Here we have the dwarven mine worker hierarchy. First, there's a base class `DwarvenMineWorker`:
36
+
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.
37
+
38
+
Here we have the dwarven mine worker hierarchy. First, there's a base class `DwarvenMineWorker`:
33
39
34
40
```java
35
41
@@ -161,35 +167,37 @@ public class DwarvenGoldmineFacade {
161
167
Now let's use the facade:
162
168
163
169
```java
164
-
var facade =newDwarvenGoldmineFacade();
165
-
facade.startNewDay();
166
-
facade.digOutGold();
167
-
facade.endDay();
170
+
publicstaticvoid main(String[] args) {
171
+
var facade =newDwarvenGoldmineFacade();
172
+
facade.startNewDay();
173
+
facade.digOutGold();
174
+
facade.endDay();
175
+
}
168
176
```
169
177
170
178
Program output:
171
179
172
-
```java
173
-
// Dwarf gold digger wakes up.
174
-
// Dwarf gold digger goes to the mine.
175
-
// Dwarf cart operator wakes up.
176
-
// Dwarf cart operator goes to the mine.
177
-
// Dwarven tunnel digger wakes up.
178
-
// Dwarven tunnel digger goes to the mine.
179
-
// Dwarf gold digger digs for gold.
180
-
// Dwarf cart operator moves gold chunks out of the mine.
181
-
// Dwarven tunnel digger creates another promising tunnel.
182
-
// Dwarf gold digger goes home.
183
-
// Dwarf gold digger goes to sleep.
184
-
// Dwarf cart operator goes home.
185
-
// Dwarf cart operator goes to sleep.
186
-
// Dwarven tunnel digger goes home.
187
-
// Dwarven tunnel digger goes to sleep.
180
+
```
181
+
06:07:20.676 [main] INFO com.iluwatar.facade.DwarvenMineWorker -- Dwarf gold digger wakes up.
182
+
06:07:20.678 [main] INFO com.iluwatar.facade.DwarvenMineWorker -- Dwarf gold digger goes to the mine.
183
+
06:07:20.678 [main] INFO com.iluwatar.facade.DwarvenMineWorker -- Dwarf cart operator wakes up.
184
+
06:07:20.678 [main] INFO com.iluwatar.facade.DwarvenMineWorker -- Dwarf cart operator goes to the mine.
185
+
06:07:20.678 [main] INFO com.iluwatar.facade.DwarvenMineWorker -- Dwarven tunnel digger wakes up.
186
+
06:07:20.678 [main] INFO com.iluwatar.facade.DwarvenMineWorker -- Dwarven tunnel digger goes to the mine.
187
+
06:07:20.678 [main] INFO com.iluwatar.facade.DwarvenGoldDigger -- Dwarf gold digger digs for gold.
188
+
06:07:20.678 [main] INFO com.iluwatar.facade.DwarvenCartOperator -- Dwarf cart operator moves gold chunks out of the mine.
189
+
06:07:20.678 [main] INFO com.iluwatar.facade.DwarvenTunnelDigger -- Dwarven tunnel digger creates another promising tunnel.
190
+
06:07:20.678 [main] INFO com.iluwatar.facade.DwarvenMineWorker -- Dwarf gold digger goes home.
191
+
06:07:20.678 [main] INFO com.iluwatar.facade.DwarvenMineWorker -- Dwarf gold digger goes to sleep.
192
+
06:07:20.678 [main] INFO com.iluwatar.facade.DwarvenMineWorker -- Dwarf cart operator goes home.
193
+
06:07:20.678 [main] INFO com.iluwatar.facade.DwarvenMineWorker -- Dwarf cart operator goes to sleep.
194
+
06:07:20.678 [main] INFO com.iluwatar.facade.DwarvenMineWorker -- Dwarven tunnel digger goes home.
195
+
06:07:20.678 [main] INFO com.iluwatar.facade.DwarvenMineWorker -- Dwarven tunnel digger goes to sleep.
188
196
```
189
197
190
198
## Class diagram
191
199
192
-

200
+

*Often used with other design patterns like Singleton and Abstract Factory.
229
-
*Command pattern can use Facade to define an interface that simplifies methods invocation.
236
+
*[Adapter](https://java-design-patterns.com/patterns/adapter/): Facade provides a unified interface while Adapter makes two existing interfaces work together.
237
+
*[Mediator](https://java-design-patterns.com/patterns/mediator/): Facade defines a simpler interface to a subsystem while Mediator centralizes complex communications and control between objects.
230
238
231
239
## Credits
232
240
233
241
*[Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3QbO7qN)
242
+
*[Effective Java](https://amzn.to/4cGk2Jz)
234
243
*[Head First Design Patterns: Building Extensible and Maintainable Object-Oriented Software](https://amzn.to/3UpTLrG)
0 commit comments