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

做网站可以不用框架吗室内设计资料网站

做网站可以不用框架吗,室内设计资料网站,优化搜狐的培训,韩国化妆品网站模板1. 引言#xff1a;中介者模式的重要性 在软件设计的世界里#xff0c;模块间的相互依赖往往会导致系统的复杂性和维护难度的增加。中介者模式#xff08;Mediator Pattern#xff09;作为一种行为设计模式#xff0c;它的出现就是为了解决这一问题。通过引入一个中介者对…1. 引言中介者模式的重要性 在软件设计的世界里模块间的相互依赖往往会导致系统的复杂性和维护难度的增加。中介者模式Mediator Pattern作为一种行为设计模式它的出现就是为了解决这一问题。通过引入一个中介者对象它能够协调各个模块之间的通信从而实现模块间的解耦提高系统的灵活性和可维护性。 1.1 中介者模式在软件设计中的作用 中介者模式的核心思想是定义一个中介对象来封装一系列对象之间的交互。中介者使各个对象不需要显式地相互引用从而使其耦合松散而且可以独立地改变它们之间的交互。这种模式在需要处理多个对象之间的复杂网络关系时尤其有用它能够简化对象之间的通信并使得系统更易于管理和扩展。 1.2 中介者模式与系统解耦 系统解耦是软件设计中的一个重要目标它意味着各个模块或组件之间应该尽可能地独立减少直接的依赖关系。中介者模式通过集中控制对象间的交互有效地减少了对象间的直接耦合。这种解耦不仅使得系统更加灵活而且当系统需要扩展或修改时只需要调整中介者对象而不需要修改其他对象从而大大降低了系统的维护成本。 2. 中介者模式概述 中介者模式是一种设计模式它允许我们通过引入一个中介对象来简化复杂对象之间的通信。这种模式通过将对象间的直接通信转化为通过中介者进行间接通信从而降低了系统的耦合度。 2.1 定义与核心概念 中介者模式定义了一个中介对象用于封装一组对象之间的交互。这些对象称为同事Colleague对象。中介者模式的核心概念包括 中介者Mediator定义了一个接口用于与各同事对象通信。具体中介者Concrete Mediator实现了中介者接口协调各同事对象了解并维护这些对象。同事类Colleague每个同事类都知道它的中介者对象并且与中介者通信而不是与其他同事类直接通信。 通过这种方式中介者模式将系统的网状结构转化为星型结构从而简化了对象间的交互。 2.2 中介者模式的角色与职责 在中介者模式中各个角色的职责如下 中介者Mediator 定义同事对象之间交互的接口。知道并管理所有的同事对象。接收同事对象的请求并协调相应的同事对象进行响应。 具体中介者Concrete Mediator 实现中介者接口。协调各个同事对象的行为。可能需要了解并维护同事对象的状态。 同事类Colleague 每个同事类都知道它的中介者对象。当需要与其他同事类通信时通过中介者进行。通常只与中介者进行交互而不直接与其他同事类交互。 通过这些角色的明确分工中介者模式有效地将对象间的复杂交互转化为中介者与同事类之间的简单交互从而提高了系统的可维护性和扩展性。 3. 中介者模式的UML类图解析 3.1 类图结构详解 中介者模式的UML类图主要包括以下几个部分 Mediator中介者这是中介者模式的中心角色负责协调各个同事对象之间的交互。Colleague同事这是直接与中介者交互的角色通常会定义一个中介者引用以便通过中介者与其他同事进行通信。ConcreteMediator具体中介者实现中介者接口的具体类负责具体的中介者逻辑。ConcreteColleagueA/ConcreteColleagueB具体同事类实现同事接口的具体类每个具体同事类都可以与中介者进行交互。 3.2 角色间的交互关系 在类图中角色间的交互关系主要体现在以下几个方面 中介者与同事具体中介者类与具体同事类之间存在关联关系具体中介者类通常包含一个同事类的集合用于管理所有同事对象。同事与中介者具体同事类中包含对中介者的引用通过这个引用具体同事类可以与中介者进行交互以实现与其他同事的通信。同事与同事在具体实现中同事类之间不直接交互而是通过中介者进行交互。这样具体同事类之间的耦合度大大降低提高了系统的灵活性和可维护性。 3.3 UML类图示例 #mermaid-svg-ah9Pd87uAIlufsbO {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-ah9Pd87uAIlufsbO .error-icon{fill:#552222;}#mermaid-svg-ah9Pd87uAIlufsbO .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-ah9Pd87uAIlufsbO .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-ah9Pd87uAIlufsbO .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-ah9Pd87uAIlufsbO .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-ah9Pd87uAIlufsbO .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-ah9Pd87uAIlufsbO .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-ah9Pd87uAIlufsbO .marker{fill:#333333;stroke:#333333;}#mermaid-svg-ah9Pd87uAIlufsbO .marker.cross{stroke:#333333;}#mermaid-svg-ah9Pd87uAIlufsbO svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-ah9Pd87uAIlufsbO g.classGroup text{fill:#9370DB;fill:#131300;stroke:none;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:10px;}#mermaid-svg-ah9Pd87uAIlufsbO g.classGroup text .title{font-weight:bolder;}#mermaid-svg-ah9Pd87uAIlufsbO .nodeLabel,#mermaid-svg-ah9Pd87uAIlufsbO .edgeLabel{color:#131300;}#mermaid-svg-ah9Pd87uAIlufsbO .edgeLabel .label rect{fill:#ECECFF;}#mermaid-svg-ah9Pd87uAIlufsbO .label text{fill:#131300;}#mermaid-svg-ah9Pd87uAIlufsbO .edgeLabel .label span{background:#ECECFF;}#mermaid-svg-ah9Pd87uAIlufsbO .classTitle{font-weight:bolder;}#mermaid-svg-ah9Pd87uAIlufsbO .node rect,#mermaid-svg-ah9Pd87uAIlufsbO .node circle,#mermaid-svg-ah9Pd87uAIlufsbO .node ellipse,#mermaid-svg-ah9Pd87uAIlufsbO .node polygon,#mermaid-svg-ah9Pd87uAIlufsbO .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-ah9Pd87uAIlufsbO .divider{stroke:#9370DB;stroke:1;}#mermaid-svg-ah9Pd87uAIlufsbO g.clickable{cursor:pointer;}#mermaid-svg-ah9Pd87uAIlufsbO g.classGroup rect{fill:#ECECFF;stroke:#9370DB;}#mermaid-svg-ah9Pd87uAIlufsbO g.classGroup line{stroke:#9370DB;stroke-width:1;}#mermaid-svg-ah9Pd87uAIlufsbO .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#mermaid-svg-ah9Pd87uAIlufsbO .classLabel .label{fill:#9370DB;font-size:10px;}#mermaid-svg-ah9Pd87uAIlufsbO .relation{stroke:#333333;stroke-width:1;fill:none;}#mermaid-svg-ah9Pd87uAIlufsbO .dashed-line{stroke-dasharray:3;}#mermaid-svg-ah9Pd87uAIlufsbO #compositionStart,#mermaid-svg-ah9Pd87uAIlufsbO .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-ah9Pd87uAIlufsbO #compositionEnd,#mermaid-svg-ah9Pd87uAIlufsbO .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-ah9Pd87uAIlufsbO #dependencyStart,#mermaid-svg-ah9Pd87uAIlufsbO .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-ah9Pd87uAIlufsbO #dependencyStart,#mermaid-svg-ah9Pd87uAIlufsbO .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-ah9Pd87uAIlufsbO #extensionStart,#mermaid-svg-ah9Pd87uAIlufsbO .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-ah9Pd87uAIlufsbO #extensionEnd,#mermaid-svg-ah9Pd87uAIlufsbO .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-ah9Pd87uAIlufsbO #aggregationStart,#mermaid-svg-ah9Pd87uAIlufsbO .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-ah9Pd87uAIlufsbO #aggregationEnd,#mermaid-svg-ah9Pd87uAIlufsbO .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-ah9Pd87uAIlufsbO .edgeTerminals{font-size:11px;}#mermaid-svg-ah9Pd87uAIlufsbO :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} Mediator setColleague(colleague: Colleague) send(message: string, colleague: Colleague) receive(message: string, colleague: Colleague) Colleague Mediator mediator: Mediator receive(message: string, colleague: Colleague) send(message: string, colleague: Colleague) ConcreteMediatorextendsMediator Colleague colleagueA: Colleague Colleague colleagueB: Colleague send(message: string, colleague: Colleague) receive(message: string, colleague: Colleague) ConcreteColleagueAextendsColleague ConcreteMediator mediator: ConcreteMediator send(message: string, colleague: Colleague) receive(message: string, colleague: Colleague) ConcreteColleagueBextendsColleague ConcreteMediator mediator: ConcreteMediator send(message: string, colleague: Colleague) receive(message: string, colleague: Colleague) ConcreteMediator ConcreteColleagueA ConcreteColleagueB 4. 中介者模式的实现步骤 实现中介者模式通常包括以下几个步骤。 4.1 创建中介者接口与具体中介者 首先我们需要定义一个中介者接口它描述了同事对象之间交互的接口。然后实现这个接口创建一个具体的中介者类它负责协调各个同事对象的行为。 // 中介者接口 public interface Mediator {void send(String message, Colleague colleague);void receive(String message, Colleague colleague); }// 具体中介者类 public class ConcreteMediator implements Mediator {private ColleagueA colleagueA;private ColleagueB colleagueB;public ConcreteMediator(ColleagueA colleagueA, ColleagueB colleagueB) {this.colleagueA colleagueA;this.colleagueB colleagueB;}Overridepublic void send(String message, Colleague colleague) {if (colleague colleagueA) {colleagueB.receive(message, colleague);} else if (colleague colleagueB) {colleagueA.receive(message, colleague);}}Overridepublic void receive(String message, Colleague colleague) {// 处理接收到的消息System.out.println(colleague received: message);} }4.2 定义同事类及其与中介者的交互 接下来我们需要定义同事类并实现它们与中介者的交互。同事类通常会维护对中介者的引用以便与中介者进行通信。 // 同事类基类 public abstract class Colleague {protected Mediator mediator;public Colleague(Mediator mediator) {this.mediator mediator;}// 抽象方法同事类的具体交互逻辑public abstract void receive(String message, Colleague colleague); }// 具体同事类A public class ColleagueA extends Colleague {public ColleagueA(Mediator mediator) {super(mediator);}Overridepublic void receive(String message, Colleague colleague) {if (colleague instanceof ColleagueB) {System.out.println(ColleagueA received: message);// 处理接收到的消息可能需要回复String reply processMessage(message);mediator.send(reply, this);}}private String processMessage(String message) {// 处理消息并返回回复return Response from A;} }// 具体同事类B public class ColleagueB extends Colleague {public ColleagueB(Mediator mediator) {super(mediator);}Overridepublic void receive(String message, Colleague colleague) {if (colleague instanceof ColleagueA) {System.out.println(ColleagueB received: message);// 处理接收到的消息可能需要回复String reply processMessage(message);mediator.send(reply, this);}}private String processMessage(String message) {// 处理消息并返回回复return Response from B;} }在这段代码中我们定义了一个中介者接口和两个具体的中介者实现以及两个同事类及其具体实现。每个同事类都包含一个receive方法用于处理从中介者接收到的消息并可能需要回复中介者。中介者类则负责转发消息协调同事类之间的交互。 5. 代码实例聊天室系统 5.1 系统需求与设计思路 假设我们正在设计一个简单的聊天室系统用户可以发送消息给其他用户系统需要处理这些消息并将其转发给目标用户。为了实现解耦我们使用中介者模式来处理用户之间的交互。 设计思路如下 用户User可以发送消息给其他用户也可以接收来自其他用户的消息。中介者Chatroom负责管理所有用户处理用户之间的消息传递。 5.2 实现中介者接口与具体中介者 首先我们定义中介者接口和具体中介者类。 // 中介者接口 public interface Chatroom {void sendMessage(String message, User sender, User receiver);void receiveMessage(String message, User sender, User receiver); }// 具体中介者类 public class ConcreteChatroom implements Chatroom {private MapString, User users new HashMap();Overridepublic void sendMessage(String message, User sender, User receiver) {receiver.receiveMessage(message, sender, this);}Overridepublic void receiveMessage(String message, User sender, User receiver) {System.out.println(sender.getName() to receiver.getName() : message);}public void registerUser(User user) {users.put(user.getName(), user);} }5.3 实现用户类同事类 接下来我们实现用户类。 // 用户类 public class User {private String name;private Chatroom chatroom;public User(String name, Chatroom chatroom) {this.name name;this.chatroom chatroom;}public void sendMessage(String message, User sender, User receiver) {chatroom.sendMessage(message, sender, receiver);}public void receiveMessage(String message, User sender, User receiver) {System.out.println(sender.getName() to this.name : message);} }5.4 用户与中介者的交互逻辑 现在我们来模拟用户之间的交互逻辑。 public class ChatroomDemo {public static void main(String[] args) {Chatroom chatroom new ConcreteChatroom();User alice new User(Alice, chatroom);User bob new User(Bob, chatroom);chatroom.registerUser(alice);chatroom.registerUser(bob);alice.sendMessage(Hello, Bob!, alice, bob);bob.sendMessage(Hi, Alice!, bob, alice);} }在这个例子中我们创建了一个聊天室中介者和两个用户Alice和Bob。每个用户都可以发送消息给其他用户而中介者负责将消息转发给目标用户。这样用户之间不需要知道对方的存在只需要通过中介者进行通信实现了用户之间的解耦。 6. 代码实例航空交通控制系统 6.1 系统需求与设计思路 在航空交通控制系统的设计中我们需要处理多架飞机之间的动态关系如起飞、降落、避让等。为了简化对象之间的交互我们采用中介者模式来实现这一系统。 设计思路如下 飞机Plane能够发送和接收与其他飞机的交互信息如请求降落、请求起飞、通知避让等。交通控制器AirTrafficController作为中介者负责协调和管理所有飞机的行为。 6.2 实现中介者接口与具体中介者 首先我们定义中介者接口和具体中介者类。 // 中介者接口 public interface AirTrafficController {void sendRequest(Plane plane, String request, Plane target);void receiveRequest(Plane plane, String request, Plane target); }// 具体中介者类 public class ConcreteAirTrafficController implements AirTrafficController {private MapString, Plane planes new HashMap();Overridepublic void sendRequest(Plane plane, String request, Plane target) {target.receiveRequest(request, plane, this);}Overridepublic void receiveRequest(Plane plane, String request, Plane target) {System.out.println(plane.getId() to target.getId() : request);// 根据请求类型处理飞机之间的交互switch (request) {case Request Landing:// 处理降落请求break;case Request Takeoff:// 处理起飞请求break;case Request Avoidance:// 处理避让请求break;}}public void registerPlane(Plane plane) {planes.put(plane.getId(), plane);} }6.3 实现飞机类同事类 接下来我们实现飞机类。 // 飞机类 public class Plane {private String id;private AirTrafficController atc;public Plane(String id, AirTrafficController atc) {this.id id;this.atc atc;}public void sendRequest(String request, Plane target) {atc.sendRequest(this, request, target);}public void receiveRequest(String request, Plane sender, AirTrafficController atc) {System.out.println(sender.getId() to this.id : request);// 根据请求类型处理与目标飞机的交互switch (request) {case Request Landing:// 处理降落请求break;case Request Takeoff:// 处理起飞请求break;case Request Avoidance:// 处理避让请求break;}} }6.4 飞机与中介者的交互逻辑 现在我们来模拟飞机之间的交互逻辑。 public class AirTrafficControlDemo {public static void main(String[] args) {AirTrafficController atc new ConcreteAirTrafficController();Plane plane1 new Plane(Plane1, atc);Plane plane2 new Plane(Plane2, atc);atc.registerPlane(plane1);atc.registerPlane(plane2);plane1.sendRequest(Request Takeoff, plane2);plane2.sendRequest(Request Landing, plane1);} }在这个例子中我们创建了一个航空交通控制器和两架飞机。飞机可以发送请求给其他飞机而交通控制器负责协调和管理这些请求。通过这种方式飞机之间不需要直接通信而是通过交通控制器进行交互实现了飞机之间的解耦。 7. 中介者模式的优缺点分析 7.1 优点解耦、简化对象交互 中介者模式的主要优点在于它能够显著地降低对象之间的耦合度。通过引入一个中介者对象对象之间的直接交互被转化为通过中介者进行的间接交互。这样对象不需要知道其他对象的存在只需要知道中介者。这种解耦不仅使系统更加灵活而且当系统需要扩展或修改时只需要调整中介者对象而不需要修改其他对象从而大大降低了系统的维护成本。 此外中介者模式还可以简化对象之间的交互。对象不再需要处理复杂的网状交互而是通过简单地与中介者进行交互来完成它们的工作。这使得系统的逻辑更加清晰易于理解和维护。 7.2 缺点中介者可能变得复杂 尽管中介者模式有许多优点但它也存在一些潜在的缺点。最主要的缺点是中介者对象可能变得非常复杂。因为中介者需要知道所有同事对象的状态和它们之间的交互所以它可能会变得非常庞大和难以管理。 当系统中的对象数量增加时中介者需要维护的对象数量也会增加。这可能导致中介者对象变得非常复杂难以理解和维护。此外如果中介者对象出现故障整个系统可能会受到影响因为所有的交互都依赖于中介者。 总的来说中介者模式是一种非常有用的设计模式特别是当需要处理多个对象之间的复杂交互时。然而在实际应用中需要仔细考虑中介者对象的设计以避免其变得过于复杂和难以管理。 8. 中介者模式的应用场景 8.1 何时使用中介者模式 中介者模式适用于以下场景 对象之间存在复杂的网状依赖关系当多个对象之间存在复杂的相互依赖和通信时中介者模式可以帮助简化这些依赖关系。需要解耦多个对象当需要降低对象之间的耦合度以便更灵活地扩展或修改系统时可以使用中介者模式。多个对象之间的交互依赖于中介者当对象之间的交互依赖于中介者提供的协调时中介者模式可以简化这些交互。对象数量较少尽管中介者模式适用于处理多个对象之间的交互但在对象数量较少的情况下使用中介者模式可能显得有些过度复杂。 8.2 实际应用案例分析 中介者模式在实际应用中非常广泛以下是一些常见的应用案例 图形编辑器在图形编辑器中多个图形对象如矩形、圆形、文本等可能需要相互交互如组合、分离、变换等。使用中介者模式可以简化这些对象之间的依赖关系。 网络通信在网络通信中不同的客户端和服务器可能需要进行交互。中介者模式可以用来简化这些客户端和服务器之间的通信逻辑。 数据库事务管理在数据库事务管理中多个事务对象可能需要相互协调和同步。使用中介者模式可以简化这些事务对象之间的交互。 企业信息系统在企业信息系统中多个业务对象如订单、客户、产品等可能需要相互协作。中介者模式可以用来简化这些业务对象之间的依赖关系。 游戏设计在游戏设计中多个游戏对象如玩家、敌人、道具等可能需要相互交互。使用中介者模式可以简化这些游戏对象之间的依赖关系。
http://www.dnsts.com.cn/news/82290.html

