Intermediary model
Intermediary model (Mediator Pattern) is used to reduce the complexity of communications between a plurality of objects and classes. This mode provides an intermediate class, which generally handles the communication between the different classes and the loosely coupled, making the code easier to maintain. Mediator pattern belongs behavioral patterns.
Introduction
Intent: with a mediation object that encapsulates a set of objects interact intermediary keeping objects explicitly referring to each other, so that it loosely coupled, and can be changed independently of the interaction between them.
Mainly to solve: there are a lot of objects and relationships between objects, this will inevitably lead to the structure of the system becomes very complicated, but if an object is changed, we also need to track objects associated with it, and make the appropriate treatment .
When to use: a plurality of classes coupled to each other to form a network structure.
How to solve: separating said network structure of the star structure.
The key code: the communication between the encapsulated object class Colleague a single process.
Application examples: 1, prior to China's accession to WTO countries trade with each other is a complex structure, it is by WTO countries to trade with each other. 2, airport scheduling system. 3, MVC frame, where C (controller) is M (models) and V (view) of the intermediary.
Advantages: 1, reduces the complexity of the class, converted into the one-to-many. 2, the decoupling between classes. 3, in line with the principles of Demeter.
Cons: Mediator will be huge, complicated by difficult to maintain.
Be used: 1, a reference to the presence of more complex relationships, dependencies between them leading to confusion and difficult to reuse the structure of the object between the objects in the system. 2, through a like plurality of intermediate class to encapsulate the behavior classes, and do not want to generate too many subclasses.
Note: should not be used when duty chaos.
achieve
Let's walk through the intermediary model chat rooms instance. Example, a plurality of users can send messages to the chat room, the chat room message is displayed to all users. We will create two classes ChatRoom and the User . User objects using ChatRoom ways to share their news.
MediatorPatternDemo , our demo class uses User objects to display the communication between them.
step 1
Create the mediation classes.
ChatRoom.java
public class ChatRoom {
public static void showMessage(User user, String message){
System.out.println(new Date().toString()
+ " [" + user.getName() +"] : " + message);
}
}
Step 2
Create a user class.
User.java
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public User(String name){
this.name = name;
}
public void sendMessage(String message){
ChatRoom.showMessage(this,message);
}
}
Step 3
Use User object to display the communication between them.
MediatorPatternDemo.java
public static void main(String[] args) {
User robert = new User("Robert");
User john = new User("John");
robert.sendMessage("Hi! John!");
john.sendMessage("Hello! Robert!");
}
}
Step 4
The implementation of the program, output:
Thu Jan 31 16 : 05 : 46 IST 2013 [ Robert ] : Hi ! John ! Thu Jan 31 16 : 05 : 46 IST 2013 [ John ] : Hello ! Robert !
In real life, there are many intermediary model's figure, such as QQ game platform, chat rooms, QQ group, messaging platforms, and real estate agent. Whether QQ game or QQ group, they are acting as a middle platform, QQ users can log in the middle of the platform to communicate with other QQ users, without these intermediate platform, if we want to chat with friends, it may need to face it it. Telephone, text messaging is also an intermediate platform, with this intermediate platform, users are not directly dependent on each other and users only need to rely on the middle platform on it, all the operations by the middle platform to distribute.
Intermediary model, defines a mediation object to encapsulate the interaction between a series of objects. Mediator the respective objects do not explicitly referring to each other between, so that the coupling is lowered, and can be changed independently interactions therebetween.
Design ideas and code implementation:
以现实生活中打牌的例子来实现下中介者模式。打牌总有输赢,对应的则是货币的变化,如果不用中介者模式的话,实现如下:
/// <summary>
/// 抽象牌友类
/// </summary> public abstract class AbstractCardPartner { public int Money { get; set; } public abstract void ChangeMoney(int money, AbstractCardPartner other); } /// <summary> /// 牌友A /// </summary> public class PartnerA : AbstractCardPartner { public override void ChangeMoney(int money, AbstractCardPartner other) { Money += money; other.Money -= money; } } /// <summary> /// 牌友B /// </summary> public class PartnerB : AbstractCardPartner { public override void ChangeMoney(int money, AbstractCardPartner other) { Money += money; other.Money -= money; } } /// <summary> /// 调用 /// </summary> /// <param name="args"></param> static void Main(string[] args) { AbstractCardPartner A = new PartnerA(); A.Money = 20; AbstractCardPartner B = new PartnerB(); B.Money = 20; // A赢了B的钱减少 A.ChangeMoney(5, B); Console.WriteLine("A 现在的钱是:{0}", A.Money); // 应该是25 Console.WriteLine("B 现在的钱是:{0}", B.Money); // 应该是15 // B赢了A的钱减少