Skip to content

Commit f03a88c

Browse files
committed
updates
1 parent 442d541 commit f03a88c

File tree

2 files changed

+23
-53
lines changed

2 files changed

+23
-53
lines changed

mediator/README.md

Lines changed: 22 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,26 @@ Objects communicate through the **Mediator** rather than directly with each othe
1919

2020
As a system evolves and becomes larger and supports more complex functionality and business rules, the problem of communicating between these components becomes more complicated to understand and manage. It may be beneficial to refactor your system to centralize some or all of its functionality via some kind of mediation process.
2121

22-
The mediator pattern is similar to creating a [Facade](/facade) pattern between your classes and processes. Except the Mediator is expected to transact data both ways between two or more other classes or processes that would normally interact directly with each other.
22+
The mediator pattern is similar to implementing a [Facade](/facade) pattern between your objects and processes. Except that the structure of the Mediator allows multi directional communication between the objects or processes that would normally be interacting directly with each other.
2323

24-
The resulting Mediator interface will be very custom to the use cases that it is now supporting.
25-
26-
The Mediator will generally look like an object that is managing one of more [Observer](/observer) patterns perhaps between the other classes or processes (colleagues). Whether you use an Observer pattern to manage a particular piece of functionality or not depends on whether it is the best use of the resources you have available.
27-
28-
When refactoring your code, you may decide to approach your refactoring from the perspective of implementing an Observer pattern first. This means that all colleagues (Observers) will receive the notification whether it was intended for them or not. If you want to avoid redundant updates in the colleagues then you can write specific cases in your code, or create specific methods as I have done in [/mediator/mediator_concept.py](/mediator/mediator_concept.py) file.
29-
30-
Colleagues now will send and receive requests via a Mediator object rather than directly between each other. The Mediator is like a router in this case, but allows you to add extra programmatic functionality and also give the option of creating other kinds of colleagues that could utilize the communications in new ways.
24+
While the Facade is a structural pattern, and the Mediator also implies structure in the way that it exists between two or more other objects or processes, it also allows changing the behavior of the interaction to make it more cooperative in some way. E.g., the centralization of application logic, managing the routing behavior, caching, logging, etc.
3125

3226
## Terminology
3327

34-
* **Mediator Interface**: An interface that the Mediator and Colleagues implement. Note that different Colleagues will have varied use cases and won't need to implement all the methods described in the Mediator interface.
35-
* **Concrete Mediator**: The single source of truth and coordinator of communications between the components (colleagues).
36-
* **Colleague Classes**: One of the many types of concrete components that use the mediator for its own particular use case.
28+
* **Mediator**: The coordinator of communications between the components (colleagues).
29+
* **Colleagues**: One of the many types of concrete components that use the mediator.
3730

3831
## Mediator UML Diagram
3932

4033
![Mediator Pattern UML Diagram](/img/mediator_concept.svg)
4134

4235
## Source Code
4336

44-
In the example concept, there are two colleague classes that use each other's methods. Instead of the Colleagues calling each other's methods directly, they implement the Mediator interface and call each other via the Mediator. Each colleague class or process is designed for a different purpose, but they utilize some related functionality from each other.
37+
In the example concept, there are two colleague classes that use each other's methods. Instead of the Colleagues calling each other's methods directly, they implement the Mediator interface and call each other via the Mediator. Each colleague is designed for a different purpose, but they utilize some related functionality from each other.
4538

46-
The system would work without the Mediator, but adding the Mediator will allow extending functionality to a potential third colleague that provides a different service, such as AI analysis or monitoring, without needing to add specific support or knowledge into the two original colleagues.
39+
The system, in this case, would work without the Mediator, but adding the Mediator would allow extending functionality to a potential third colleague that provides a different service, such as AI analysis or monitoring, without needing to add specific support or knowledge into the two original colleagues.
40+
41+
In this first example the Mediator is structurally acting as a multi directional relay between the two colleagues.
4742