相关文章:

  • 建设银行河北分行招聘网站wordpress 去掉头部栏目
  • php网站开发实例视频钱网站制作
  • 大港做网站天元建设集团有限公司申请破产了吗
  • iis网站视频无法播放国家职业资格证书网站
  • 做电影售票网站的难点做网站 天津
  • 企业建设网站应如何申请wordpress加一个字段
  • 优化推广网站淄博合肥小程序搭建
  • 合肥做网站的公微信引流的十个方法
  • 提高网站搜索排名jiasale wordpress 插件
  • 网页和网站做哪个好用施工企业安全管理制度
  • 网站源码区别wordpress 伪静态 分页
  • 做淘宝那样的网站麻烦吗网站开发asp.net和sql数据库
  • 吉林市网站建设招标mysql网站后台管理系统下载
  • 宁波网站推广专业服务长春网站快照优化公司
  • jsp建设网站教程吴江市建设局网站
  • 济南网站建设公司哪家好一点中国建设银行移动门户
  • 网站制作需要什么软件做直播网站找哪家网站
  • 网站开发中网页之间的链接形式apache建wordpress
  • 网站开发外包报价单网站搭建教程零基础
  • 深圳住房城乡建设局网站首页陕西省建设网官网首页
  • 阳城网站建设微信公众号开发创新
  • 网站 线框图网站兼容浏览器
  • 青岛外贸网站推广怎么自己刷推广链接
  • 网站后台清空三丰云免费服务器
  • 吴江企业网站制作虹口区建设工程管理网站
  • 做的比较好的医院网站西部数据网站助手
  • 西安做行业平台网站的公司本地合肥网站建设
  • 瓯北网站制作抖音官网链接网站怎么做
  • 天河建设网站公司长沙交互网站设计服务商
  • 定制设计网站公司制作网页代码大全