tgoop.com/openit2/3310
Last Update:
π€ΠΠ°ΡΠ΅ΡΠ½ ΠΏΡΠΎΠ³ΡΠ°ΠΌΡΠ²Π°Π½Π½Ρ Mediator ποΈ
ΠΠΎΠΏΠΎΠΌΠ°Π³Π°Ρ Π·ΠΌΠ΅Π½ΡΠΈΡΠΈ ΠΊΡΠ»ΡΠΊΡΡΡΡ Π·Π°Π»Π΅ΠΆΠ½ΠΎΡΡΠ΅ΠΉ ΠΌΡΠΆ ΠΎΠ±'ΡΠΊΡΠ°ΠΌΠΈ ΠΉ ΡΠΎΠ±ΠΈΡΡ ΡΡ
Π½Ρ Π²Π·Π°ΡΠΌΠΎΠ΄ΡΡ ΡΠ΅Π½ΡΡΠ°Π»ΡΠ·ΠΎΠ²Π°Π½ΠΎΡ ΡΠ΅ΡΠ΅Π· ΡΠΏΠ΅ΡΡΠ°Π»ΡΠ½ΠΈΠΉ ΠΎΠ±'ΡΠΊΡ β ΠΌΠ΅Π΄ΡΠ°ΡΠΎΡ. π
Mediator Π΄ΠΎΠ·Π²ΠΎΠ»ΡΡ Π³ΡΡΠΏΡ ΠΎΠ±'ΡΠΊΡΡΠ² ΡΠΏΡΠ»ΠΊΡΠ²Π°ΡΠΈΡΡ ΡΠ΅ΡΠ΅Π· ΡΠ΅Π½ΡΡΠ°Π»ΡΠ½ΠΈΠΉ ΠΎΠ±'ΡΠΊΡ. ΠΠ°ΠΌΡΡΡΡ ΡΠΎΠ³ΠΎ, ΡΠΎΠ± ΠΎΠ±'ΡΠΊΡΠΈ Π½Π°ΠΏΡΡΠΌΡ Π²ΠΈΠΊΠ»ΠΈΠΊΠ°Π»ΠΈ ΠΌΠ΅ΡΠΎΠ΄ΠΈ ΠΎΠ΄ΠΈΠ½ ΠΎΠ΄Π½ΠΎΠ³ΠΎ, Π²ΠΎΠ½ΠΈ Π²ΠΈΠΊΠ»ΠΈΠΊΠ°ΡΡΡ ΠΌΠ΅ΡΠΎΠ΄ΠΈ ΠΌΠ΅Π΄ΡΠ°ΡΠΎΡΠ°, ΡΠΊΠΈΠΉ ΠΏΠΎΡΡΠΌ ΠΊΠ΅ΡΡΡ ΡΡ
Π½Ρ Π²Π·Π°ΡΠΌΠΎΠ΄ΡΡΡ. π‘
ΠΡΠΈΠΊΠ»Π°Π΄ ΠΊΠΎΠ΄Ρ Π½Π° Pythonclass Mediator:
def notify(self, sender, event):
pass
class ConcreteMediator(Mediator):
def init(self, component1, component2):
self._component1 = component1
self._component1.mediator = self
self._component2 = component2
self._component2.mediator = self
def notify(self, sender, event):
if event == "A":
print("Mediator reacts on A and triggers following operations:")
self._component2.do_c()
elif event == "D":
print("Mediator reacts on D and triggers following operations:")
self._component1.do_b()
self._component2.do_c()
class BaseComponent:
def init(self, mediator=None):
self._mediator = mediator
@property
def mediator(self):
return self._mediator
@mediator.setter
def mediator(self, mediator):
self._mediator = mediator
class Component1(BaseComponent):
def do_a(self):
print("Component 1 does A.")
self.mediator.notify(self, "A")
def do_b(self):
print("Component 1 does B.")
class Component2(BaseComponent):
def do_c(self):
print("Component 2 does C.")
def do_d(self):
print("Component 2 does D.")
self.mediator.notify(self, "D")
if name == "main":
component1 = Component1()
component2 = Component2()
mediator = ConcreteMediator(component1, component2)
print("Client triggers operation A.")
component1.do_a()
print("\nClient triggers operation D.")
component2.do_d()
πΠΠΎΡΡΠ½Π΅Π½Π½Ρπ
1. Mediator β Π±Π°Π·ΠΎΠ²ΠΈΠΉ ΠΊΠ»Π°Ρ, ΡΠΊΠΈΠΉ Π²ΠΈΠ·Π½Π°ΡΠ°Ρ ΠΌΠ΅ΡΠΎΠ΄ Β«notifyΒ».
2. ConcreteMediator β ΠΌΠ΅Π΄ΡΠ°ΡΠΎΡ, ΡΠΊΠΈΠΉ Π·Π½Π°Ρ ΠΏΡΠΎ Π²ΡΡ ΠΊΠΎΠΌΠΏΠΎΠ½Π΅Π½ΡΠΈ ΠΉ ΠΊΠ΅ΡΡΡ ΡΡ
Π½Π΅Ρ Π²Π·Π°ΡΠΌΠΎΠ΄ΡΡΡ.
3. BaseComponent β Π±Π°Π·ΠΎΠ²ΠΈΠΉ ΠΊΠ»Π°Ρ Π΄Π»Ρ ΠΊΠΎΠΌΠΏΠΎΠ½Π΅Π½ΡΡΠ², ΡΠΊΡ ΠΌΠ°ΡΡΡ ΠΏΠΎΡΠΈΠ»Π°Π½Π½Ρ Π½Π° ΠΌΠ΅Π΄ΡΠ°ΡΠΎΡΠ°.
4. Component1 Ρ Component2 β ΠΊΠΎΠ½ΠΊΡΠ΅ΡΠ½Ρ ΠΊΠΎΠΌΠΏΠΎΠ½Π΅Π½ΡΠΈ, ΡΠΊΡ Π²ΠΈΠΊΠΎΠ½ΡΡΡΡ ΡΠ²ΠΎΡ Π΄ΡΡ ΡΠ° ΠΏΠΎΠ²ΡΠ΄ΠΎΠΌΠ»ΡΡΡΡ ΠΌΠ΅Π΄ΡΠ°ΡΠΎΡΠ° ΠΏΡΠΎ ΠΏΠΎΠ΄ΡΡ.
Π£ ΡΡΠΎΠΌΡ ΠΏΡΠΈΠΊΠ»Π°Π΄Ρ ΠΊΠΎΠΌΠΏΠΎΠ½Π΅Π½Ρ 1 Π²ΠΈΠΊΠ»ΠΈΠΊΠ°Ρ ΠΏΠΎΠ΄ΡΡ Β«AΒ», ΡΠΊΡ ΠΌΠ΅Π΄ΡΠ°ΡΠΎΡ ΠΏΠ΅ΡΠ΅Ρ
ΠΎΠΏΠ»ΡΡ Ρ Π²ΠΈΠΊΠ»ΠΈΠΊΠ°Ρ ΠΌΠ΅ΡΠΎΠ΄ Β«do_cΒ» ΠΊΠΎΠΌΠΏΠΎΠ½Π΅Π½ΡΠ° 2. ΠΠ½Π°Π»ΠΎΠ³ΡΡΠ½ΠΎ, ΠΏΠΎΠ΄ΡΡ Β«DΒ» ΠΊΠΎΠΌΠΏΠΎΠ½Π΅Π½ΡΠ° 2 Π²ΠΈΠΊΠ»ΠΈΠΊΠ°Ρ ΠΌΠ΅ΡΠΎΠ΄ΠΈ Β«do_bΒ» ΠΊΠΎΠΌΠΏΠΎΠ½Π΅Π½ΡΠ° 1 Ρ Β«do_cΒ» ΠΊΠΎΠΌΠΏΠΎΠ½Π΅Π½ΡΠ° 2. π
#CodeBreakdown_for_students
π IT Student β Π±ΡΠ΄Ρ Π² ΠΊΡΡΡΡ IT-ΠΌΠΎΠΆΠ»ΠΈΠ²ΠΎΡΡΠ΅ΠΉ Π΄Π»Ρ ΡΠ΅Π±Π΅!
BY IT Student by Kharkiv IT Cluster
Share with your friend now:
tgoop.com/openit2/3310