做软装设计能用到的网站有哪些,惠州网站开发公司电话,百度官网首页登录入口,网站设置右击不了如何查看源代码小谈设计模式#xff08;26#xff09;—中介者模式 专栏介绍专栏地址专栏介绍 中介者模式分析角色分析抽象中介者#xff08;Mediator#xff09;具体中介者#xff08;ConcreteMediator#xff09;抽象同事类#xff08;Colleague#xff09;具体同事类#xff08;C… 小谈设计模式26—中介者模式 专栏介绍专栏地址专栏介绍 中介者模式分析角色分析抽象中介者Mediator具体中介者ConcreteMediator抽象同事类Colleague具体同事类ConcreteColleague 工作过程优缺点分析优点分析12 缺点分析12 适用场景12 Java程序分析分析123 输出结果 总结 专栏介绍
专栏地址
http://t.csdnimg.cn/VpriY
专栏介绍
主要对目前市面上常见的23种设计模式进行逐一分析和总结希望有兴趣的小伙伴们可以看一下会持续更新的。希望各位可以监督我我们一起学习进步加油各位。
中介者模式
中介者模式Mediator Pattern是一种行为型设计模式它通过将对象之间的通信封装到一个中介者对象中从而实现对象之间的松耦合。中介者模式可以减少对象之间的直接依赖提高系统的灵活性和可维护性。
分析
在中介者模式中存在一个中介者对象它封装了对象之间的通信逻辑。对象之间的通信不再直接发生而是通过中介者对象进行。当一个对象需要与其他对象进行通信时它不需要知道其他对象的具体细节只需要与中介者进行交互即可。
角色分析
抽象中介者Mediator
定义了中介者对象的接口它通常包含一个或多个抽象的通信方法用于定义对象之间的通信规则。
具体中介者ConcreteMediator
实现了抽象中介者的接口它通过协调各个同事对象来实现协作行为。
抽象同事类Colleague
定义了同事对象的接口它通常包含一个中介者对象的引用用于与中介者进行通信。
具体同事类ConcreteColleague
实现了抽象同事类的接口它与其他同事对象通过中介者进行通信。
工作过程
各个同事对象将自己的引用传递给中介者对象以便中介者对象能够与各个同事对象进行通信。 当一个同事对象需要与其他同事对象进行通信时它将请求发送给中介者对象。 中介者对象接收到请求后根据通信规则进行相应的处理并将请求转发给目标同事对象。 目标同事对象接收到请求后进行相应的处理。
优缺点分析
优点分析
1
减少了对象之间的直接依赖提高了系统的灵活性和可维护性。
2
将对象之间的通信集中到一个中介者对象中使得系统结构更加清晰。
缺点分析
1
中介者对象将承担较多的责任可能会变得复杂。
2
如果中介者对象存在过多的逻辑可能会影响系统的性能。
适用场景
1
当对象之间存在复杂的通信逻辑时可以使用中介者模式将通信逻辑集中到一个中介者对象中。
2
当对象之间的通信关系呈现网状结构时可以使用中介者模式将通信关系简化为星型结构。
Java程序分析
// 抽象中介者
interface Mediator {void sendMessage(String message, Colleague colleague);
}// 具体中介者
class ConcreteMediator implements Mediator {private Colleague colleague1;private Colleague colleague2;public void setColleague1(Colleague colleague1) {this.colleague1 colleague1;}public void setColleague2(Colleague colleague2) {this.colleague2 colleague2;}Overridepublic void sendMessage(String message, Colleague colleague) {if (colleague colleague1) {colleague2.receiveMessage(message);} else if (colleague colleague2) {colleague1.receiveMessage(message);}}
}// 抽象同事类
abstract class Colleague {protected Mediator mediator;public Colleague(Mediator mediator) {this.mediator mediator;}public abstract void sendMessage(String message);public abstract void receiveMessage(String message);
}// 具体同事类
class ConcreteColleague1 extends Colleague {public ConcreteColleague1(Mediator mediator) {super(mediator);}Overridepublic void sendMessage(String message) {mediator.sendMessage(message, this);}Overridepublic void receiveMessage(String message) {System.out.println(ConcreteColleague1 received message: message);}
}// 具体同事类
class ConcreteColleague2 extends Colleague {public ConcreteColleague2(Mediator mediator) {super(mediator);}Overridepublic void sendMessage(String message) {mediator.sendMessage(message, this);}Overridepublic void receiveMessage(String message) {System.out.println(ConcreteColleague2 received message: message);}
}// 测试类
public class MediatorPatternExample {public static void main(String[] args) {ConcreteMediator mediator new ConcreteMediator();ConcreteColleague1 colleague1 new ConcreteColleague1(mediator);ConcreteColleague2 colleague2 new ConcreteColleague2(mediator);mediator.setColleague1(colleague1);mediator.setColleague2(colleague2);colleague1.sendMessage(Hello from colleague1);colleague2.sendMessage(Hi from colleague2);}
}分析
1
在上述示例中Mediator是抽象中介者接口定义了中介者对象的通信方法。ConcreteMediator是具体中介者类实现了抽象中介者接口并通过协调各个同事对象来实现协作行为。
2
Colleague是抽象同事类定义了同事对象的接口并包含一个中介者对象的引用用于与中介者进行通信。ConcreteColleague1和ConcreteColleague2是具体同事类分别实现了抽象同事类的接口。
3
在测试类MediatorPatternExample中创建了具体中介者对象和具体同事对象并将同事对象的引用传递给中介者对象。然后通过同事对象调用sendMessage方法发送消息中介者对象根据通信规则进行处理并将消息转发给目标同事对象。最后目标同事对象接收到消息并进行处理。
输出结果
ConcreteColleague2 received message: Hello from colleague1
ConcreteColleague1 received message: Hi from colleague2
以上示例演示了中介者模式的基本实现通过中介者对象实现了对象之间的松耦合实现了对象之间的通信。总结
总结起来中介者模式通过将对象之间的通信封装到一个中介者对象中实现了对象之间的松耦合。它可以减少对象之间的直接依赖提高系统的灵活性和可维护性。中介者模式适用于对象之间存在复杂的通信逻辑或通信关系呈现网状结构的场景。