4843
## Output
4944

@@ -55,13 +50,13 @@ COLLEAGUE2 <--> Here is the Colleague1 specific data you asked for
5550

5651
## Example Use Case
5752

58-
This is a simplified game engine. There is the main game engine component, a scheduler that manages game events and there are game clients that act as separate game players submitting bets into a game round.
53+
In this example use case, we will implement some behavior into the mediation process.
5954

60-
All of the components implement the Mediators interface. They all implement one or some of the methods differently depending on their purpose. While they all perform different types of functionality, they all require a single source of truth being the Game Engine that acts as the Mediator.
55+
Before the mediation logic is added, consider that the below example is a series of components all subscribed to a central location being the subject. They all implement the [Observer](/observer) pattern.
6156

62-
There is mixture of this Mediator example using the [Observer](/observer) pattern to notify the game clients, as well as specific methods not necessarily shared between the scheduler, game engine and clients but benefits from being managed via the Mediator.
57+
Each component is updated independently by external forces, but when it has new information, it notifies the subject which in turn then notifies the other subscribed components.
6358

64-
Normally the processes being mediated will be running from different servers or programs, but in this example they are all part of the same client in order to demonstrate the concept easier.
59+
During the synchronization of all the subscribed components, without the extra mediation, the component that provided the new information will receive back the same message that it just notified the subject of. In order to manage the unnecessary duplicate message, the notifications will be mediated to exclude to component where the original message originated from.
6560

6661
## Example UML Diagram
6762

@@ -70,37 +65,14 @@ Normally the processes being mediated will be running from different servers or
7065
## Output
7166

7267
``` bash
73-
python.exe ./mediator/client.py
74-
Sean -> New Game, Place Bets
75-
Cosmo -> New Game, Place Bets
76-
Emmy -> New Game, Place Bets
77-
Emmy -> You Are The Winner with result `10`. You Won 12. Your balance = 310
78-
```
79-
80-
## New Coding Concepts
81-
82-
### The Underscore Only `_` Variable
83-
84-
In the `Player` class, there is a for loop that iterates the bets list.
85-
86-
``` python
87-
for _ in bets:
88-
self.balance -= 1
89-
```
90-
91-
The `_` is used instead of a more tradition `i`, `x`, `y` or another variable. If using a letter in the for loop, and not actually using it, like in the above example, then Pylint would indicate a warning of unused variable. Note that the Python interpreter would still run this code ok if using a letter as the variable name, but reducing Pylint warnings helps makes code look neater.
92-
93-
E.g.,
94-
95-
``` python
96-
for i in bets:
97-
self.balance -= 1
98-
```
99-
100-
The Pylint warning would be
101-
102-
``` bash
103-
W0612: Unused variable 'i' (unused-variable)
68+
python ./mediator/client.py
69+
Component1: >>> Out >>> : data A
70+
Component2: <<< In <<< : data A
71+
Component3: <<< In <<< : data A
72+
Component2: >>> Out >>> : data B
73+
Component3: <<< In <<< : data B
74+
Component1: <<< In <<< : data B
75+
Component3: >>> Out >>> : data C
76+
Component2: <<< In <<< : data C
77+
Component1: <<< In <<< : data C
10478
```
105-
106-
So, using the `_` prevents this warning.

observer/README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,4 @@ PS> python
137137
{'grandy', 'yankee', 'dandy'}
138138
```
139139

140-
!!! Note
141-
142-
If instantiating an empty **Set** then use `my_object = Set()` rather than `my_object = {}` to reduce ambiguity with creating an empty [Dictionary](/singleton#python-dictionary).
140+
Note, if instantiating an empty **Set** then use `my_object = Set()` rather than `my_object = {}` to reduce ambiguity with creating an empty [Dictionary](/singleton#python-dictionary).

0 commit comments

Comments
 (0)