Skip to content

Commit 137b584

Browse files
committed
updates
1 parent e328be0 commit 137b584

File tree

12 files changed

+115
-266
lines changed

12 files changed

+115
-266
lines changed

mediator/client.py

Lines changed: 15 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,15 @@
1-
"Mediator Pattern Example Code"
2-
3-
4-
from game_engine import GameEngine
5-
from game_client import GameClient
6-
from player import Player
7-
from scheduler import Scheduler
8-
9-
# The concrete GameEngine process that would run on a dedicated server
10-
GAMEENGINE = GameEngine()
11-
12-
# 3 Hypothetical game clients, all running externally on mobile phones
13-
# calling the GAMEENGINE mediator across a network proxy
14-
MOBILECLIENT1 = GameClient(GAMEENGINE)
15-
PLAYER1 = Player("Sean", 100)
16-
MOBILECLIENT1.add_player(PLAYER1)
17-
18-
MOBILECLIENT2 = GameClient(GAMEENGINE)
19-
PLAYER2 = Player("Cosmo", 200)
20-
MOBILECLIENT2.add_player(PLAYER2)
21-
22-
MOBILECLIENT3 = GameClient(GAMEENGINE)
23-
PLAYER3 = Player("Emmy", 300, )
24-
MOBILECLIENT3.add_player(PLAYER3)
25-
26-
# A scheduler is a separate process that manages game rounds and
27-
# triggers game events at time intervals
28-
SCHEDULER = Scheduler(GAMEENGINE)
29-
SCHEDULER.new_game()
30-
31-
# 3 Hypothetical game clients have received notifications of new game,
32-
# they now place there bets
33-
PLAYER1.place_bets([1, 2, 3])
34-
PLAYER2.place_bets([5, 6, 7, 8])
35-
PLAYER3.place_bets([10, 11])
36-
37-
# The scheduler closes the round, and triggers the result and
38-
# winnings notifications
39-
SCHEDULER.calculate_winners()
40-
SCHEDULER.notify_winners()
1+
"The Mediator Use Case Example"
2+
from component import Component
3+
from mediator import Mediator
4+
5+
MEDIATOR = Mediator()
6+
COMPONENT1 = Component(MEDIATOR, "Component1")
7+
COMPONENT2 = Component(MEDIATOR, "Component2")
8+
COMPONENT3 = Component(MEDIATOR, "Component3")
9+
MEDIATOR.add(COMPONENT1)
10+
MEDIATOR.add(COMPONENT2)
11+
MEDIATOR.add(COMPONENT3)
12+
13+
COMPONENT1.notify("data A")
14+
COMPONENT2.notify("data B")
15+
COMPONENT3.notify("data C")

mediator/component.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"Each component stays synchronized through a mediator"
2+
from interface_component import IComponent
3+
4+
5+
class Component(IComponent):
6+
"Each component stays synchronized through a mediator"
7+
8+
def __init__(self, mediator, name):
9+
self._mediator = mediator
10+
self._name = name
11+
12+
def notify(self, message):
13+
print(self._name + ": >>> Out >>> : " + message)
14+
self._mediator.notify(message, self)
15+
16+
def receive(self, message):
17+
print(self._name + ": <<< In <<< : " + message)

mediator/game_client.py

Lines changed: 0 additions & 30 deletions
This file was deleted.

mediator/game_engine.py

Lines changed: 0 additions & 48 deletions
This file was deleted.

mediator/interface_component.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"An interface that each component will implement"
2+
from abc import ABCMeta, abstractmethod
3+
4+
5+
class IComponent(metaclass=ABCMeta):
6+
"An interface that each component will implement"
7+
8+
@staticmethod
9+
@abstractmethod
10+
def notify(message):
11+
"The required notify method"
12+
13+
@staticmethod
14+
@abstractmethod
15+
def receive(message):
16+
"The required receive method"

mediator/interface_game_engine.py

Lines changed: 0 additions & 35 deletions
This file was deleted.

mediator/mediator.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"The Subject that all components will stay synchronized with"
2+
3+
4+
class Mediator():
5+
"A Subject whose notify method is mediated"
6+
7+
def __init__(self):
8+
self._components = set()
9+
10+
def add(self, component):
11+
"Add components"
12+
self._components.add(component)
13+
14+
def notify(self, message, originator):
15+
"Add components except for the originator component"
16+
for component in self._components:
17+
if component != originator:
18+
component.receive(message)

mediator/mediator_concept.py

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,48 @@
1+
# pylint: disable=too-few-public-methods
12
"Mediator Concept Sample Code"
2-
from abc import ABCMeta, abstractmethod
33

44

5-
class IMediator(metaclass=ABCMeta):
6-
"The Mediator interface indicating all the methods to implement"
7-
@staticmethod
8-
@abstractmethod
9-
def colleague1_method():
10-
"A method to implement"
11-
12-
@staticmethod
13-
@abstractmethod
14-
def colleague2_method():
15-
"A method to implement"
16-
17-
18-
class Mediator(IMediator):
5+
class Mediator():
196
"The Mediator Concrete Class"
207

218
def __init__(self):
229
self.colleague1 = Colleague1()
2310
self.colleague2 = Colleague2()
2411

2512
def colleague1_method(self):
26-
return self.colleague1.colleague1_method()
13+
"Calls the method provided by Colleague1"
14+
return self.colleague1.method_1()
2715

2816
def colleague2_method(self):
29-
return self.colleague2.colleague2_method()
17+
"Calls the method provided by Colleague2"
18+
return self.colleague2.method_2()
3019

3120

32-
class Colleague1(IMediator):
33-
"This Colleague calls the other Colleague via the Mediator"
21+
class Colleague1():
22+
"This Colleague provides data for Colleague2"
3423

35-
def colleague1_method(self):
24+
@staticmethod
25+
def method_1():
26+
"A simple method"
3627
return "Here is the Colleague1 specific data you asked for"
3728

38-
def colleague2_method(self):
39-
"not implemented"
40-
41-
42-
class Colleague2(IMediator):
43-
"This Colleague calls the other Colleague via the Mediator"
4429

45-
def colleague1_method(self):
46-
"not implemented"
30+
class Colleague2():
31+
"This Colleague provides data for Colleague1"
4732

48-
def colleague2_method(self):
33+
@staticmethod
34+
def method_2():
35+
"A simple method"
4936
return "Here is the Colleague2 specific data you asked for"
5037

5138

52-
# This Client is either Colleague1 or Colleague2
53-
# This Colleague will instantiate a Mediator, rather than calling
54-
# the other Colleague directly.
39+
# The Client
5540
MEDIATOR = Mediator()
5641

57-
# If I am Colleague1, I want some data from Colleague2
42+
# Colleague1 wants some data from Colleague2
5843
DATA = MEDIATOR.colleague2_method()
5944
print(f"COLLEAGUE1 <--> {DATA}")
6045

61-
# If I am Colleague2, I want some data from Colleague1
46+
# Colleague2 wants some data from Colleague1
6247
DATA = MEDIATOR.colleague1_method()
6348
print(f"COLLEAGUE2 <--> {DATA}")

mediator/player.py

Lines changed: 0 additions & 22 deletions
This file was deleted.

mediator/scheduler.py

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)