The intermediary mode _java of Java design pattern

Source: Internet
Author: User

Intermediary mode

Object-oriented design encourages the distribution of behavior across objects, which can cause many connections between objects. In the worst case, each object needs to know all the other objects.

While splitting a system into many objects enhances reusability, the proliferation of interconnected objects can reduce their reusability. A large number of connections makes it impossible for an object to work with no other object (the system behaves as an indivisible whole), and it is difficult to make any major changes to the system behavior at this time. Because behavior is distributed among many objects, the result is that many subclasses have to be defined to customize the behavior of the system. This brings us to the intermediary object Mediator:


Through the intermediary object, the network structure can be transformed into the intermediary-centric star structure , each object no longer directly related to another object, but mediated by mediator object. The introduction of intermediary objects , It also makes the structure of the system not cause a lot of modification due to the introduction of new objects.

Mediator Pattern: also called Mediator mode , a mediator object (mediator) is used to encapsulate the interaction of a series of objects so that the objects do not need to be shown to each other to refer to each other, so that the coupling is loose , and They can change their interactions independently :


(Image source: Design pattern: The basis of reusable object-oriented software) Tips: Each colleague only know the existence of mediator , do not need to know whether other colleague exist (or how to decouple it), it simply send the message to mediator, The mediator is then forwarded to the other colleague( Mediator stores all colleague relationships, and only mediator knows how many/ Colleague).

Pattern implementation

The United Nations forwarded states ' statements to mediate relations between States:
States send and receive messages to the United Nations Security Council, where the Security Council forwards requests ' appropriately ' to achieve collaborative action between States:

Colleague

Abstract colleague class, define the public method of each colleague:

/**
 * @author Jifang
 * @since 16/8/28 afternoon 4:22.
 * * Public
abstract class Country {

 protected unitednations mediator;

 private String name;

 Public Country (unitednations Mediator, String name) {
  this.mediator = mediator;
  this.name = name;

 Public String GetName () {return
  name;
 }

 protected abstract void Declare (String msg);

 protected abstract void Receive (String msg);

--------------------------------------------------------------------------------

Concretecolleague

Specific colleague class:
• Each colleague class knows its mediator object.
• Each colleague object communicates with its mediator when it is necessary to communicate with other colleagues.

 Class USA extends Country {public USA (unitednations Mediator, String name) {Super (
 Mediator, name);
 @Override public void Declare (String msg) {Mediator.declare (this, msg);
 @Override public void receive (String msg) {System.out.println ("U.S. Received: [+ msg +]");
 } class Iraq extends Country {public Iraq (unitednations Mediator, String name) {Super (mediator, name);
 @Override public void Declare (String msg) {Mediator.declare (this, msg);
 @Override public void receive (String msg) {SYSTEM.OUT.PRINTLN ("Iraq Received: [+ msg +]");
 Class extends Country {public unitednations Mediator, String name) {Super (mediator, name);
 @Override public void Declare (String msg) {Mediator.declare (this, msg);
 @Override public void receive (String msg) {System.out.println ("China Received: [+ msg +]"); }
}

--------------------------------------------------------------------------------

Mediator

Abstract Mediator: Defines an interface for communicating with each colleague object:

Public abstract class Unitednations {

 protected list<country> countries = new linkedlist<> ();

 public void Register (Country Country) {
  countries.add (Country);
 }

 public void Remove (Country Country) {
  countries.remove (Country);
 }

 protected abstract void Declare (Country Country, String msg);


--------------------------------------------------------------------------------

Concretemediator

Specific intermediaries:
• Understand and maintain all of its colleagues;
• Implement collaborative behavior through coordination of colleagues ' objects (receiving messages from co-workers, issuing commands to specific colleagues).

Class Unitednationssecuritycouncil extends Unitednations {

 /**
  * The council intervened in the Middle
  * *
  @param country
  * param msg
  */
 @Override
 protected void Declare (Country Country, String msg) {for
  (Country Tocountry:cou ntries) {
   if (!tocountry.equals (country)) {
    String name = Country.getname ();
    Tocountry.receive (name + "Peace": "+ msg";
   }}
  }


If there is no expansion, then mediator can be combined with concretemediator.
Client

public class Client {

 @Test public
 void Client () {
  unitednations mediator = new Unitednationssecuritycouncil ();

  Country USA = new USA (mediator, "United States");
  Country country = New China (mediator, "Chinese");
  Country Iraq = new Iraq (mediator, "Iraq");

  Mediator.register (USA);
  Mediator.register (a);
  Mediator.register (Iraq);

  Usa.declare ("I want to fight Iraq, who cares who I am anxious!!!");
  System.out.println ("----------");
  China.declare ("We strongly condemn!!!");
  System.out.println ("----------");
  Iraq.declare ("Come on, hurt each other!!!");
 }


--------------------------------------------------------------------------------

Summary

The presence of mediator reduces the coupling between the colleague, allowing for the independent alteration and reuse of colleague and mediator, and the abstraction of the object's collaboration, the mediation as an independent concept and its encapsulation in an object. The focus of such attention is shifted from the behavior of the objects themselves to the interaction between them, so that the system can be viewed in a more macroscopic perspective.

• Applicability
The intermediary model is easy to use in the system and is easily misused in the system. When the system appears "Many-to-many" Interactive complex object group, do not rush to use intermediaries, it is best to first reflect on whether the design of the system is reasonable. Because Concretemediator controls centralization, it turns interaction complexity into an intermediary's complexity, making intermediaries more complex than either concretecolleague. It is recommended that you use the mediator pattern in the following situations:
◦ A set of objects to communicate in well-defined but complex ways. The resulting interdependence structure is confusing and difficult to understand.
◦ An object references many other objects and communicates directly with them, which makes it difficult to reuse the object.
◦ wants to customize the behavior of a distribution in multiple classes without having to generate too many subclasses.

• Related mode
The difference between a ◦facade and an intermediary is that it abstracts an object subsystem, which provides a more convenient interface, and its protocol is one-way, that is, the façade object requests the subsystem class, but the opposite is not. Instead, mediator provides collaborative behavior that each colleague object does not support or cannot support, and the protocol is multi-directional.
◦colleague can communicate with mediator using the Observer mode.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.