Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to deeply understand the intermediary pattern of Java design pattern

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you the intermediary pattern about how to deeply understand the Java design pattern. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

I. what is the intermediary model

A mediation object is used to encapsulate a series of object interactions. Intermediaries make objects do not need to explicitly reference each other, so that they are loosely coupled, and can change the interaction between them independently.

As a behavioral design pattern, the intermediary exposes a unified interface through which different objects or components of the system can communicate. After adding an intermediary object, all related objects communicate through the intermediary object instead of referencing each other, so when an object changes, you only need to notify the intermediary object.

II. The structure of the intermediary model

Mediator: intermediary interface. Define the method needed for interaction between colleagues, which can be a common method of communication, such as the changed method, which is used by everyone, or a small-scale interaction method.

ConcreteMediator: the concrete intermediary implements the object. It needs to understand and maintain each colleague object, and is responsible for coordinating the interaction of each colleague object.

Colleague: the definition of colleague class is usually implemented as an abstract class, which is mainly responsible for constraining the type of colleague object and implementing some common functions between concrete colleague classes. For example, every concrete colleague class should know the intermediary object, that is, the concrete colleague class will hold the intermediary object, so it can be defined into this class.

ConcreteColleague: specific colleague class, implement your own business, communicate with the holding intermediary when you need to communicate with other colleagues, and the intermediary will be responsible for interacting with other colleagues.

Third, the advantages and disadvantages of the intermediary model

Advantages: reduce the coupling between Colleague, so that individual Colleague classes and Mediator can be changed and reused independently

Disadvantages: the shortcomings of the intermediary model are obvious, because the "intermediary" bears more responsibility, so once the intermediary object has a problem, then the whole system will be significantly affected.

IV. The use scenario of the intermediary model

1. A set of well-defined objects that now require complex communication.

two。 Customize a behavior distributed across multiple classes without generating too many subclasses.

As you can see, mediation objects are mainly used to encapsulate behavior, and the participants in the behavior are those objects, but through intermediaries, these objects do not need to know each other.

5. Similarities and differences between intermediary model and publish / subscribe model.

1. The intermediary model is related to the business, while the subscription / publishing model has nothing to do with the business.

two。 Both modes have the effect of centralized scheduling, and objects are not directly involved in communication.

VI. The realization of the intermediary model

The Security Council acts as an intermediary

United Nations agencies category-Mediator category

/ / United Nations agency abstract class UnitedNations {/ / statement public abstract void Declare (string message, Country colleague);}

Country category-Colleague category

/ / country abstract class Country {protected UnitedNations mediator; public Country (UnitedNations mediator) {this.mediator = mediator;}}

American Class-- ConcreteColleague Class

/ / US class USA: Country {public USA (UnitedNations mediator): base (mediator) {} / / declare public void Declare (string message) {mediator.Declare (message, this);} / / get message public void GetMessage (string message) {Console.WriteLine ("America gets the other party's message:" + message);}}

Iraq class-ConcreteColleague class

/ / class Iraq of Iraq: Country {public Iraq (UnitedNations mediator): base (mediator) {} / / declare public void Declare (string message) {mediator.Declare (message, this);} / / get the message public void GetMessage (string message) {Console.WriteLine ("Iraq gets the message:" + message);}}

United Nations Security Council-ConcreteMediator category

/ / United Nations Security Council class UnitedNationsSecurity: UnitedNations {private USA colleague1; private Iraq colleague2; / / US public USA Colleague1 {set {colleague1 = value;}} / / Iraq public Iraq Colleague2 {set {colleague2 = value;}} public override void Declare (string message, Country colleague) {if (colleague = = colleague1) {colleague2.GetMessage (message) } else {colleague1.GetMessage (message);}

Client code

Class Program {/ / client code static void Main (string [] args) {UnitedNationsSecurity UNSC = new UnitedNationsSecurity (); USA C1 = new USA (UNSC); Iraq c2 = new Iraq (UNSC); UNSC.Colleague1 = C1; UNSC.Colleague2 = c2; c1.Declare ("No development of nuclear weapons, or war begins"); c2.Declare ("war begins without research and development") Console.Read ();}} Iraq gets the news from the other side: no nuclear weapons are allowed, or war will begin.

The United States got the news from the other side: if there is no research and development, the war will start.

The above is the intermediary pattern of how to deeply understand the Java design pattern shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report