Introduction to the intermediary mode (mediator pattern) of Java design Patterns _java

Source: Internet
Author: User

The intermediary pattern of the Java design pattern is one of the behavioral patterns. Defines a mediation object to encapsulate the interaction between series objects. Intermediaries do not need to refer explicitly to each other, so that they are loosely coupled and can independently alter their interactions.

As shown in the following illustration:

Examples of intermediary models between producers, consumers and agents:

Public abstract class Personcolleague {protected Mediator mediator;
  Public Mediator Getmediator () {return mediator;
  } public void Setmediator (final mediator mediator) {this.mediator = mediator;

protected abstract void Message (String context);
  public class Mediator {private Personcolleague consumer = null;

  Private Producercolleague producer = null;
  Public Producercolleague Getproducer () {return producer;
  } public void Setproducer (final producercolleague producer) {this.producer = producer;
  Public Personcolleague Getconsumer () {return consumer;
  } public void Setconsumer (final personcolleague consumer) {This.consumer = consumer;
    Public mediator () {} public synchronized void message (final String context) {if (Consumer!= null)
    {System.out.println (context);
    else if (producer!= null) {System.out.println (context); }} public class ConsumercoLleague extends Personcolleague {public Consumercolleague (final mediator mediator) {this.mediator = mediator;
    } @Override protected void message (final String context) {This.mediator.setConsumer (this); This.mediator.message ("hello!
  I am a consumer ");
    } public class Producercolleague extends Personcolleague {public Producercolleague (final mediator mediator) {
  This.mediator = Mediator;
    } @Override protected void message (final String context) {this.mediator.setProducer (this);
  This.mediator.message (context); } public class Client {public static void main (final string[] args) {final Mediator mediator = new Mediator ()
    ;
    Final Personcolleague person = new Consumercolleague (mediator);

    Final Personcolleague Person1 = new Producercolleague (mediator);
    Person.message ("I am a Consumer");
  Person1.message ("I am a producer");

 }
}

Run Result:

Hello! I am a consumer
I am a producer

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.