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

东营港西安网站seo推广

东营港,西安网站seo推广,上海微网站设计,楼盘怎么在网站上做推广中介者模式是一种行为设计模式,可以减少对象之间混乱无序的依赖关系。该模式会限制对象之间的直接交互,迫使它们通过一个封装了对象间交互行为的中介者对象来进行合作,从而使对象间耦合松散,并可独立地改变它们之间的交互。中介者…

中介者模式是一种行为设计模式,可以减少对象之间混乱无序的依赖关系。该模式会限制对象之间的直接交互,迫使它们通过一个封装了对象间交互行为的中介者对象来进行合作,从而使对象间耦合松散,并可独立地改变它们之间的交互。中介者模式又称为调停者模式。

Mediator is a behavior design pattern. It can reduce the disordered dependencies between objects. 
This pattern restricts direct interaction between objects, forcing them to collaborate through a mediator object 
that encapsulates the interaction behavior between objects, resulting in loose coupling between objects 
and the ability to independently change their interactions.  

结构设计

中介者模式包含如下角色:
Component,组件基类,声明组件的基本功能,有一个指向中介者的引用,该引用被声明为中介者接口类型。组件不知道中介者实际所属的类,因此可通过将其连接到不同的中介者以使其能在其他程序中复用。
Mediator,中介者接口,声明了与组件交流的方法,但通常仅包括一个通知方法。组件可将任意上下文作为该方法的参数,只有这样接收组件和发送者类之间才不会耦合。
ConcreteComponent,具体组件,实现组件声明的方法,并自定义业务逻辑接口。
ConcreteMediator,具体中介者,实现中介者接口声明的方法。
中介者模式类图表示如下:
请添加图片描述

伪代码实现

接下来将使用代码介绍下中介者模式的实现。

// 1、中介者接口,声明了与组件交流的方法  
public interface IMediator {void notify(Sender sender);
}//2、具体中介者,实现中介者接口声明的方法
public class ConcreteMediator implements IMediator {@Overridepublic void notify(Sender sender) {String message = sender.getMessage();Component target = sender.getTarget();target.operation(message);}
}// 3、组件基类,声明组件的基本功能,有一个指向中介者的引用,该引用被声明为中介者接口类型
public abstract class Component {protected IMediator mediator;public Component(IMediator mediator) {this.mediator = mediator;}public void operation(String message) {System.out.println("message is " + message);}public void send(String message, Component target) {Sender sender = new Sender(message, this, target);mediator.notify(sender);}
}// 4、具体组件,实现组件声明的方法,并自定义业务逻辑接口
public class ConcreteComponentA extends Component {public ConcreteComponentA(IMediator mediator) {super(mediator);}@Overridepublic void operation(String message) {super.operation(message);operationA();}public void operationA() {System.out.println("operationA in a Concrete ComponentA instance");}
}
public class ConcreteComponentB extends Component {public ConcreteComponentB(IMediator mediator) {super(mediator);}@Overridepublic void operation(String message) {super.operation(message);operationB();}public void operationB() {System.out.println("operationB in a Concrete ComponentB instance");}
}
public class ConcreteComponentC extends Component {public ConcreteComponentC(IMediator mediator) {super(mediator);}@Overridepublic void operation(String message) {super.operation(message);operationC();}public void operationC() {System.out.println("operationC in a Concrete ComponentC instance");}
}
public class ConcreteComponentD extends Component {public ConcreteComponentD(IMediator mediator) {super(mediator);}@Overridepublic void operation(String message) {super.operation(message);operationD();}public void operationD() {System.out.println("operationD in a Concrete ComponentD instance");}
}// 5、客户端
public class MediatorClient {public void test() {IMediator mediator = new ConcreteMediator();Component componentA = new ConcreteComponentA(mediator);Component componentB = new ConcreteComponentB(mediator);Component componentC = new ConcreteComponentC(mediator);Component componentD = new ConcreteComponentD(mediator);componentA.send("i am a", componentB);componentB.send("i am b", componentC);componentC.send("i am c", componentD);componentD.send("i am d", componentA);}
}public class Sender {private String message;private Component source;private Component target;public Sender(String message, Component source, Component target) {this.message = message;this.source = source;this.target = target;}public String getMessage() {return this.message;}public Component getSource() {return this.source;}public Component getTarget() {return this.target;}
}

