6.20. Intermediary model

发布时间 :2025-10-25 12:24:44 UTC      

The intermediary pattern (Mediator Pattern) is used to reduce the complexity of communication between multiple objects and classes. This pattern provides a mediation class that usually handles communication between different classes and supports loose coupling, making the code easy to maintain. The intermediary model belongs to the behavioral model.

6.20.1. Introduction

意图: A mediation object is used to encapsulate a series of object interactions, and the mediator makes the objects do not need to explicitly reference each other, so that they are loosely coupled, and the interaction between them can be changed independently.

主要解决: There are a large number of relationships between objects, which is bound to cause the structure of the system to become very complex. at the same time, if an object changes, we also need to track the objects associated with it and deal with them accordingly.

何时使用: Multiple classes are coupled to each other to form a network structure.

如何解决: The above reticular structure is separated into a star structure.

关键代码: Communication between object Colleague is encapsulated into a class to handle separately.

应用实例: 1. Before China joined the WTO, various countries traded with each other, and the structure was complex. Now, various countries trade with each other through WTO. 2. Airport dispatching system. 3. MVC framework, in which C (controller) is the intermediary between M (model) and V (view).

优点: 1. Reduce the complexity of the class and transform one-to-many into one-to-one. 2. Decoupling between different classes. 3. It conforms to Dimitt’s principle.

缺点: Intermediaries can be large and complex to maintain.

使用场景: 1. There is a complex reference relationship between objects in the system, which leads to confusion in the structure of the dependency relationship between them and it is difficult to reuse the object. 2. Want to encapsulate the behavior of multiple classes through an intermediate class, but do not want to generate too many subclasses.

注意事项: It should not be used when duties are confused.

6.20.2. Realize

We demonstrate the intermediary model through a chat room example. In an example, multiple users can send messages to the chat room, and the chat room displays the message to all users. We will create two classes ChatRoom And User . User Object usage ChatRoom To share their information.

MediatorPatternDemo Our demo class uses the User Object to display the communication between them.

中介者模式的 UML 图

6.20.3. Step 1

Create a mediation class.

ChatRoom.java

importjava.util.Date;publicclassChatRoom{publicstaticvoidshowMessage(Useruser,Stringmessage){System.out.println(newDate().toString()+"["+user.getName()+"]
:"+message);}}

6.20.4. Step 2

Create the user class.

User.java

publicclassUser{privateStringname;publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicUser(Stringname){this.name=name;}publicvoidsendMessage(Stringmessage){ChatRoom.showMessage(this,message);}}

6.20.5. Step 3

Use User Object to display the communication between them.

MediatorPatternDemo.java

publicclassMediatorPatternDemo{publicstaticvoidmain(String[]args){Userrobert=newUser("Robert");Userjohn=newUser("John");robert.sendMessage("Hi!
John!");john.sendMessage("Hello! Robert!");}}

6.20.6. Step 4

Execute the program and output the result:

Thu Jan 31 16:05:46 IST 2013 [Robert] : Hi! John!
Thu Jan 31 16:05:46 IST 2013 [John] : Hello! Robert!
Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.