docs: add diagrams for facade, factory kit, factory method, factory, fan-out/fan-in, feature toggle, filterer, fluent interface, flyweight, front controller, function composition

This commit is contained in:
Ilkka Seppälä
2025-04-06 19:10:43 +03:00
parent aef4b37c9a
commit 9b64cdee98
21 changed files with 40 additions and 5 deletions
+4
View File
@@ -33,6 +33,10 @@ Wikipedia says
> A facade is an object that provides a simplified interface to a larger body of code, such as a class library.
Sequence diagram
![Facade sequence diagram](./etc/facade-sequence-diagram.png)
## Programmatic Example of Facade Pattern in Java
Here's an example of the Facade Design Pattern in a goldmine scenario, demonstrating how a Java facade can streamline complex operations.
Binary file not shown.

After

Width:  |  Height:  |  Size: 62 KiB

+4
View File
@@ -32,6 +32,10 @@ In plain words
> Factory kit is a configurable object builder, a factory to create factories.
Sequence diagram
![Factory Kit sequence diagram](./etc/factory-kit-sequence-diagram.png)
## Programmatic Example of Factory Kit Pattern in Java
Imagine a magical weapon factory in Java capable of creating any desired weapon using the Factory Kit Pattern. This pattern allows for configurable object builders, making it ideal for scenarios where the types of objects are not known upfront.
Binary file not shown.

After

Width:  |  Height:  |  Size: 44 KiB

+4
View File
@@ -36,6 +36,10 @@ Wikipedia says
> In class-based programming, the factory method pattern is a creational pattern that uses factory methods to deal with the problem of creating objects without having to specify the exact class of the object that will be created. This is done by creating objects by calling a factory method — either specified in an interface and implemented by child classes, or implemented in a base class and optionally overridden by derived classes—rather than by calling a constructor.
Sequence diagram
![Factory Method sequence diagram](./etc/factory-method-sequence-diagram.png)
## Programmatic Example of Factory Method Pattern in Java
The Factory Method approach is pivotal in Java Design Patterns for achieving flexible and maintainable code as we see in the following example.
Binary file not shown.

After

Width:  |  Height:  |  Size: 59 KiB

+4
View File
@@ -26,6 +26,10 @@ Wikipedia says
> Factory is an object for creating other objects formally a factory is a function or method that returns objects of a varying prototype or class.
Sequence diagram
![Factory sequence diagram](./etc/factory-sequence-diagram.png)
## Programmatic Example of Factory Pattern in Java
Imagine an alchemist who is about to manufacture coins. The alchemist must be able to create both gold and copper coins and switching between them must be possible without modifying the existing source code. The factory pattern makes it possible by providing a static construction method which can be called with relevant parameters.
Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

+4
View File
@@ -35,6 +35,10 @@ Wikipedia says
>
> The fan-in concept, on the other hand, typically refers to the aggregation of multiple inputs. In digital electronics, it describes the number of inputs a logic gate can handle. Combining these concepts, the Fan-Out/Fan-In pattern in software engineering involves distributing tasks (fan-out) and then aggregating the results (fan-in).
Sequence diagram
![Fan-Out/Fan-In flowchart](./etc/fan-out-fan-in-flowchart.png)
## Programmatic Example of Fan-Out/Fan-In Pattern in Java
The provided implementation involves a list of numbers with the objective to square them and aggregate the results. The `FanOutFanIn` class receives the list of numbers as `SquareNumberRequest` objects and a `Consumer` instance that collects the squared results as the requests complete. Each `SquareNumberRequest` squares its number with a random delay, simulating a long-running process that finishes at unpredictable times. The `Consumer` instance gathers the results from the various `SquareNumberRequest` objects as they become available at different times.
Binary file not shown.

After

Width:  |  Height:  |  Size: 61 KiB

