首页 > 其他分享 >设计模式之中介者模式

设计模式之中介者模式

时间:2022-10-06 14:46:10浏览次数:39  
标签:Console mediator ._ 模式 中介 component2 设计模式 public

中介者模式

概念

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();

 

标签:Console,mediator,._,模式,中介,component2,设计模式,public
From: https://www.cnblogs.com/chenyingzuo/p/16757578.html

相关文章

  • 设计模式-原型模式
    自己实现自己的对象拷贝逻辑,这个原型模式就是所谓这个prototype模式,prototype这个模式的话呢,其实简单来说是这样子的,它的场景没有什么太具体的场景,就是说,如果我们现在手头......
  • 设计模式之迭代器模式
    迭代器模式概念Iterator isabehavioraldesignpatternthatletsyoutraverseelementsofacollectionwithoutexposingitsunderlyingrepresentation(list,......
  • JAVA设计模式-外观模式
    JAVA设计模式-外观模式介绍外观模式是一种结构型模式,主要是为了隐藏系统的复杂性,对外提供一个可以访问的接口,客户端只需要访问这个接口即可。例如:我们现在的智能家居中......
  • 设计模式-构造器模式
    封装复杂对象的构造逻辑,那么这讲的话呢,实际上是这个builder模式,这个构造器模式就是builder,ok,那么这个builder模式所要实现的是一个什么场景呢,就是,是这样的,比如说我们现在要......
  • Red Hat Enterprise Linux release 8.0 (Ootpa)-进入单用户模式重新设置root密码
    RedHatEnterpriseLinuxrelease8.0使用单用户模式有一个前提,即系统引导器(GRUB)能正常工作;否则,就要使用修复模式进行系统维护。需要注意的是,进入单用户模式后,如果没有开......
  • 设计模式之责任链模式
    责任链模式概念ChainofResponsibility isabehavioraldesignpatternthatletsyoupassrequestsalongachainofhandlers.Uponreceivingarequest,each......
  • 设计模式之命令模式
    命令模式概念Command isabehavioraldesignpatternthatturnsarequestintoastand-aloneobjectthatcontainsallinformationabouttherequest.Thistra......
  • 5-互评-OO之接口-DAO模式代码阅读及应用
    DAO模式是接口的一个典型应用。阅读附件中的参考代码,并回答以下问题:StudenDaoListImpl.java与StudentDaoArrayImpl.java有何不同?StudenDaoListImpl.java采用List存......
  • 分组密码的基本的工作模式、优缺点
    1、电子密码本ECB(Electroniccodebook)需要加密的消息按照块密码的块大小被分为数个块,并对每个块进行独立加密。适用于数据较少的情况,比如加密秘钥。优点:简单、快速、......
  • 设计模式-外观模式
    将多个内部模块的调用封装在一个类中。这个外观模式的化,它是facade模式,这个是它的一个英文名啊,那我们这边的话呢,还是一样啊,上来给大家举一个场景吧,这个场景啊,假设我们现在......