Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Mediator pattern
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
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>
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Mediator pattern
(section)
Add topic