From 325b580ccc61529ee6ed4f4aea34c667ae80d9c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ilkka=20Sepp=C3=A4l=C3=A4?= Date: Sun, 26 May 2024 06:14:59 +0300 Subject: [PATCH] docs: update facade --- facade/README.md | 67 +++++++++++++++++++++++++++--------------------- 1 file changed, 38 insertions(+), 29 deletions(-) diff --git a/facade/README.md b/facade/README.md index b57db7405..0bc261888 100644 --- a/facade/README.md +++ b/facade/README.md @@ -3,9 +3,13 @@ title: Facade category: Structural language: en tag: + - Abstraction + - API design - Code simplification + - Decoupling - Encapsulation - Gang Of Four + - Interface - Object composition --- @@ -17,7 +21,7 @@ Provide a unified interface to a set of interfaces in a subsystem. Facade define 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 @@ -29,7 +33,9 @@ Wikipedia says **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 @@ -161,35 +167,37 @@ public class DwarvenGoldmineFacade { Now let's use the facade: ```java -var facade = new DwarvenGoldmineFacade(); -facade.startNewDay(); -facade.digOutGold(); -facade.endDay(); +public static void main(String[] args) { + var facade = new DwarvenGoldmineFacade(); + facade.startNewDay(); + facade.digOutGold(); + facade.endDay(); +} ``` Program output: -```java -// Dwarf gold digger wakes up. -// Dwarf gold digger goes to the mine. -// Dwarf cart operator wakes up. -// Dwarf cart operator goes to the mine. -// Dwarven tunnel digger wakes up. -// Dwarven tunnel digger goes to the mine. -// Dwarf gold digger digs for gold. -// Dwarf cart operator moves gold chunks out of the mine. -// Dwarven tunnel digger creates another promising tunnel. -// Dwarf gold digger goes home. -// Dwarf gold digger goes to sleep. -// Dwarf cart operator goes home. -// Dwarf cart operator goes to sleep. -// Dwarven tunnel digger goes home. -// Dwarven tunnel digger goes to sleep. +``` +06:07:20.676 [main] INFO com.iluwatar.facade.DwarvenMineWorker -- Dwarf gold digger wakes up. +06:07:20.678 [main] INFO com.iluwatar.facade.DwarvenMineWorker -- Dwarf gold digger goes to the mine. +06:07:20.678 [main] INFO com.iluwatar.facade.DwarvenMineWorker -- Dwarf cart operator wakes up. +06:07:20.678 [main] INFO com.iluwatar.facade.DwarvenMineWorker -- Dwarf cart operator goes to the mine. +06:07:20.678 [main] INFO com.iluwatar.facade.DwarvenMineWorker -- Dwarven tunnel digger wakes up. +06:07:20.678 [main] INFO com.iluwatar.facade.DwarvenMineWorker -- Dwarven tunnel digger goes to the mine. +06:07:20.678 [main] INFO com.iluwatar.facade.DwarvenGoldDigger -- Dwarf gold digger digs for gold. +06:07:20.678 [main] INFO com.iluwatar.facade.DwarvenCartOperator -- Dwarf cart operator moves gold chunks out of the mine. +06:07:20.678 [main] INFO com.iluwatar.facade.DwarvenTunnelDigger -- Dwarven tunnel digger creates another promising tunnel. +06:07:20.678 [main] INFO com.iluwatar.facade.DwarvenMineWorker -- Dwarf gold digger goes home. +06:07:20.678 [main] INFO com.iluwatar.facade.DwarvenMineWorker -- Dwarf gold digger goes to sleep. +06:07:20.678 [main] INFO com.iluwatar.facade.DwarvenMineWorker -- Dwarf cart operator goes home. +06:07:20.678 [main] INFO com.iluwatar.facade.DwarvenMineWorker -- Dwarf cart operator goes to sleep. +06:07:20.678 [main] INFO com.iluwatar.facade.DwarvenMineWorker -- Dwarven tunnel digger goes home. +06:07:20.678 [main] INFO com.iluwatar.facade.DwarvenMineWorker -- Dwarven tunnel digger goes to sleep. ``` ## Class diagram -![alt text](./etc/facade.urm.png "Facade pattern class diagram") +![Facade](./etc/facade.urm.png "Facade pattern class diagram") ## Applicability @@ -201,10 +209,10 @@ Use the Facade pattern when ## Tutorials -* [DigitalOcean](https://www.digitalocean.com/community/tutorials/facade-design-pattern-in-java) -* [Refactoring Guru](https://refactoring.guru/design-patterns/facade) -* [GeekforGeeks](https://www.geeksforgeeks.org/facade-design-pattern-introduction/) -* [Tutorialspoint](https://www.tutorialspoint.com/design_pattern/facade_pattern.htm) +* [Facade Design Pattern in Java (DigitalOcean)](https://www.digitalocean.com/community/tutorials/facade-design-pattern-in-java) +* [Facade (Refactoring Guru)](https://refactoring.guru/design-patterns/facade) +* [Facade Method Design Pattern (GeekforGeeks)](https://www.geeksforgeeks.org/facade-design-pattern-introduction/) +* [Design Patterns - Facade Pattern (TutorialsPoint)](https://www.tutorialspoint.com/design_pattern/facade_pattern.htm) ## Known Uses @@ -225,10 +233,11 @@ Trade-offs: ## Related Patterns -* Often used with other design patterns like Singleton and Abstract Factory. -* Command pattern can use Facade to define an interface that simplifies methods invocation. +* [Adapter](https://java-design-patterns.com/patterns/adapter/): Facade provides a unified interface while Adapter makes two existing interfaces work together. +* [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 * [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)