Mediator API Documentation

class Mediator(*args, **kwargs)[source]

Bases: Protocol

Defines the interface for the Mediator.

A mediator facilitates communication between different Colleagues.

send_message(sender, message)[source]

Sends a message from one colleague to others.

Parameters:
  • sender (Colleague) – The colleague sending the message.

  • message (str) – The message to send.

Return type:

None

add_colleague(colleague)[source]

Registers a colleague with the mediator.

Parameters:

colleague (Colleague) – The colleague to register.

Return type:

None

class Colleague(mediator)[source]

Bases: ABC

Abstract base class for Colleagues in the Mediator pattern.

Colleagues communicate with each other through a Mediator.

Parameters:

mediator (Mediator)

__init__(mediator)[source]

Initializes the colleague with a given mediator.

Parameters:

mediator (Mediator) – The Mediator that the colleague will use to communicate.

Return type:

None

send(message)[source]

Sends a message via the Mediator.

Parameters:

message (str) – The message to send.

Return type:

None

abstractmethod receive(message)[source]

Handles a received message.

Parameters:

message (str) – The message received.

Return type:

None

class ConcreteMediator[source]

Bases: Mediator

Concrete implementation of the Mediator interface.

Facilitates communication between registered colleagues.

__init__()[source]

Initializes the mediator with an empty list of colleagues.

When created, the mediator has no colleagues registered.

Return type:

None

send_message(sender, message)[source]

Sends a message to all colleagues except the sender.

Parameters:
  • sender (Colleague) – The colleague sending the message.

  • message (str) – The message to send.

Return type:

None

add_colleague(colleague)[source]

Adds a colleague to the mediator’s list of colleagues.

Parameters:

colleague (Colleague) – The colleague to be added to the mediator.

Return type:

None

class ConcreteColleague(mediator, name)[source]

Bases: Colleague

Concrete implementation of the Colleague class.

Represents a participant in communication facilitated by the Mediator.

Parameters:
__init__(mediator, name)[source]

Initializes the concrete colleague with a given mediator and name.

Parameters:
  • mediator (Mediator) – The Mediator that the colleague will use to communicate.

  • name (str) – The name of the colleague.

Return type:

None

receive(message)[source]

Handles a received message by logging it.

Parameters:

message (str) – The message received.

Return type:

None

property name: str

Returns the name of the colleague.