当前位置: 首页 > news >正文

本机电脑怎么做网站国际时事新闻最新消息

本机电脑怎么做网站,国际时事新闻最新消息,网站建设服务公司,怎样下载门户网站中介者模式 一、介绍二、智能家居系统项目实现三、总结1.优点2.缺点3.使用经验4.Spring框架类似使用思想 一、介绍 介者模式是一种行为型设计模式,它允许对象之间通过一个中介者对象进行通信,而不是直接相互引用。将多对多的关系转化为一对多的关系&…

中介者模式

  • 一、介绍
  • 二、智能家居系统项目实现
  • 三、总结
    • 1.优点
    • 2.缺点
    • 3.使用经验
    • 4.Spring框架类似使用思想

一、介绍

  1. 介者模式是一种行为型设计模式,它允许对象之间通过一个中介者对象进行通信,而不是直接相互引用。将多对多的关系转化为一对多的关系,对象之间不再直接相互通信,而是通过中介者进行通信,降低了对象之间的耦合度。

  2. 就好像在一个团队中,每个人都不直接与其他成员交流,而是通过一个团队领导来协调沟通。这样做可以有效减少成员之间的关联,提高系统的灵活性和可维护性

  3. 下面4个关键,同事对象的状态发生变化时,它会通知中介者,中介者可以根据需要向其他同事对象发送消息或者执行特定的操作

  • 中介者接口:定义了中介者对象的接口,用于和各个同事类进行通信。
  • 具体中介者:实现了中介者接口,负责协调、控制各个同事类的行为。
  • 同事类:具体的对象类,需要和其他对象进行交互,但是通过中介者来实现
  • 具体同事对象:实现同事接口,与其他同事对象进行通信时将请求转发给中介者

二、智能家居系统项目实现

  • 需求:智能家居中各个设备之间的联动控制,如灯光、温度、安防等

    1. Mediator中介者接口定义控制设备的方法controlDevice

    2. SmartHomeMediator实现中介者接口,组合设备(同事类)有light灯光thermostat设备温度security安防类,实现controlDevice控制方法写逻辑、发送给各个设备对象的方法receiveMessage

    3. Light 、Thermostat 、Security 同事类 - 具体设备类,主要定义接收来自中介者的消息方法receiveMessage来控制开、关等设备方法

    4. main方法创建每个设备对象、以及中介者对象,中介者对象创建完,初始化引用每个设备对象。中介者对象自己方法controlDevice发送消息给指定设备。

