docs: add diagrams for marker interface, master-worker, mediator, memento, metadata mapping, microservices api gateway, microservices log aggregation, monad, monitor, monostate, multiton, mute idiom

This commit is contained in:
Ilkka Seppälä
2025-04-07 22:14:20 +03:00
parent 72eeb167c5
commit 8d5cb59388
24 changed files with 48 additions and 4 deletions
+4
View File
@@ -37,6 +37,10 @@ Wikipedia says
>
> To use this pattern, a class implements a marker interface (also called tagging interface) which is an empty interface, and methods that interact with instances of that class test for the existence of the interface. Whereas a typical interface specifies functionality (in the form of method declarations) that an implementing class must support, a marker interface need not do so. The mere presence of such an interface indicates specific behavior on the part of the implementing class. Hybrid interfaces, which both act as markers and specify required methods, are possible but may prove confusing if improperly used.
Flowchart
![Marker Interface flowchart](./etc/marker-interface-flowchart.png)
## Programmatic Example of Marker Interface Pattern in Java
The Marker Interface design pattern is a design pattern in computer science that is used with languages that provide run-time type information about objects. It provides a means to associate metadata with a class where the language does not have explicit support for such metadata.
Binary file not shown.

After

Width:  |  Height:  |  Size: 83 KiB

+4
View File
@@ -33,6 +33,10 @@ Wikipedia says
> Masterslave is a model of asymmetric communication or control where one device or process (the master) controls one or more other devices or processes (the slaves) and serves as their communication hub. In some systems, a master is selected from a group of eligible devices, with the other devices acting in the role of slaves.
Sequence diagram
![Master-Worker sequence diagram](./etc/master-worker-sequence-diagram.png)
## Programmatic Example of Master-Worker Pattern in Java
In the provided code, the `MasterWorker` class initiates the concurrent computation process. The `Master` class divides the work among `Worker` objects, each performing its task in parallel, thus optimizing task processing and enhancing system efficiency.
Binary file not shown.

After

Width:  |  Height:  |  Size: 53 KiB

