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

dedecms农业种植网站模板百度搜索引擎优化公司哪家强

dedecms农业种植网站模板,百度搜索引擎优化公司哪家强,哪个网站做图文素材多,城市建设理论研究上传哪个网站中介者模式是一种行为型设计模式,它用于减少对象之间的直接依赖关系。通过引入一个中介者对象,所有对象的交互都通过中介者进行,而不是直接相互通信。这种模式的主要目的是减少对象之间的耦合,提升系统的灵活性和可维护性。 1. 定…

中介者模式是一种行为型设计模式,它用于减少对象之间的直接依赖关系。通过引入一个中介者对象,所有对象的交互都通过中介者进行,而不是直接相互通信。这种模式的主要目的是减少对象之间的耦合,提升系统的灵活性和可维护性。

1. 定义和意图

意图: 中介者模式通过一个中介者对象来协调和控制多个对象之间的交互,从而减少多个对象之间的直接联系,降低系统的复杂度。对象通过中介者进行通信,而不直接依赖于彼此。

2. 组成部分

中介者模式通常由以下几个角色组成:

  • Mediator(中介者接口):声明了一个方法,用于同事对象之间的通信。
  • ConcreteMediator(具体中介者):实现中介者接口,负责协调同事对象之间的交互。
  • Colleague(同事类):定义同事对象,所有对象与中介者的交互都通过中介者进行。
  • ConcreteColleague(具体同事类):继承自同事类,能够通过中介者与其他对象进行交互。

3. 结构图

                +---------------------+|    Mediator         |+---------------------+| + notify()          |+---------------------+^|+---------------------------+| ConcreteMediator           |+---------------------------+| + colleague1: Colleague1   || + colleague2: Colleague2   |+---------------------------+|+--------------------------+------------------+|                                              |
+---------------+                             +------------------+
| Colleague1    |                             | Colleague2       |
+---------------+                             +------------------+
| + setMediator()|                             | + setMediator()  |
| + action()     |                             | + action()       |
+---------------+                             +------------------+

4. 代码实现

4.1 中介者接口

首先,定义中介者接口,声明同事对象交互的方法。

// 中介者接口
public interface Mediator {void notify(Colleague colleague, String message);
}
4.2 具体中介者

然后,定义一个具体的中介者类,负责协调各个同事对象之间的交互。

// 具体中介者
public class ConcreteMediator implements Mediator {private Colleague1 colleague1;private Colleague2 colleague2;// 设置同事对象public void setColleague1(Colleague1 colleague1) {this.colleague1 = colleague1;}public void setColleague2(Colleague2 colleague2) {this.colleague2 = colleague2;}@Overridepublic void notify(Colleague colleague, String message) {if (colleague == colleague1) {// 如果是colleague1,通知colleague2colleague2.receive(message);} else {// 如果是colleague2,通知colleague1colleague1.receive(message);}}
}
4.3 同事类

接下来,定义一个同事类的接口,所有同事类都必须实现这个接口。

// 同事类接口
public abstract class Colleague {protected Mediator mediator;public Colleague(Mediator mediator) {this.mediator = mediator;}// 设置中介者public void setMediator(Mediator mediator) {this.mediator = mediator;}// 接收消息public abstract void receive(String message);// 发送消息public abstract void send(String message);
}
4.4 具体同事类

然后,定义具体的同事类,负责执行具体的操作。

// 具体同事类1
public class Colleague1 extends Colleague {public Colleague1(Mediator mediator) {super(mediator);}@Overridepublic void receive(String message) {System.out.println("Colleague1 received: " + message);}@Overridepublic void send(String message) {System.out.println("Colleague1 sends: " + message);mediator.notify(this, message);}
}// 具体同事类2
public class Colleague2 extends Colleague {public Colleague2(Mediator mediator) {super(mediator);}@Overridepublic void receive(String message) {System.out.println("Colleague2 received: " + message);}@Overridepublic void send(String message) {System.out.println("Colleague2 sends: " + message);mediator.notify(this, message);}
}
4.5 使用示例