+4
View File
@@ -34,6 +34,10 @@ Wikipedia says
> A feature toggle in software development provides an alternative to maintaining multiple feature branches in source code. A condition within the code enables or disables a feature during runtime. In agile settings the toggle is used in production, to switch on the feature on demand, for some or all the users.
Flowchart
![Feature Toggle flowchart](./etc/feature-toggle-flowchart.png)
## Programmatic Example of Feature Toggle Pattern in Java
This Java code example demonstrates how to display a feature when it is enabled by the developer and the user is a Premium member of the application. This approach is useful for managing subscription-locked features.
Binary file not shown.

After

Width:  |  Height:  |  Size: 45 KiB

+4
View File
@@ -33,6 +33,10 @@ In plain words
> Filterer pattern is a design pattern that helps container-like objects return filtered versions of themselves.
Flowchart
![Filterer flowchart](./etc/filterer-flowchart.png)
## Programmatic Example of Filterer Pattern in Java
To illustrate, we use the Filterer design pattern for a malware detection system in Java. This system can filter threats based on various criteria, showcasing the patterns flexibility and dynamic nature. In the design we have to take into consideration that new Threat types can be added later. Additionally, there is a requirement that the threat detection system can filter the detected threats based on different criteria (the target system acts as container-like object for threats).
Binary file not shown.

After

Width:  |  Height:  |  Size: 39 KiB

+4
View File
@@ -35,6 +35,10 @@ Wikipedia says
> In software engineering, a fluent interface is an object-oriented API whose design relies extensively on method chaining. Its goal is to increase code legibility by creating a domain-specific language (DSL).
Sequence diagram
![Fluent Interface sequence diagram](./etc/fluent-interface-sequence-diagram.png)
## Programmatic Example of Fluent Interface Pattern in Java
We need to select numbers based on different criteria from the list. It's a great chance to utilize fluent interface pattern to provide readable easy-to-use developer experience.
Binary file not shown.

After

Width:  |  Height:  |  Size: 89 KiB

+4
View File
@@ -30,6 +30,10 @@ Wikipedia says
> In computer programming, flyweight is a software design pattern. A flyweight is an object that minimizes memory use by sharing as much data as possible with other similar objects; it is a way to use objects in large numbers when a simple repeated representation would use an unacceptable amount of memory.
Sequence diagram
![Flyweight sequence diagram](./etc/flyweight-sequence-diagram.png)
## Programmatic Example of Flyweight Pattern in Java
Alchemist's shop has shelves full of magic potions. Many of the potions are the same so there is no need to create a new object for each of them. Instead, one object instance can represent multiple shelf items so the memory footprint remains small.
Binary file not shown.

After

Width:  |  Height:  |  Size: 74 KiB

-1
View File
@@ -38,7 +38,6 @@ Architecture diagram
![Front Controller Architecture Diagram](./etc/front-controller-architecture-diagram.png)
## Programmatic Example of Front Controller Pattern in Java
The Front Controller design pattern is a pattern that provides a centralized entry point for handling all requests in a web application. It ensures that request handling is managed consistently and efficiently across an application.
+4 -4
View File
@@ -39,6 +39,10 @@ Wikipedia says
> Function composition is an act or mechanism to combine simple functions to build more complicated ones. Like the usual composition of functions in mathematics, the result of each function is passed as the argument of the next, and the result of the last one is the result of the whole.
Sequence diagram
![Function Composition sequence diagram](./etc/function-composition-sequence-diagram.png)
## Programmatic Example of Function Composition Pattern in Java
In the functional programming paradigm, function composition is a powerful technique. For instance, in Java, you can use higher-order functions to compose operations like multiplying and squaring numbers.
@@ -81,10 +85,6 @@ Result of composing 'timesTwo' and 'square' functions applied to 3 is: 36
This example demonstrates how the Function Composition pattern can be used to create complex functions by composing simpler ones, enhancing modularity and reusability of function-based logic.
## Function Composition Pattern Sequence diagram
![Functional Composition Diagram](./etc/function.composition.urm.png "Functional Composition")
## When to Use the Function Composition Pattern in Java
Use the Function Composition pattern when:
Binary file not shown.

After

Width:  |  Height:  |  Size: 34 KiB