docs: update facade

This commit is contained in:
Ilkka Seppälä committed 2024-05-26 06:14:59 +03:00
1 parent d6bb988e42
commit 325b580ccc
1 file changed
+38 -29
+38 -29
View File
@@ -3,9 +3,13 @@ title: Facade
category: Structural category: Structural
language: en language: en
tag: tag:
- Abstraction
- API design
- Code simplification - Code simplification
- Decoupling
- Encapsulation - Encapsulation
- Gang Of Four - Gang Of Four
- Interface
- Object composition - Object composition
--- ---
@@ -17,7 +21,7 @@ Provide a unified interface to a set of interfaces in a subsystem. Facade define
Real-world example Real-world example
> 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. > 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.
In plain words In plain words
@@ -29,7 +33,9 @@ Wikipedia says
**Programmatic Example** **Programmatic Example**
Let's take our goldmine example from above. Here we have the dwarven mine worker hierarchy. First, there's a base class `DwarvenMineWorker`: 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.
Here we have the dwarven mine worker hierarchy. First, there's a base class `DwarvenMineWorker`:
```java ```java
@@ -161,35 +167,37 @@ public class DwarvenGoldmineFacade {
Now let's use the facade: Now let's use the facade:
```java ```java
var facade = new DwarvenGoldmineFacade(); public static void main(String[] args) {
facade.startNewDay(); var facade = new DwarvenGoldmineFacade();
facade.digOutGold(); facade.startNewDay();
facade.endDay(); facade.digOutGold();
facade.endDay();
}
``` ```
Program output: Program output:
```java ```
// Dwarf gold digger wakes up. 06:07:20.676 [main] INFO com.iluwatar.facade.DwarvenMineWorker -- Dwarf gold digger wakes up.
// Dwarf gold digger goes to the mine. 06:07:20.678 [main] INFO com.iluwatar.facade.DwarvenMineWorker -- Dwarf gold digger goes to the mine.
// Dwarf cart operator wakes up. 06:07:20.678 [main] INFO com.iluwatar.facade.DwarvenMineWorker -- Dwarf cart operator wakes up.
// Dwarf cart operator goes to the mine. 06:07:20.678 [main] INFO com.iluwatar.facade.DwarvenMineWorker -- Dwarf cart operator goes to the mine.
// Dwarven tunnel digger wakes up. 06:07:20.678 [main] INFO com.iluwatar.facade.DwarvenMineWorker -- Dwarven tunnel digger wakes up.
// Dwarven tunnel digger goes to the mine. 06:07:20.678 [main] INFO com.iluwatar.facade.DwarvenMineWorker -- Dwarven tunnel digger goes to the mine.
// Dwarf gold digger digs for gold. 06:07:20.678 [main] INFO com.iluwatar.facade.DwarvenGoldDigger -- Dwarf gold digger digs for gold.
// Dwarf cart operator moves gold chunks out of the mine. 06:07:20.678 [main] INFO com.iluwatar.facade.DwarvenCartOperator -- Dwarf cart operator moves gold chunks out of the mine.
// Dwarven tunnel digger creates another promising tunnel. 06:07:20.678 [main] INFO com.iluwatar.facade.DwarvenTunnelDigger -- Dwarven tunnel digger creates another promising tunnel.
// Dwarf gold digger goes home. 06:07:20.678 [main] INFO com.iluwatar.facade.DwarvenMineWorker -- Dwarf gold digger goes home.
// Dwarf gold digger goes to sleep. 06:07:20.678 [main] INFO com.iluwatar.facade.DwarvenMineWorker -- Dwarf gold digger goes to sleep.
// Dwarf cart operator goes home. 06:07:20.678 [main] INFO com.iluwatar.facade.DwarvenMineWorker -- Dwarf cart operator goes home.
// Dwarf cart operator goes to sleep. 06:07:20.678 [main] INFO com.iluwatar.facade.DwarvenMineWorker -- Dwarf cart operator goes to sleep.
// Dwarven tunnel digger goes home. 06:07:20.678 [main] INFO com.iluwatar.facade.DwarvenMineWorker -- Dwarven tunnel digger goes home.
// Dwarven tunnel digger goes to sleep. 06:07:20.678 [main] INFO com.iluwatar.facade.DwarvenMineWorker -- Dwarven tunnel digger goes to sleep.
``` ```
## Class diagram ## Class diagram
![alt text](./etc/facade.urm.png "Facade pattern class diagram") ![Facade](./etc/facade.urm.png "Facade pattern class diagram")
## Applicability ## Applicability
@@ -201,10 +209,10 @@ Use the Facade pattern when
## Tutorials ## Tutorials
* [DigitalOcean](https://www.digitalocean.com/community/tutorials/facade-design-pattern-in-java) * [Facade Design Pattern in Java (DigitalOcean)](https://www.digitalocean.com/community/tutorials/facade-design-pattern-in-java)
* [Refactoring Guru](https://refactoring.guru/design-patterns/facade) * [Facade (Refactoring Guru)](https://refactoring.guru/design-patterns/facade)
* [GeekforGeeks](https://www.geeksforgeeks.org/facade-design-pattern-introduction/) * [Facade Method Design Pattern (GeekforGeeks)](https://www.geeksforgeeks.org/facade-design-pattern-introduction/)
* [Tutorialspoint](https://www.tutorialspoint.com/design_pattern/facade_pattern.htm) * [Design Patterns - Facade Pattern (TutorialsPoint)](https://www.tutorialspoint.com/design_pattern/facade_pattern.htm)
## Known Uses ## Known Uses
@@ -225,10 +233,11 @@ Trade-offs:
## Related Patterns ## Related Patterns
* Often used with other design patterns like Singleton and Abstract Factory. * [Adapter](https://java-design-patterns.com/patterns/adapter/): Facade provides a unified interface while Adapter makes two existing interfaces work together.
* Command pattern can use Facade to define an interface that simplifies methods invocation. * [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.
## Credits ## Credits
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3QbO7qN) * [Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3QbO7qN)
* [Effective Java](https://amzn.to/4cGk2Jz)
* [Head First Design Patterns: Building Extensible and Maintainable Object-Oriented Software](https://amzn.to/3UpTLrG) * [Head First Design Patterns: Building Extensible and Maintainable Object-Oriented Software](https://amzn.to/3UpTLrG)