+4 -4
View File
@@ -33,6 +33,10 @@ Wikipedia says
> In software engineering, the mediator pattern defines an object that encapsulates how a set of objects interact. This pattern is considered to be a behavioral pattern due to the way it can alter the program's running behavior. In object-oriented programming, programs often consist of many classes. Business logic and computation are distributed among these classes. However, as more classes are added to a program, especially during maintenance and/or refactoring, the problem of communication between these classes may become more complex. This makes the program harder to read and maintain. Furthermore, it can become difficult to change the program, since any change may affect code in several other classes. With the mediator pattern, communication between objects is encapsulated within a mediator object. Objects no longer communicate directly with each other, but instead communicate through the mediator. This reduces the dependencies between communicating objects, thereby reducing coupling.
Sequence diagram
![Mediator sequence diagram](./etc/mediator-sequence-diagram.png)
## Programmatic Example of Mediator Pattern in Java
In this example, the mediator encapsulates how a set of objects interact. Instead of referring to each other directly, they use the mediator interface.
@@ -175,10 +179,6 @@ Here's the console output from running the example.
14:05:15.083 [main] INFO com.iluwatar.mediator.PartyMemberBase -- Rogue arrives for dinner
```
## Detailed Explanation of Mediator Pattern with Real-World Examples
![Mediator](./etc/mediator_1.png "Mediator")
## When to Use the Mediator Pattern in Java
Use the Mediator pattern when
Binary file not shown.

After

Width:  |  Height:  |  Size: 34 KiB

+4
View File
@@ -36,6 +36,10 @@ Wikipedia says
> The memento pattern is a software design pattern that provides the ability to restore an object to its previous state (undo via rollback).
Sequence diagram
![Memento sequence diagram](./etc/memento-sequence-diagram.png)
## Programmatic Example of Memento Pattern in Java
In our astrology application, we use the Memento pattern to capture and restore the state of star objects. Each state is saved as a memento, allowing us to revert to previous states as needed.
Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

+4
View File
@@ -29,6 +29,10 @@ Wikipedia says
> Create a "virtual [object database](https://en.wikipedia.org/wiki/Object_database)" that can be used from within the programming language.
Flowchart
![Metadata Mapping flowchart](./etc/metadata-mapping-flowchart.png)
## Programmatic Example of Metadata Mapping Pattern in Java
Hibernate ORM Tool uses Metadata Mapping Pattern to specify the mapping between classes and tables either using XML or annotations in code.
Binary file not shown.

After

Width:  |  Height:  |  Size: 73 KiB

+4
View File
@@ -38,6 +38,10 @@ Wikipedia says
> API Gateway is a server that acts as an API front-end, receives API requests, enforces throttling and security policies, passes requests to the back-end service and then passes the response back to the requester. A gateway often includes a transformation engine to orchestrate and modify the requests and responses on the fly. A gateway can also provide functionality such as collecting analytics data and providing caching. The gateway can provide functionality to support authentication, authorization, security, audit and regulatory compliance.
Sequence diagram
![Microservices API Gateway sequence diagram](./etc/microservices-api-gateway-sequence-diagram.png)
## Programmatic Example of Microservice API Gateway in Java
This implementation shows what the API Gateway pattern could look like for an e-commerce site. The`ApiGateway` makes calls to the Image and Price microservices using the `ImageClientImpl` and `PriceClientImpl` respectively. Customers viewing the site on a desktop device can see both price information and an image of a product, so the `ApiGateway` calls both of the microservices and aggregates the data in the `DesktopProduct` model. However, mobile users only see price information; they do not see a product image. For mobile users, the `ApiGateway` only retrieves price information, which it uses to populate the `MobileProduct`.
Binary file not shown.

After

Width:  |  Height:  |  Size: 69 KiB

+4
View File
@@ -38,6 +38,10 @@ Wikipedia says
> You have applied the Microservice architecture pattern. The application consists of multiple services and service instances that are running on multiple machines. Requests often span multiple service instances. Each service instance generates writes information about what it is doing to a log file in a standardized format. The log file contains errors, warnings, information and debug messages.
Flowchart
![Microservices Log Aggregration flowchart](./etc/microservices-log-aggregation-flowchart.png)
## Programmatic Example of Microservices Log Aggregation Pattern in Java
Log Aggregation is a pattern that centralizes the collection, storage, and analysis of logs from multiple sources to facilitate monitoring, debugging, and operational intelligence. It is particularly useful in distributed systems where logs from various components need to be centralized for better management and analysis.
Binary file not shown.

After

Width:  |  Height:  |  Size: 61 KiB

+4
View File
@@ -43,6 +43,10 @@ Wikipedia says
> In functional programming, a monad is a structure that combines program fragments (functions) and wraps their return values in a type with additional computation. In addition to defining a wrapping monadic type, monads define two operators: one to wrap a value in the monad type, and another to compose together functions that output values of the monad type (these are known as monadic functions). General-purpose languages use monads to reduce boilerplate code needed for common operations (such as dealing with undefined values or fallible functions, or encapsulating bookkeeping code). Functional languages use monads to turn complicated sequences of functions into succinct pipelines that abstract away control flow, and side effects.
Flowchart
![Monad flowchart](./etc/monad-flowchart.png)
## Programmatic Example of Monad Pattern in Java
Heres the Monad implementation in Java. The `Validator` class encapsulates an object and performs validation steps in a monadic fashion, showcasing the benefits of using the Monad pattern for error handling and state management.
Binary file not shown.

After

Width:  |  Height:  |  Size: 63 KiB

+4
View File
@@ -36,6 +36,10 @@ Wikipedia says
> In concurrent programming (also known as parallel programming), a monitor is a synchronization construct that allows threads to have both mutual exclusion and the ability to wait (block) for a certain condition to become false. Monitors also have a mechanism for signaling other threads that their condition has been met.
Sequence diagram
![Monitor sequence diagram](./etc/monitor-sequence-diagram.png)
## Programmatic Example of Monitor Pattern in Java
The Monitor design pattern is a synchronization technique used in concurrent programming to ensure that only one thread can execute a particular section of code at a time. It is a method of wrapping and hiding the synchronization primitives (like semaphores or locks) within the methods of an object. This pattern is useful in situations where race conditions could occur.
Binary file not shown.

After

Width:  |  Height:  |  Size: 76 KiB

+4
View File
@@ -34,6 +34,10 @@ wiki.c2.com says
> A monostate is a "conceptual singleton" - all data members of a monostate are static, so all instances of the monostate use the same (static) data. Applications using a monostate can create any number of instances that they desire, as each instance uses the same data.
Flowchart
![Monostate flowchart](./etc/monostate-flowchart.png)
## Programmatic Example of Monostate Pattern in Java
The Monostate pattern in Java ensures that all instances of a class share the same state, making it a great Singleton alternative for maintaining consistent data. This is achieved by using static fields in the class. Any changes to these fields will be reflected across all instances of the class. This pattern is useful when you want to avoid global variables but still need a shared state across multiple instances.
Binary file not shown.

After

Width:  |  Height:  |  Size: 54 KiB

+4
View File
@@ -32,6 +32,10 @@ Wikipedia says
> In software engineering, the multiton pattern is a design pattern which generalizes the singleton pattern. Whereas the singleton allows only one instance of a class to be created, the multiton pattern allows for the controlled creation of multiple instances, which it manages through the use of a map.
Flowchart
![Multiton flowchart](./etc/multiton-flowchart.png)
## Programmatic Example of Multiton Pattern in Java
In this tutorial, well explore how to implement the Multiton pattern in Java, covering its structure, benefits, and providing code examples. By following these implementation tips, youll be able to effectively utilize this Java design pattern.
Binary file not shown.

After

Width:  |  Height:  |  Size: 45 KiB

+4
View File
@@ -34,6 +34,10 @@ In plain words
> The Mute Idiom design pattern suppresses the handling of trivial or non-critical exceptions to simplify code.
Flowchart
![Mute Idiom flowchart](./etc/mute-idiom-flowchart.png)
## Programmatic Example of Mute Idiom Pattern in Java
In the following Java code example, we demonstrate the Mute Idiom by muting non-critical exceptions during the resource management process. This approach ensures error handling does not interrupt the main logic.
Binary file not shown.

After

Width:  |  Height:  |  Size: 51 KiB