zoukankan      html  css  js  c++  java
  • 中介者模式

    一张图片得以理解为什么要使用中介者模式:

    各个对象之间相互引用,紧耦合体现的淋漓尽致,如果此时需要发生变化,那么改代码就糟糕了,因为无法修改。

    如果我们使用一个中介者来完成对象之间的交互,那么看起来明朗多了,每个对象只需要与中介者产生联系,将请求通过中介者发给接收对象,这有点像依赖倒转原则,这样不但看起来清晰,以后再修改逻辑的时候将会十分的方便与轻松。这就是中介者模式

     

    适用性:
        1.一组对象已经定义良好但是复杂的方式进行通信。产生的相互依赖关系结构混乱且难以理解。
        2.一个对象引用其他很多对象并且直接与这些对象通信,导致难以复用该对象。
        3.想定制一个分布在多个类中的行为,而又不想生成太多的子类。

    ***###***

    每个对象可以接收指定对象的消息,也可以给指定的对象发送消息

    中介者可以指定某个对象给另一个对象发送某些消息

     #region 抽象中介者
        abstract class AbstractChatroom
        {
            public abstract void Register(Participant participant);//可注册成员
            public abstract void Send(string from, string to, string message);//可传递消息
        }
        #endregion
       #region 具体中介者
        class Chatroom : AbstractChatroom
        {
            protected Hashtable hash = new Hashtable();
            public Hashtable Hash { get; }
            public override void Register(Participant participant)
            {
                if (hash[participant.name] == null)//如果没有注册该人
                {
                    hash[participant.name] = participant;
                }
                participant.chatroom = this;//注册该实例内部的中间者为此中介者,这个是重点
            }
    
            public override void Send(string from, string to, string message)
            {
                if(hash[to]!=null)
                {
                    ((Participant)hash[to]).Receive(from,message);
                }
            }
        }
        #endregion
      #region 对象成员基类
        class Participant
        {
            public Chatroom chatroom { get; set; }
            public string name { get; set; }
            public Participant(string name)
            {
                this.name = name;
            }
            public void Send(string to,string message)
            {
                chatroom.Send(this.name,to,message);//通过中介者将所有的逻辑全部写在Receive中
            }
            public virtual void Receive(string from,string message)
            {
                Console.WriteLine("{0} to {1}:'{2}'", from, name, message);
            }
        }
        #endregion
      #region 具体成员类
        class Beatle: Participant
        {
            public Beatle(string name) : base(name)
            {
            }
    
            public override void Receive(string from, string message)
            {
                Console.WriteLine("Beatle Receive");
                base.Receive(from,message);
            }
        }
        class NoBeatle : Participant
        {
            public NoBeatle(string name) : base(name)
            {
            }
            public override void Receive(string from, string message)
            {
                Console.WriteLine("NoBeatle Receive");
                base.Receive(from, message);
            }
        }
        #endregion
     static void Main(string[] args)
            {
                Chatroom chatroom = new Chatroom();
                //构建成员
                Participant participant1 = new Beatle("小王");
                Participant participant2 = new Beatle("小飞");
                Participant participant3 = new NoBeatle("小李");
                //注册到中介者
                chatroom.Register(participant1);
                chatroom.Register(participant2);
                chatroom.Register(participant3);
    
                participant1.Send("小飞", "发送消息喽");
                Console.ReadKey();
            }

    上述案例,体现了中介者中,虽然对象可以接收也可以发送,也可以通过中介者来实信息传递,如果每个地方都写一次逻辑的话,那么将使得代码十分混乱与复杂,巧妙的使用代码, 让所有逻辑写在发送或者接收里(不论使用接收还是发送,使用哪个,就需要在基类中定义该方法为虚方法,这样转过来的时候才会触发真正覆盖的实例方法)

    这就是中介者模式喽!

  • 相关阅读:
    【转】Tomcat中部署java web应用程序
    【转】如何安装mysql服务
    【转】java_web开发入门
    【转】SVN 查看历史信息
    【转】java编译错误 程序包javax.servlet不存在javax.servlet.*
    【转】MySQL5安装的图解(mysql-5.0.27-win32.zip)
    【转】JAVA变量path , classpth ,java_home设设置作用和作用
    intellij idea 10.5介绍
    Java中的IO与NIO
    javaWeb完成注册功能
  • 原文地址:https://www.cnblogs.com/ningxinjie/p/12189217.html
Copyright © 2011-2022 走看看