适用场景

在以下情况下可以考虑使用中介者模式:
(1) 当一些对象和其他对象紧密耦合,产生的相互依赖关系结构混乱且难以理解,从而导致难以对其进行修改时,可考虑使用中介者模式。中介者模式可将对象间的所有关系抽取成为一个单独的类,
以使对于特定组件的修改工作独立于其他组件。
(2) 当组件因过于依赖其他组件而无法在不同应用中复用时,可考虑使用中介者模式。应用中介者模式后, 每个组件不再知晓其他组件的情况。尽管这些组件无法直接交流,但它们仍可通过中介者对象进行间接交流。
如果希望在不同应用中复用一个组件,则需要为其提供一个新的中介者类。
(3) 如果为了能在不同情景下复用一些基本行为,导致需要被迫创建大量组件子类时,可考虑使用中介者模式。由于所有组件间关系都被包含在中介者中, 因此无需修改组件就能方便地新建中介者类以定义新的组件合作方式。

优缺点

中介者模式有以下优点:
(1) 符合单一职责原则。可以将多个组件间的交流抽取到同一位置,使其更易于理解和维护。
(2) 符合开闭原则。无需修改实际组件就能增加新的中介者。
(3) 可以减轻应用中多个组件间的耦合情况。
但是该模式也存在以下缺点:
(1) 在具体中介者类中包含了组件之间的交互细节,可能会导致具体中介者类非常复杂,使得系统难以维护。一段时间后,中介者可能会演化成为上帝对象。

参考

《设计模式 可复用面向对象软件的基础》 Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides 著, 李英军, 马晓星等译
https://design-patterns.readthedocs.io/zh_CN/latest/behavioral_patterns/mediator.html 中介者模式
https://refactoringguru.cn/design-patterns/mediator 中介者模式
https://www.runoob.com/design-pattern/mediator-pattern.html 中介者模式
https://www.cnblogs.com/adamjwh/p/10959987.html 简说设计模式——中介者模式

http://www.zhongyajixie.com/news/38735.html

相关文章:

  • 深圳网站建设 site百度登录页面
  • wordpress关闭邮箱验证码白山seo
  • wordpress分类文章上海百度seo优化
  • 免备案主机做seo如何赚钱
  • 群晖做网站服务器速度快吗厦门百度广告
  • 上海备案证查询网站查询系统谷歌浏览器官方app下载
  • 网站快照怎么更新百度搜索排名怎么做
  • 房地产网站开发商百度推广账号出售
  • 哪些行业做网站多自建站平台
  • 美团网站网站建设发展品牌营销公司
  • 大学网站策划方案品牌整合营销推广
  • 网站历史记录怎么恢复优化的定义
  • 头条有没有做网站的如何自己创建一个网站
  • 宿迁做网站公司哪家好搜索引擎营销怎么做
  • 平面设计风格有哪些类型苏州seo网站优化软件
  • 制作做动画的网站成人培训机构
  • 什么企业做网站如何建立网站服务器
  • 聂教练做0网站广州aso优化
  • c 网站开发中间层怎么写百度搜索简洁版网址
  • 网站ico图标产品策划方案怎么做
  • 虚拟机可以做多个网站怎么优化整站
  • 广州做网站哪家强百度搜索排名规则
  • 高端建设网站企业公司主页网站设计
  • 关于电商网站建设的论文软文小故事200字
  • 网站开发项目需求分析说明书网络推广公司网站
  • 节日网站设计怎么免费推广自己网站
  • 网站从哪些方面来做广告推广方案怎么写
  • 网站怎么做友情链接南宁百度首页优化
  • 做网站用什么语言好app推广注册接单平台
  • 网站开发项目教程站长工具seo综合查询 分析