Mediator – It can let the program reduces chaotic dependencies between objects. This pattern can restrict the direct communications between the object and forces them to collaborates with a mediator object.
From Design Pattern – Behavioral Patterns
Structure
Applicability
Pros and Cons
PROS
CONS
How to Implement
#1 Basic Sample
static void Main(string[] args)
{
Component1 obj1 = new Component1();
Component2 obj2 = new Component2();
new ConcerteMediator(obj1, obj2);
Console.WriteLine("Client triggets operation A.");
obj1.DoA();
Console.WriteLine();
Console.WriteLine("Client triggers operation C.");
obj2.DoC();
Console.ReadKey();
}
interface IMediator
{
void Notify(object sender, string ev);
}
class ConcerteMediator : IMediator
{
private Component1 component1;
private Component2 component2;
public ConcerteMediator(Component1 obj1, Component2 obj2)
{
component1 = obj1;
component1.SetMediator(this);
component2 = obj2;
component2.SetMediator(this);
}
public void Notify(object sender, string ev)
{
switch (ev)
{
case "A":
Console.WriteLine("Mediator reacts on A and triggers folowing operations:");
component2.DoC();
break;
case "C":
Console.WriteLine("Mediator reacts on C and triggers following operations:");
break;
}
}
}
class BaseComponent
{
protected IMediator mediator;
public BaseComponent(IMediator _mediator = null) => mediator = _mediator;
public void SetMediator(IMediator _mediator) => mediator = _mediator;
}
class Component1 : BaseComponent
{
public void DoA()
{
Console.WriteLine("Component 1 does A.");
mediator.Notify(this, "A");
}
public new void SetMediator(IMediator _mediator) => mediator = _mediator;
}
class Component2 : BaseComponent
{
public void DoC()
{
Console.WriteLine("Component 2 does C.");
mediator.Notify(this, "C");
}
public new void SetMediator(IMediator _mediator) => mediator = _mediator;
} #2 Web Application Project
Step 1 Create the mediator pattern’s interface class files in the Core projects. One interface class file name sets “IMediator.cs.” Another interface class file name sets “IMediatorExe.cs.”
Step 2 Create the mediator pattern-related files in the Core projects, such as the “BaseComponent.cs”, the “Mediator.cs”, the “Component1.cs” and the “Component2.cs.”
The “BaseComponent.cs” file doesn’t need to inherit any class files. The “IMediator” variables direct statement, which is a “protected” variable, then adding the constructor mechanism and “SetMediator” function in its class.
The component class flies inherits the “BaseComponent.cs” file. Every class files add the constructor mechanism, then adds a few operation functions.
The “Mediator.cs” file inherits the “IMediator” class files, then adds the constructor mechanism.
Step 3 Create the “IMediatorExe” class’s service class flies in the Core projects. This services class file name sets “MediatorServices.cs.” Its file inherits the “ImediatorExe” interface class flies. Suppose the data object is not null before executing the mediator pattern operation function.
Step 4 Add the dispose mechanism by the IDispose class methods in every one class file in the Core project.
Step 5 Add the iterator pattern-related files in the web project, such as view flies, controller files. Register the iterator pattern files in the web project’s start.cs file.
Reference
What is the API gateway? An API gateway is an API management tool that sits…
The .NET 5 SDK is a kind of milestone in the .NET world. The .NET…
In object-oriented design, the dependency inversion principle is a specific methodology for loosely coupling software…
In the field of software engineering, the interface segregation principle (ISP) states that no code…
Subtype Requirement: Let be a property provable about objects of type T. Then should be true for objects of type S where S is…
In object-oriented programming, the open–closed principle states "software entities (classes, modules, functions, etc.) should be…
View Comments