Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Mediator pattern
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== C# === The mediator pattern ensures that components are [[loosely coupled]], such that they do not call each other explicitly, but instead do so through calls to a mediator. In the following example, the mediator registers all Components and then calls their {{Code|SetState}} methods. <syntaxhighlight lang="csharp"> interface IComponent { void SetState(object state); } class Component1 : IComponent { internal void SetState(object state) { throw new NotImplementedException(); } } class Component2 : IComponent { internal void SetState(object state) { throw new NotImplementedException(); } } // Mediates the common tasks class Mediator { internal IComponent Component1 { get; set; } internal IComponent Component2 { get; set; } internal void ChangeState(object state) { this.Component1.SetState(state); this.Component2.SetState(state); } } </syntaxhighlight> A chat room could use the mediator pattern, or a system where many ‘clients’ each receive a message each time one of the other clients performs an action (for chat rooms, this would be when each person sends a message). In reality using the mediator pattern for a chat room would only be practical when used with [[remoting]]. Using raw sockets would not allow for the [[Delegation (object-oriented programming)|delegate]] [[callbacks]] (people subscribed to the Mediator class’ MessageReceived event). <syntaxhighlight lang="csharp"> public delegate void MessageReceivedEventHandler(string message, string sender); public class Mediator { public event MessageReceivedEventHandler MessageReceived; public void Send(string message, string sender) { if (MessageReceived != null) { Console.WriteLine("Sending '{0}' from {1}", message, sender); MessageReceived(message, sender); } } } public class Person { private Mediator _mediator; public Person(Mediator mediator, string name) { Name = name; _mediator = mediator; _mediator.MessageReceived += new MessageReceivedEventHandler(Receive); } public string Name { get; set; } private void Receive(string message, string sender) { if (sender != Name) Console.WriteLine("{0} received '{1}' from {2}", Name, message, sender); } public void Send(string message) { _mediator.Send(message, Name); } } </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)