Java中的中介器设计模式

Mediator design pattern is one of the behavioral design pattern, so it deals with the behaviors of objects. Mediator design pattern is used to provide a centralized communication medium between different objects in a system.

中介者设计模式是行为设计​​模式之一,因此它处理对象的行为。 中介器设计模式用于在系统中不同对象之间提供集中式通信介质。

中介者设计模式 (Mediator Design Pattern)

According to GoF, mediator pattern intent is:

根据GoF,中介者模式意图是:

Allows loose coupling by encapsulating the way disparate sets of objects interact and communicate with each other. Allows for the actions of each object set to vary independently of one another.
通过封装不同对象集相互交互和通信的方式,允许松散耦合。 允许每个对象集的动作彼此独立地变化。

Mediator design pattern is very helpful in an enterprise application where multiple objects are interacting with each other. If the objects interact with each other directly, the system components are tightly-coupled with each other that makes higher maintainability cost and not hard to extend. Mediator pattern focuses on provide a mediator between objects for communication and help in implementing lose-coupling between objects.

Mediator设计模式在企业应用程序中非常有用,在该应用程序中多个对象相互交互。 如果对象直接相互影响,则系统组件彼此紧密耦合,这会增加可维护性成本,并且很难扩展。 调解器模式致力于在对象之间提供调解器以进行通信,并帮助实现对象之间的损耗耦合。

Air traffic controller is a great example of mediator pattern where the airport control room works as a mediator for communication between different flights. Mediator works as a router between objects and it can have it’s own logic to provide way of communication.

空中交通管制员是调解员模式的一个很好的例子,其中机场控制室充当不同航班之间通信的调解员。 中介器充当对象之间的路由器,并且可以具有自己的逻辑来提供通信方式。

The system objects that communicate each other are called Colleagues. Usually we have an interface or abstract class that provides the contract for communication and then we have concrete implementation of mediators.

相互通信的系统对象称为同事。 通常,我们有一个提供通信契约的接口或抽象类 ,然后我们有介体的具体实现。

For our example, we will try to implement a chat application where users can do group chat. Every user will be identified by it’s name and they can send and receive messages. The message sent by any user should be received by all the other users in the group.

对于我们的示例,我们将尝试实现一个聊天应用程序,用户可以在其中进行群聊。 每个用户都将通过其名称进行标识,他们可以发送和接收消息。 该组中的所有其他用户都应接收任何用户发送的消息。

介体模式界面 (Mediator Pattern Interface)

First of all we will create Mediator interface that will define the contract for concrete mediators.

首先,我们将创建Mediator接口,该接口将为具体的调解人定义合同。

ChatMediator.java

ChatMediator.java

package com.journaldev.design.mediator;

public interface ChatMediator {

	public void sendMessage(String msg, User user);

	void addUser(User user);
}

调解人模式同事界面 (Mediator Pattern Colleague Interface)

Users can send and receive messages, so we can have User interface or abstract class. I am creating User as abstract class like below.

用户可以发送和接收消息,因此我们可以具有用户界面或抽象类。 我创建用户作为抽象类,如下所示。

User.java

User.java

package com.journaldev.design.mediator;

public abstract class User {
	protected ChatMediator mediator;
	protected String name;
	
	public User(ChatMediator med, String name){
		this.mediator=med;
		this.name=name;
	}
	
	public abstract void send(String msg);
	
	public abstract void receive(String msg);
}

Notice that User has a reference to the mediator object, it’s required for the communication between different users.

请注意,用户具有对中介对象的引用,这是不同用户之间进行通信所必需的。

混凝土调解员 (Concrete Mediator)

Now we will create concrete mediator class, it will have a list of users in the group and provide logic for the communication between the users.

现在,我们将创建具体的调解器类,它将在组中具有用户列表,并为用户之间的通信提供逻辑。

ChatMediatorImpl.java

ChatMediatorImpl.java

package com.journaldev.design.mediator;