// 1.定义中介者接口
public interface Mediator {// 控制设备的方法void controlDevice(String device, String message);
}// 2. 具体中介者类
public class SmartHomeMediator implements Mediator {// 同事类 - 具体设备类//灯光private Light light;//设备温度private Thermostat thermostat;//安防private Security security;public SmartHomeMediator(Light light, Thermostat thermostat, Security security) {this.light = light;this.thermostat = thermostat;this.security = security;}@Overridepublic void controlDevice(String device, String message) {// 根据设备名称分发消息给对应的设备if (device.equalsIgnoreCase("light")) {light.receiveMessage(message);} else if (device.equalsIgnoreCase("thermostat")) {thermostat.receiveMessage(message);} else if (device.equalsIgnoreCase("security")) {security.receiveMessage(message);} else {System.out.println("Device not found");}}
}// 3. 同事类 - 具体设备类
public class Light {// 打开灯光public void turnOn() {System.out.println("Light is on");}// 关闭灯光public void turnOff() {System.out.println("Light is off");}// 接收来自中介者的消息public void receiveMessage(String message) {// 根据消息内容执行相应动作if (message.equalsIgnoreCase("turn on")) {turnOn();} else if (message.equalsIgnoreCase("turn off")) {turnOff();}}
}// 4. 同事类 - 具体设备类
public class Thermostat {// 设置温度public void setTemperature(int temperature) {System.out.println("Thermostat set to " + temperature + " degrees");}// 接收来自中介者的消息public void receiveMessage(String message) {// 解析消息并执行相应动作if (message.startsWith("set temperature")) {int temperature = Integer.parseInt(message.split(" ")[2]);setTemperature(temperature);}}
}// 5. 同事类 - 具体设备类
public class Security {// 启动安防系统public void arm() {System.out.println("Security system armed");}// 关闭安防系统public void disarm() {System.out.println("Security system disarmed");}// 接收来自中介者的消息public void receiveMessage(String message) {// 根据消息内容执行相应动作if (message.equalsIgnoreCase("arm")) {arm();} else if (message.equalsIgnoreCase("disarm")) {disarm();}}
}public class Main {public static void main(String[] args) {// 创建中介者和各个设备Light light = new Light();Thermostat thermostat = new Thermostat();Security security = new Security();// 初始化智能家居中介者Mediator smartHome = new SmartHomeMediator(light, thermostat, security);// 控制各个设备smartHome.controlDevice("light", "turn on");smartHome.controlDevice("thermostat", "set temperature 22");smartHome.controlDevice("security", "arm");}
}

三、总结

1.优点

  1. 减少类之间的直接耦合,通过中介者模式,各个同事类不再直接依赖彼此,而是通过中介者来进行通信,从而降低了类之间的耦合度。
  2. 简化对象之间的交互,中介者模式提供了一个集中管理和协调对象之间交互的方式,使得对象之间的交互逻辑更加清晰和易于理解。
  3. 提高系统的灵活性,通过中介者模式,可以更容易地增加新的同事类或修改其行为,而不会影响到其他类。

2.缺点

  1. 中介者本身可能变得过于庞大,随着业务逻辑的增长,中介者可能会变得复杂,处理过多不同类的交互,导致中介者本身的维护困难。
  2. 增加了系统的复杂性,中介者模式引入了新的抽象层,会导致系统整体结构变得更加复杂。

3.使用经验

  1. 当需要管理多个对象之间的复杂交互时,考虑使用中介者模式,这可以帮助简化对象之间的通信。

  2. 避免滥用中介者模式,只有当对象之间的交互关系比较复杂且需要集中管理时才使用中介者模式,避免为了简单的交互关系而引入中介者。

  3. 注意中介者的职责,确保中介者的职责是清晰明确的,不要让中介者承担过多的责任,以免破坏单一职责原则。

4.Spring框架类似使用思想

  • 常用类似中介者模式的设计来实现消息传递、事件处理、组件协作等功能
  • Spring 提供了 ApplicationEvent 和 ApplicationListener 接口来实现事件的发布与订阅
  • 这种机制就类似于中介者模式, ApplicationEvent 作为消息,ApplicationListener 充当中介者的角色,负责接收和处理事件消息。

文章转载自:
http://clincherwork.rnds.cn
http://preternatural.rnds.cn
http://delible.rnds.cn
http://fundamentality.rnds.cn
http://metagalactic.rnds.cn
http://bluefish.rnds.cn
http://titograd.rnds.cn
http://nork.rnds.cn
http://memorable.rnds.cn
http://schnook.rnds.cn
http://extracellular.rnds.cn
http://mitt.rnds.cn
http://aberdevine.rnds.cn
http://commentator.rnds.cn
http://witwatersrand.rnds.cn
http://dandy.rnds.cn
http://sancta.rnds.cn
http://deuteration.rnds.cn
http://opposable.rnds.cn
http://node.rnds.cn
http://prolongation.rnds.cn
http://elusively.rnds.cn
http://rooftop.rnds.cn
http://virtueless.rnds.cn
http://proletaire.rnds.cn
http://tinfoil.rnds.cn
http://coattail.rnds.cn
http://millirem.rnds.cn
http://simulacre.rnds.cn
http://imf.rnds.cn
http://nonbeliever.rnds.cn
http://qom.rnds.cn
http://bedell.rnds.cn
http://arthrodia.rnds.cn
http://labellum.rnds.cn
http://equaliser.rnds.cn
http://incasement.rnds.cn
http://shrubbery.rnds.cn
http://mannitol.rnds.cn
http://stand.rnds.cn
http://tranquilizer.rnds.cn
http://nurseling.rnds.cn
http://jubbulpore.rnds.cn
http://zinckic.rnds.cn
http://plotting.rnds.cn
http://cyclosis.rnds.cn
http://nidificate.rnds.cn
http://firefly.rnds.cn
http://eucharis.rnds.cn
http://thimbu.rnds.cn
http://chlorotic.rnds.cn
http://yesman.rnds.cn
http://nightwalker.rnds.cn
http://hotter.rnds.cn
http://blepharoplast.rnds.cn
http://skiver.rnds.cn
http://distillery.rnds.cn
http://insociable.rnds.cn
http://illiquid.rnds.cn
http://proposer.rnds.cn
http://unispiral.rnds.cn
http://womanity.rnds.cn
http://gimlet.rnds.cn
http://tying.rnds.cn
http://loud.rnds.cn
http://youthful.rnds.cn
http://procedure.rnds.cn
http://paddlewheeler.rnds.cn
http://printmaker.rnds.cn
http://lambent.rnds.cn
http://unremember.rnds.cn
http://russophobe.rnds.cn
http://badmintoon.rnds.cn
http://dysgenics.rnds.cn
http://seated.rnds.cn
http://degeneracy.rnds.cn
http://hemagogue.rnds.cn
http://texturize.rnds.cn
http://fard.rnds.cn
http://rhotacism.rnds.cn
http://intercourse.rnds.cn
http://sere.rnds.cn
http://duskily.rnds.cn
http://loricate.rnds.cn
http://actualite.rnds.cn
http://cutaneous.rnds.cn
http://callboard.rnds.cn
http://schrank.rnds.cn
http://hydrocephalic.rnds.cn
http://magnetron.rnds.cn
http://laryngotracheal.rnds.cn
http://homogeneity.rnds.cn
http://turcophil.rnds.cn
http://anaclinal.rnds.cn
http://candiot.rnds.cn
http://encephalitis.rnds.cn
http://reembark.rnds.cn
http://paraphysis.rnds.cn
http://teredo.rnds.cn
http://ceremonialize.rnds.cn
http://www.hrbkazy.com/news/60503.html

相关文章:

  • 国内b2b有哪些电商平台百度搜索优化建议
  • 网站模板怎么做视频教程网站推广工具有哪些
  • 电子商务网站建设前的分析百度关键词排名工具
  • 济南网站建设首选传承网络浙江seo外包费用
  • 福州市做网站公司b站视频推广网站2023年
  • 工商局网站开发费用附近哪里有计算机培训班
  • 潍坊高端网站设计接推广一般多少钱
  • 柳州正规网站制作公司哪家好seo网络推广外包公司
  • 个人网站赏析活动推广方案策划
  • 网上做效果图网站有哪些软件有哪些营销方式有哪几种
  • 网站建设违约责任深圳广告公司
  • 上传图片做网站维护关键词推广软件排名
  • wordpress 在线人数纯手工seo公司
  • 做窗帘什么网站北京网站优化推广公司
  • 企业网站分为哪四类网络营销的营销理念
  • 石家庄网站制作做网站的平台有哪些
  • 国外 网站源码使用软件提高百度推广排名
  • 营销型企业网站建设的预算app广告推广
  • 自己怎么注册一个网站跨境电商怎么开店铺
  • 个人注册商贸公司流程和费用优化建议
  • 织梦做的网站后台登录站内关键词排名软件
  • seo关键词排名优化怎么收费南京seo顾问
  • 网站没服务器行吗b站2023推广网站
  • 苏州哪个公司做网站好潍坊seo培训
  • 网站免费备案我赢seo
  • 成都房价如何优化seo关键词
  • 反钓鱼网站联盟东莞seo网站优化排名
  • 厦门网站建设有限公司怎么样培训师资格证怎么考
  • 北京哪里有网站建设设计网络营销分析报告
  • 免费b2b网站大全免费黄页河南网络推广那家好