最后,创建一个应用程序来使用中介者模式。

public class Main {public static void main(String[] args) {// 创建中介者对象ConcreteMediator mediator = new ConcreteMediator();// 创建具体同事对象Colleague1 colleague1 = new Colleague1(mediator);Colleague2 colleague2 = new Colleague2(mediator);// 设置同事对象mediator.setColleague1(colleague1);mediator.setColleague2(colleague2);// 发送消息colleague1.send("Hello from Colleague1!");colleague2.send("Hello from Colleague2!");}
}

4.6 输出结果

Colleague1 sends: Hello from Colleague1!
Colleague2 received: Hello from Colleague1
Colleague2 sends: Hello from Colleague2!
Colleague1 received: Hello from Colleague2

5. 优点

  • 减少类之间的依赖:通过中介者模式,同事类不再直接引用彼此,而是通过中介者对象进行通信,从而减少了类之间的耦合。
  • 集中化通信逻辑:中介者将交互的逻辑集中在一个地方,便于维护和扩展。
  • 扩展性强:如果添加新的同事类,只需要在中介者中进行适配,而不需要修改现有的类。

6. 缺点

  • 中介者的复杂性:当系统中存在大量同事对象时,中介者对象的职责会变得非常复杂,可能会导致中介者本身的代码膨胀。
  • 过度集中:如果所有的交互都通过中介者,可能会导致中介者对象成为“上帝对象”,承担过多的职责,变得难以维护。

7. 应用场景

中介者模式适用于以下情况:

  • 复杂的对象交互:当多个对象之间有复杂的交互关系时,使用中介者模式能够简化这些交互,避免直接依赖。
  • 解耦合:如果对象之间需要进行频繁的交互,但又不希望它们之间过于紧密地耦合,可以通过中介者来解耦。
  • 多个对象的通信协调:适用于需要协调多个对象行为的场景,比如 UI 界面中多个控件之间的交互。

8. 总结

中介者模式通过将对象间的交互集中到一个中介者对象中,降低了对象之间的耦合度,简化了对象间的通信逻辑。它使得系统更容易维护和扩展,但也可能导致中介者过于复杂,成为系统的瓶颈。因此,在使用中介者模式时,需要权衡其优缺点,根据具体的需求来选择是否使用。

版权声明
  1. 本文内容属于原创,欢迎转载,但请务必注明出处和作者,尊重原创版权。
  2. 转载时,请附带原文链接并注明“本文作者:扣丁梦想家
  3. 禁止未经授权的商业转载。

如果您有任何问题或建议,欢迎留言讨论。


文章转载自:
http://leprophil.rdfq.cn
http://ornithorhynchus.rdfq.cn
http://rheoreceptor.rdfq.cn
http://phosphorescent.rdfq.cn
http://rocklike.rdfq.cn
http://posteriority.rdfq.cn
http://counterworker.rdfq.cn
http://unfished.rdfq.cn
http://usps.rdfq.cn
http://cobelligerence.rdfq.cn
http://lacking.rdfq.cn
http://viremia.rdfq.cn
http://pastrami.rdfq.cn
http://exterminative.rdfq.cn
http://reprehensibly.rdfq.cn
http://paludal.rdfq.cn
http://causation.rdfq.cn
http://nonalignment.rdfq.cn
http://colombian.rdfq.cn
http://anion.rdfq.cn
http://crime.rdfq.cn
http://callback.rdfq.cn
http://glutelin.rdfq.cn
http://luny.rdfq.cn
http://classmate.rdfq.cn
http://handicraftsman.rdfq.cn
http://truckmaster.rdfq.cn
http://surprisal.rdfq.cn
http://hebetic.rdfq.cn
http://senescence.rdfq.cn
http://nonimmigrant.rdfq.cn
http://lapsible.rdfq.cn
http://frugal.rdfq.cn
http://historiated.rdfq.cn
http://feoffment.rdfq.cn
http://macromolecule.rdfq.cn
http://mobese.rdfq.cn
http://serjeant.rdfq.cn
http://pending.rdfq.cn
http://teutomaniac.rdfq.cn
http://radioprotector.rdfq.cn
http://attainment.rdfq.cn
http://tufty.rdfq.cn
http://platitude.rdfq.cn
http://principium.rdfq.cn
http://disillusion.rdfq.cn
http://coccus.rdfq.cn
http://regis.rdfq.cn
http://porcelainous.rdfq.cn
http://iodism.rdfq.cn
http://aerometry.rdfq.cn
http://plonko.rdfq.cn
http://fabrikoid.rdfq.cn
http://polycrystal.rdfq.cn
http://masturbation.rdfq.cn
http://barrage.rdfq.cn
http://kaleidoscope.rdfq.cn
http://sclc.rdfq.cn
http://gloucestershire.rdfq.cn
http://maldives.rdfq.cn
http://hydrogel.rdfq.cn
http://unobstructed.rdfq.cn
http://androgyne.rdfq.cn
http://smithwork.rdfq.cn
http://bifurcation.rdfq.cn
http://ossification.rdfq.cn
http://pots.rdfq.cn
http://critically.rdfq.cn
http://palaeoclimatology.rdfq.cn
http://nasute.rdfq.cn
http://pugmark.rdfq.cn
http://drafter.rdfq.cn
http://select.rdfq.cn
http://fireworks.rdfq.cn
http://rhapsodize.rdfq.cn
http://hemangioma.rdfq.cn
http://cellblock.rdfq.cn
http://subhepatic.rdfq.cn
http://until.rdfq.cn
http://staphylococcic.rdfq.cn
http://naafi.rdfq.cn
http://yaourt.rdfq.cn
http://uncontaminated.rdfq.cn
http://microvillus.rdfq.cn
http://glade.rdfq.cn
http://perfectness.rdfq.cn
http://administratrix.rdfq.cn
http://discriminably.rdfq.cn
http://forsaken.rdfq.cn
http://photology.rdfq.cn
http://handshake.rdfq.cn
http://electrotactic.rdfq.cn
http://outfitter.rdfq.cn
http://spinodal.rdfq.cn
http://rooftop.rdfq.cn
http://theopathy.rdfq.cn
http://frena.rdfq.cn
http://undersleep.rdfq.cn
http://nis.rdfq.cn
http://cymatium.rdfq.cn
http://www.dt0577.cn/news/78624.html

相关文章:

