JAVA Design Pattern Mediator pattern

Source: Internet
Author: User

Use

the Mediator pattern (mediator) uses a mediation object to encapsulate a series of object interactions. The mediator makes the objects not need to be referenced to each other, so that they are loosely coupled and can independently change the interaction between them. The mediator pattern is aBehavioral Patterns。


structure

Figure-Broker Pattern structure diagram

Mediator: Defines an AC interface for the colleague object.Abstract classMediator {
PublicAbstractvoidSend (String message, colleague colleague);
}

Concretemediator : Implements the AC interface in the mediator. All colleague objects need to be understood and maintained in this class.

classConcretemediatorextendsMediator {
PrivateConcreteColleague1 colleague1;
PrivateConcreteColleague2 colleague2;

PublicvoidSetColleague1 (ConcreteColleague1 colleague1) {
This. colleague1 = colleague1;
}

PublicvoidSetColleague2 (ConcreteColleague2 colleague2) {
This. colleague2 = colleague2;
}

@Override
PublicvoidSend (String message, colleague colleague) {
if(colleague = = colleague1) {
Colleague2. Notify (message);
}Elseif(colleague = = Colleague2) {
Colleague1. Notify (message);
}Else{
System.out.println ("error!");
}
}
}

colleague Group : Each colleague object should know its mediator object, but not other colleague objects. It can only contact the Mediator object.

Abstractclasscolleague {
protectedMediator mediator;

PublicColleague (mediator mediator) {
This. Mediator = Mediator;
}

Public voidSend (String message) {
Mediator. Send (Message, This);
}

PublicAbstractvoidNotify (String message);
}

classConcreteColleague1extendscolleague {
PublicConcreteColleague1 (mediator mediator) {
Super(mediator);
}

@Override
PublicvoidNotify (String message) {
System.out.println ("Colleague 1 Get information:" + message);
}
}

classConcreteColleague2extendscolleague {
PublicConcreteColleague2 (mediator mediator) {
Super(mediator);
}

@Override
PublicvoidNotify (String message) {
System.out.println ("Colleague 2 Get information:" + message);
}
}

Test code

PublicclassMediatorpattern {
PublicStaticvoidMain (string[] args) {
Concretemediator Mediator =NewConcretemediator ();
ConcreteColleague1 colleague1 =NewConcreteColleague1 (mediator);
ConcreteColleague2 colleague2 =NewConcreteColleague2 (mediator);

Mediator.setcolleague1 (colleague1);
Mediator.setcolleague2 (colleague2);

Colleague1. Send ("How is You?");
Colleague2. Send ("Fine, thank. And you? ");
Colleague1. Send ("I ' m fine. Thankes. ");
}
}View Code
Application Scenarios

A mediator pattern is typically used to define a well-defined but interactive collection of objects that are more responsible. The resulting interdependence is unstructured and difficult to understand.

It is difficult to reuse objects that have referential relationships and interactions with many other objects.

You want to customize a behavior that is distributed across multiple classes without having to generate too many subclasses.

JAVA Design Pattern Mediator pattern

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.