设计模式之中介者模式

中介者模式

概念

Mediator is a behavioral design pattern that lets you reduce chaotic dependencies between objects. The pattern restricts direct communications between the objects and forces them to collaborate only via a mediator object.

中介者模式是一种行为设计模式 能让你减少对象之间混乱无序的依赖关系 该模式会限制对象之间的直接交互 迫使它们通过一个中介者对象进行合作

场景

当一个系统之中,多个类之间存在关联耦合的时候,需要单独抽出来的一个类,维护这些类之间的沟通。

不采用中介者模式的时候,所有类之间直接通信,这会导致类与类之家的关系非常杂乱,很难维护。

采用中介者模式之后,所有类都不直接调用对方,而是先把信息发送给中介者,中介者进行处理后再调用特定的类进行处理。

案例

这里我们有几个组件,分别在特定时机,需要调用其他组件的方法。

 可以看到,每个组件提供了设置中介者的方法,并通过中介者的Notify方法把信息告知中介者。

    class BaseComponent
    {
        protected IMediator _mediator;

        public BaseComponent(IMediator mediator = null)
        {
            this._mediator = mediator;
        }

        public void SetMediator(IMediator mediator)
        {
            this._mediator = mediator;
        }
    }

    class Component1 : BaseComponent
    {
        public void DoA()
        {
            Console.WriteLine("Component 1 does A.");

            this._mediator.Notify(this, "A");
        }

        public void DoB()
        {
            Console.WriteLine("Component 1 does B.");

            this._mediator.Notify(this, "B");
        }
    }

    class Component2 : BaseComponent
    {
        public void DoC()
        {
            Console.WriteLine("Component 2 does C.");

            this._mediator.Notify(this, "C");
        }

        public void DoD()
        {
            Console.WriteLine("Component 2 does D.");

            this._mediator.Notify(this, "D");
        }
    }

 中介者实现:

 中介者在构造的时候,给所有组件都设置了中介者对象,提供了Notify方法,方法内根据业务逻辑再调用不同的组件的方法进行处理

    public interface IMediator
    {
        void Notify(object sender, string ev);
    }

    class ConcreteMediator : IMediator
    {
        private Component1 _component1;

        private Component2 _component2;

        public ConcreteMediator(Component1 component1, Component2 component2)
        {
            this._component1 = component1;
            this._component1.SetMediator(this);
            this._component2 = component2;
            this._component2.SetMediator(this);
        }

        public void Notify(object sender, string ev)
        {
            if (ev == "A")
            {
                Console.WriteLine("Mediator reacts on A and triggers folowing operations:");
                this._component2.DoC();
            }
            if (ev == "D")
            {
                Console.WriteLine("Mediator reacts on D and triggers following operations:");
                this._component1.DoB();
                this._component2.DoC();
            }
        }
    }

 使用中介者模式

 

            Component1 component1 = new Component1();
            Component2 component2 = new Component2();
            new ConcreteMediator(component1, component2);

            Console.WriteLine("Client triggets operation A.");
            component1.DoA();

            Console.WriteLine();

            Console.WriteLine("Client triggers operation D.");
            component2.DoD();

 

posted @ 2022-10-06 14:36  内心澎湃的水晶侠  阅读(32)  评论(0编辑  收藏  举报