The intermediary model of Java schema

Source: Internet
Author: User

definition: encapsulates a series of object interactions with an intermediary object that allows each object to interact with each other without the need to be displayed, thus loosening the coupling and independently altering the interaction between them.

Type: behavior class pattern

class Diagram:

structure of the mediator pattern

The mediator pattern, also known as the mediator pattern, is divided into 3 parts from the class diagram: An abstract mediator: an interface that defines a coworker class object to an intermediary object, and is used for communication between peer classes. Typically includes one or more abstract event methods, which are implemented by subclasses. The broker implementation class: inherits from the abstract mediator and implements the event methods defined in the abstract mediator. Receives a message from a colleague class and then affects other simultaneous classes through the message. Colleague class: If an object affects other objects and is also affected by other objects, the two objects are called coworker classes. In the class diagram, the colleague class has only one, this is actually the omission of the reality, in the practical application, the colleague class generally consists of several components, they influence each other and depend on each other. The more colleagues class, the more complex the relationship. Also, a colleague class can behave as a set of implementations that inherit the same abstract class. In mediator mode, the peer class must pass through the mediator for message delivery.

Why use Mediator mode

In general, the relationship between colleague classes is more complex, when multiple colleagues are related to each other, their relationship will appear as a complex network structure, which is an overly coupled architecture, which is not conducive to the reuse of classes, but also unstable. For example, in the following figure, there are six colleagues class objects, and if object 1 changes, 4 objects will be affected. If object 2 changes, then 5 objects will be affected. That is, the design that is directly related to the colleague class is not good.

If you introduce an intermediary pattern, the relationship between coworker classes becomes a star structure, and as you can see from the diagram, any change in the class will only affect the class itself and the mediator, thus reducing the coupling of the system. A good design will not encapsulate all the object-relational logic in this class, but instead use a specialized class to manage the behavior that is not yours.

Let's use an example to illustrate what a colleague class is: There are two classes A and B, each with a number in the class, and you want to ensure that the number in class B is always 100 times times the number in Class A. That is, when you modify the number of Class A, multiply that number by 100 and assign it to Class B, and when you modify Class B, you assign the number divided by 100 to Class A. Class A b affects each other, and is called a coworker class. The code is as follows:[Java]  View Plain  copy abstract class abstractcolleague {        protected int number;          public int getnumber ( )  {           return number;        }          public void setnumber (int  Number) {           this.number = number;        }       //Abstract method to modify the associated object while modifying numbers         public abstract void setnumber (int number, abstractcolleague  Coll);  }      class colleaguea extends abstractcolleague{        public void setnumber (int number, abstractcolleague  Coll)  {           this.number = number;            coll.setnumber (number*100);       }   }      class colleagueb extends abstractcolleague{              public void setnumber (Int number,  abstractcolleague coll)  {            this.number = number;           coll.setnumber ( number/100);       }  }      public class  client {       public static void main (String[] args) {               abstractcolleague colla =  new colleaguea();           abstractcolleague collb = new  colleagueb ();                       system.out.println ("========== Set a impact b==========");            colla.setnumber (1288, COLLB);            system.out.println ("Colla number value:" +colla.getnumber ());            system.out.println ("COLLB number value:" +collb.getnumber ());               system.out.println ("========== setting B Impact a==========");            collb.setnumber (87635, colla);            system.out.println ("COLLB number value:" +collb.getnumber ());  

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.