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

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

3.Participant(The ‘AbstractColleague’ class)

聊天的参与者基类

    class Participant{private Chatroom _chatroom;private string _name;// Constructorpublic Participant(string name){this._name = name;}// Gets participant namepublic string Name{get { return _name; }}// Gets chatroompublic Chatroom Chatroom{set { _chatroom = value; }get { return _chatroom; }}// Sends message to given participantpublic void Send(string to, string message){_chatroom.Send(_name, to, message);}// Receives message from given participantpublic virtual void Receive(string from, string message){Debug.Log(from + " to " + Name + ": '" + message + "'");}}

4.Beatle( A ‘ConcreteColleague’ class)

聊天参与者子类

    class Beatle : Participant{// Constructorpublic Beatle(string name): base(name){}public override void Receive(string from, string message){Debug.Log("To a Beatle: ");base.Receive(from, message);}}

5.NonBeatle(A ‘ConcreteColleague’ class)

聊天参与者子类

    class NonBeatle : Participant{// Constructorpublic NonBeatle(string name): base(name){}public override void Receive(string from, string message){Debug.Log("To a non-Beatle: ");base.Receive(from, message);}}

6.测试

    public class MediatorExample1 : MonoBehaviour{void Start(){// Create chatroomChatroom chatroom = new Chatroom();// Create participants and register themParticipant 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 participantsYoko.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");}}

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/120574.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Linux系统如何将新硬盘挂载到Home目录下

Linux系统如果将硬盘挂载到Home目录下 目录 1、对新增磁盘进行分区 2、分区格式化 3、将新硬盘临时挂载在一个目录下

《Reinforcement Learning: An Introduction》第8章笔记

文章目录 Chapter 8 Planning and Learning with Tabular Methods8.1 Models and Planning8.2 Dyna: Integrated Planning, Acting, and Learning8.3 When the Models Is Wrong8.4 Prioritized Sweeping8.5 Expected vs. Sample Updates8.6 Trajectory Sampling8.7 Real-time D…

EasyWindow - Android 悬浮窗框架

官网 https://github.com/getActivity/EasyWindow 项目介绍 本框架意在解决一些极端需求&#xff0c;如果是普通的 Toast 封装推荐使用 Toaster 集成步骤 如果你的项目 Gradle 配置是在 7.0 以下&#xff0c;需要在 build.gradle 文件中加入 allprojects {repositories {/…

SR800-D 5G工业路由器:将无人驾驶汽车的通信能力提升到极限

​大家好&#xff01;欢迎来到今天星创易联的课堂&#xff0c;我是你们的通信老师&#xff0c;今天我们将讨论无人驾驶解决方案&#xff0c;其中包括SR800-D 5G工业路由器的运用。 首先&#xff0c;让我们聚焦于无人驾驶技术的重要性。无人驾驶汽车正在迅速崛起&#xff0c;这种…

多线程总结(线程池 线程安全 常见锁)

本篇文章主要是对线程池进行详解。同时引出了单例模式的线程池&#xff0c;也对线程安全问题进行了解释。其中包含了智能指针、STL容器、饿汉模式的线程安全。也对常见的锁&#xff1a;悲观锁&#xff08;Pessimistic Locking&#xff09;、乐观锁&#xff08;Optimistic Locki…

B058-SpringBoot

目录 springboot概念与作用入门案例springboot运行方式热部署配置文件Profile多环境支持整合测试-springboot-testSpringboot-web1.返回json数据2.返回页面&#xff08;模板技术&#xff09;thymeleaf1.导入thymeleaf依赖2.模板文件3.controller4.启动类 SSM整合1.导包2.项目目…

私有继承和虚函数私有化能用么?

源起 以前就知道private私有化声明关键字&#xff0c;和virtual虚函数关键字两者并不冲突&#xff0c;可以同时使用。 但是&#xff0c;它所表示的场景没有那么明晰&#xff0c;也觉得难以理解&#xff0c;直到近段时间遇到一个具体场景。 场景 借助ACE遇到的问题进行展示 …

蓝桥杯 题库 简单 每日十题 day12

01 列名 问题描述 在Excel中&#xff0c;列的名称使用英文字母的组合。前26列用一个字母&#xff0c;依 次为A到Z&#xff0c;接下来2626列使用两个字母的组合&#xff0c;依次为AA到zz. 请问第2022列的名称是什么&#xff1f; 答案提交 这是一道结果填空的题&#xff0c;你只…

【再识C进阶3(下)】详细地认识字符分类函数,字符转换函数和内存函数

前言 &#x1f493;作者简介&#xff1a; 加油&#xff0c;旭杏&#xff0c;目前大二&#xff0c;正在学习C&#xff0c;数据结构等&#x1f440; &#x1f493;作者主页&#xff1a;加油&#xff0c;旭杏的主页&#x1f440; ⏩本文收录在&#xff1a;再识C进阶的专栏&#x1…

设计模式3、工厂方法模式 Factory Method

解释说明&#xff1a;定义一个用于创建对象的接口&#xff0c;但是让子类决定将哪一个类实例化。工厂方法模式让一个类的实例化延迟到其子类 抽象工厂&#xff08;AbstractFactory&#xff09;&#xff1a;提供了创建产品的接口&#xff0c;调用者通过它访问具体工厂的工厂方法…

HTML5中使用video标签

参考链接 <videocontrolscontrolslist"nodownload noplaybackrate"disablePictureInPicture"true"disableRemotePlayback"true"src"https://www.runoob.com/try/demo_source/movie.mp4"></video>隐藏下载&#xff1a;nod…

ubuntu apt工具软件操作

apt工具 -----> 网关 国内网络(仓库源) 美国网络(仓库源)/etc/apt/sources.list https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu/sudo apt-get update sudo apt install sl 安装包 sudo apt-cache show sl 查看包信…