• Unity实现设计模式——中介者模式


    Unity实现设计模式——中介者模式

    用一个中介者对象来封装一系列的对象交互,中介者使各对象不需要显示地相互引用,从而使其松散耦合,而且可以独立地改变它们之间的交互。
    在这里插入图片描述
    这里使用一个生活中的例子来介绍中介者模式,比如当我们在一个聊天大厅进行聊天,每个人可以根据名字给对应的人发送消息,那么A人一定要持有其余人的名字信息吗,要是突然有新的人加入进来需要告知所有人多了个人吗,显然这样的设计方式非常繁琐,我们可以转化为以中介者为中心的星型结构,极大的解耦。
    在这里插入图片描述

    1.AbstractChatroom(The ‘Mediator’ abstract class)

    聊天大厅的抽象基类

        abstract class AbstractChatroom
        {
            public abstract void Register(Participant participant);
            public abstract void Send(string from, string to, string message);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2.Chatroom(The ‘ConcreteMediator’ class)

        class Chatroom : AbstractChatroom
        {
            private Dictionary<string, Participant> _participants =
              new Dictionary<string, Participant>();
    
            public override void Register(Participant participant)
            {
                if (!_participants.ContainsValue(participant))
                {
                    _participants[participant.Name] = participant;
                }
    
                participant.Chatroom = this;
            }
    
            public override void Send( string from, string to, string message)
            {
                Participant participant = _participants[to];
    
                if (participant != null)
                {
                    participant.Receive(from, message);
                }
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    3.Participant(The ‘AbstractColleague’ class)

    聊天的参与者基类

        class Participant
        {
            private Chatroom _chatroom;
            private string _name;
    
            // Constructor
            public Participant(string name)
            {
                this._name = name;
            }
    
            // Gets participant name
            public string Name
            {
                get { return _name; }
            }
    
            // Gets chatroom
            public Chatroom Chatroom
            {
                set { _chatroom = value; }
                get { return _chatroom; }
            }
    
            // Sends message to given participant
            public void Send(string to, string message)
            {
                _chatroom.Send(_name, to, message);
            }
    
            // Receives message from given participant
            public virtual void Receive(string from, string message)
            {
                Debug.Log(from + " to " + Name + ": '" + message + "'");
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    4.Beatle( A ‘ConcreteColleague’ class)

    聊天参与者子类

        class Beatle : Participant
        {
            // Constructor
            public Beatle(string name)
              : base(name)
            {
            }
    
            public override void Receive(string from, string message)
            {
                Debug.Log("To a Beatle: ");
                base.Receive(from, message);
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    5.NonBeatle(A ‘ConcreteColleague’ class)

    聊天参与者子类

        class NonBeatle : Participant
        {
            // Constructor
            public NonBeatle(string name)
              : base(name)
            {
            }
    
            public override void Receive(string from, string message)
            {
                Debug.Log("To a non-Beatle: ");
                base.Receive(from, message);
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    6.测试

        public class MediatorExample1 : MonoBehaviour
        {
            void Start()
            {
                // Create chatroom
                Chatroom chatroom = new Chatroom();
    
                // Create participants and register them
                Participant George = new Beatle("George");
                Participant Paul = new Beatle("Paul");
                Participant Ringo = new Beatle("Ringo");
                Participant John = new Beatle("John");
                Participant Yoko = new NonBeatle("Yoko");
    
                chatroom.Register(George);
                chatroom.Register(Paul);
                chatroom.Register(Ringo);
                chatroom.Register(John);
                chatroom.Register(Yoko);
    
                // Chatting participants
                Yoko.Send("John", "Hi John!");
                Paul.Send("Ringo", "All you need is love");
                Ringo.Send("George", "My sweet Lord");
                Paul.Send("John", "Can't buy me love");
                John.Send("Yoko", "My sweet love");
    
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    在这里插入图片描述

  • 相关阅读:
    Linux 创建虚拟机和安装CentOS过程中的参数解释
    密码学【第一节:密码学概述】
    羧酸/氨基/羟基功能化修饰荧光聚苯乙烯AIE微球/AIE三苯胺衍生物荧光微球的研究与制备
    Selenium java 控制当前已经打开的 chrome浏览器窗口
    Node.js全栈指南:认识MIME和HTTP
    我的创作纪念日
    分享金媒v10.3开源系统中CRM线下客户管理系统使用指南和小程序上架细分流程
    扩容后宝塔不更新硬盘容量
    3、树(上篇)
    什么是轻量应用服务器?腾讯云轻量服务器可以干什么?
  • 原文地址:https://blog.csdn.net/zzzsss123333/article/details/133409908