import java.util.ArrayList;
import java.util.List;

public class ChatMediatorImpl implements ChatMediator {

	private List<User> users;
	
	public ChatMediatorImpl(){
		this.users=new ArrayList<>();
	}
	
	@Override
	public void addUser(User user){
		this.users.add(user);
	}
	
	@Override
	public void sendMessage(String msg, User user) {
		for(User u : this.users){
			//message should not be received by the user sending it
			if(u != user){
				u.receive(msg);
			}
		}
	}

}

中介者设计模式混凝土同事 (Mediator Design Pattern Concrete Colleague)

Now we can create concrete User classes to be used by client system.

现在,我们可以创建供客户端系统使用的具体User类。

UserImpl.java

UserImpl.java

package com.journaldev.design.mediator;

public class UserImpl extends User {

	public UserImpl(ChatMediator med, String name) {
		super(med, name);
	}

	@Override
	public void send(String msg){
		System.out.println(this.name+": Sending Message="+msg);
		mediator.sendMessage(msg, this);
	}
	@Override
	public void receive(String msg) {
		System.out.println(this.name+": Received Message:"+msg);
	}

}

Notice that send() method is using mediator to send the message to the users and it has no idea how it will be handled by the mediator.

请注意,send()方法正在使用中介程序将消息发送给用户,并且不知道中介程序将如何处理该消息。

介体模式示例客户端程序代码 (Mediator Pattern Example Client Program Code)

Let’s test this our chat application with a simple program where we will create mediator and add users to the group and one of the user will send a message.

让我们用一个简单的程序来测试我们的聊天应用程序,在该程序中,我们将创建介体并将用户添加到组中,并且其中一个用户将发送一条消息。

ChatClient.java

ChatClient.java

package com.journaldev.design.mediator;

public class ChatClient {

	public static void main(String[] args) {
		ChatMediator mediator = new ChatMediatorImpl();
		User user1 = new UserImpl(mediator, "Pankaj");
		User user2 = new UserImpl(mediator, "Lisa");
		User user3 = new UserImpl(mediator, "Saurabh");
		User user4 = new UserImpl(mediator, "David");
		mediator.addUser(user1);
		mediator.addUser(user2);
		mediator.addUser(user3);
		mediator.addUser(user4);
		
		user1.send("Hi All");
		
	}

}

Notice that client program is very simple and it has no idea how the message is getting handled and if mediator is getting user or not.

请注意,客户端程序非常简单,并且不知道如何处理消息以及中介程序是否正在获得用户。

Output of the mediator pattern example program is:

中介者模式示例程序的输出为:

Pankaj: Sending Message=Hi All
Lisa: Received Message:Hi All
Saurabh: Received Message:Hi All
David: Received Message:Hi All

中介者模式类图 (Mediator Pattern Class Diagram)

JDK中的中介者模式示例 (Mediator Pattern Example in JDK)

介体设计模式要点 (Mediator Design Pattern Important Points)

  • Mediator pattern is useful when the communication logic between objects is complex, we can have a central point of communication that takes care of communication logic.

    当对象之间的通信逻辑很复杂时,调解器模式非常有用,我们可以拥有一个负责通信逻辑的中心通信点。
  • Java Message Service (JMS) uses Mediator pattern along with Observer pattern to allow applications to subscribe and publish data to other applications.

    Java消息服务(JMS)使用Mediator模式和Observer模式来允许应用程序向其他应用程序订阅和发布数据。
  • We should not use mediator pattern just to achieve lose-coupling because if the number of mediators will grow, then it will become hard to maintain them.

    我们不应该仅仅使用调解器模式来实现损失耦合,因为如果调解器的数量增加,那么将很难维持它们。

That’s all for mediator design pattern and it’s implementation in java.

这就是介体设计模式的全部内容,并且是用Java实现的。

翻译自: https://www.journaldev.com/1730/mediator-design-pattern-java

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值