Skip to content

Commit 325b580

Browse files
committed
docs: update facade
1 parent d6bb988 commit 325b580

File tree

1 file changed

+38
-29
lines changed

1 file changed

+38
-29
lines changed

facade/README.md

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@ title: Facade
33
category: Structural
44
language: en
55
tag:
6+
- Abstraction
7+
- API design
68
- Code simplification
9+
- Decoupling
710
- Encapsulation
811
- Gang Of Four
12+
- Interface
913
- Object composition
1014
---
1115

@@ -17,7 +21,7 @@ Provide a unified interface to a set of interfaces in a subsystem. Facade define
1721

1822
Real-world example
1923

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

@@ -29,7 +33,9 @@ Wikipedia says
2933
3034
**Programmatic Example**
3135

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

3440
```java
3541

@@ -161,35 +167,37 @@ public class DwarvenGoldmineFacade {
161167
Now let's use the facade:
162168

163169
```java
164-
var facade = new DwarvenGoldmineFacade();
165-
facade.startNewDay();
166-
facade.digOutGold();
167-
facade.endDay();
170+
public static void main(String[] args) {
171+
var facade = new DwarvenGoldmineFacade();
172+
facade.startNewDay();
173+
facade.digOutGold();
174+
facade.endDay();
175+
}
168176
```
169177

170178
Program output:
171179

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.
188196
```
189197

190198
## Class diagram
191199

192-
![alt text](./etc/facade.urm.png "Facade pattern class diagram")
200+
![Facade](./etc/facade.urm.png "Facade pattern class diagram")
193201

194202
## Applicability
195203

@@ -201,10 +209,10 @@ Use the Facade pattern when
201209

202210
## Tutorials
203211

204-
* [DigitalOcean](https://www.digitalocean.com/community/tutorials/facade-design-pattern-in-java)
205-
* [Refactoring Guru](https://refactoring.guru/design-patterns/facade)
206-
* [GeekforGeeks](https://www.geeksforgeeks.org/facade-design-pattern-introduction/)
207-
* [Tutorialspoint](https://www.tutorialspoint.com/design_pattern/facade_pattern.htm)
212+
* [Facade Design Pattern in Java (DigitalOcean)](https://www.digitalocean.com/community/tutorials/facade-design-pattern-in-java)
213+
* [Facade (Refactoring Guru)](https://refactoring.guru/design-patterns/facade)
214+
* [Facade Method Design Pattern (GeekforGeeks)](https://www.geeksforgeeks.org/facade-design-pattern-introduction/)
215+
* [Design Patterns - Facade Pattern (TutorialsPoint)](https://www.tutorialspoint.com/design_pattern/facade_pattern.htm)
208216

209217
## Known Uses
210218

@@ -225,10 +233,11 @@ Trade-offs:
225233

226234
## Related Patterns
227235

228-
* 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.
230238

231239
## Credits
232240

233241
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3QbO7qN)
242+
* [Effective Java](https://amzn.to/4cGk2Jz)
234243
* [Head First Design Patterns: Building Extensible and Maintainable Object-Oriented Software](https://amzn.to/3UpTLrG)

0 commit comments

Comments
 (0)