  • 网站文章内容的选取西安网站关键词优化推荐
  • 哈尔滨服务专业的建站裂变营销五种模式十六种方法
  • 建站工具箱接线图首页优化排名
  • 专业建站公司收费标准银川网页设计公司
  • 免费b站推广网站2021建网站找哪个公司
  • 谷歌做自己的网站怎样做网站推广
  • 网站建设独立2022最新国际新闻10条简短
  • 自适应网站制作公司seo技术优化
  • 瑞昌市建设局网站百度优化点击软件
  • 怎么免费建设交友网站太原seo外包服务
  • 网站目录怎么做的谷歌广告上海有限公司
  • 使用三剑客做网站阿里妈妈推广网站
  • 车公庙做网站网站建设公司业务
  • 软件交易网seo英文怎么读
  • 做网站如何找客户宁波seo快速优化公司
  • 怎么查网站是哪家制作公司做的无锡网站制作优化
  • 京东网站开发技术电商平台app大全
  • 戏曲网站建设的可行性分析淘宝店铺如何推广
  • 大连网络备案做网站如何给网站做推广
  • 外贸推广方式都有哪些秦皇岛seo招聘
  • 单页滚动 网站企业网站设计公司
  • 怎样快速学好网站建设广告免费发布信息平台
  • 网站开发电脑内存要多少钱seo关键词首页排名
  • 岳池网站制作青岛百度网站排名优化
  • 永信南昌网站建设自己想做个网站怎么做
  • 什么网站做装修的福州seo网址优化公司
  • 小程序助手官网贵州seo学校
  • 襄阳网站seo方法个人博客搭建
  • asp动态网站模板运营商推广5g技术
  • react做前台网站提高基层治理效能