"Mediator pattern", the mediator pattern of Java design patterns

Source: Internet
Author: User

I. Overview

Using a Mediation object (the mediator) to encapsulate a series of object interactions, the mediator makes the objects do not need to explicitly reference each other, so that they are loosely coupled and can independently change the interaction between them. The mediator pattern, also known as the mediator pattern, is an object-behavioral pattern.


Second, the application scenario

Coordinates the interaction between multiple objects.


Third, UML class diagram


Iv. participants

1, Mediator (abstract mediator): It defines an interface that is used to communicate with each colleague object.

2, Concretemediator (specific intermediary): It is a subclass of abstract intermediaries, through the coordination of each colleague object to achieve collaborative behavior, it maintains a reference to the individual colleague objects.

3, colleague (abstract colleague Class): It defines the method of the common public of each colleague class, and declares some abstract methods for the subclass implementation, and it maintains a reference to the abstract mediator class, whose subclasses can communicate with the mediator through the reference.

4, Concretecolleague (Specific colleague Class): It is a subclass of abstract colleague class; Each colleague object communicates with the intermediary when it is necessary to communicate with other colleague objects, and indirectly through the intermediary to complete communication with other colleague classes. The abstract method that is declared in the abstract colleague class is implemented in the specific colleague class.


V. Use-case learning [use case background: Ask the tenant-housing rental Agency-landlord story]

1, abstract colleague class: Person.java

/** * Abstract Colleague Class: * @author  [email protected] * */public abstract class Person {//maintain a reference to an abstract mediator protected mediator mediator; protected string Name;public Person (string name, mediator mediator) {this.mediator = Mediator;this.name = name;} /** * Set Broker Object * @param mediator */public void Setmediator (mediator mediator) {this.mediator = mediator;} /** * Send message to Mediation */protected abstract void SendMessage (String msg);/** * Get message from mediation */protected abstract void GetMessage (Strin g msg);}
2, housing for rent-seeking peopleRenter.java

/** * Specific colleague class: The role here is renters * @author  [email protected] * */public class Renter extends person {public renter (String name, Mediator mediator) {Super (name, mediator);} @Overrideprotected void SendMessage (String msg) {mediator.operation (this, msg);} @Overrideprotected void GetMessage (String msg) {System.out.println ("tenant [" + name + "] receives the message from the intermediary:" + msg);}}
3. Specific co-worker type housing lessor [landlord]Landlord.java

/** * Specific colleague class: The role here is landlord * @author  [email protected] * */public class Landlord extends person {public landlord (String Nam E, mediator Mediator) {super (name,mediator);} @Overrideprotected void SendMessage (String msg) {mediator.operation (this, msg);} @Overrideprotected void GetMessage (String msg) {System.out.println ("landlord [" + name + "] receives the message from the intermediary:" + msg);}}
4. Abstract Intermediary class:Mediator.java

/** * Abstract Broker class * @author  [email protected] * */public abstract class Mediator {//for adding storage "landlord" role protected list<person& Gt Landlordlist = new arraylist<person> ();//for adding storage "tenant" role protected list<person> renterlist = new ArrayList <Person> ();/** * Intermediary registered landlord information * @param landlord landlord entity */public void Registerlandlord (person landlord) {Landlordlist.add (landlord);} /** * Intermediary Registration for tenant information * @param landlord landlord entity */public void Registerrenter (person landlord) {Renterlist.add (landlord);} /** * declares that the abstract method implements the relay and coordination of messages by a specific mediator subclass */public Abstract void operation (person person, String message);}
5. Specific intermediary Category: Housing intermediaryHousemediator.java

/** * Specific Intermediary class: Here is the role of housing rental agent * @author  [email protected] * */public class Housemediator extends mediator {@Overridepubli c void operation (person person, String message) {if (person instanceof renter) {////The demand message for the rental house is passed to the registered landlord for the person landlord:l Andlordlist) {landlord.getmessage (message);}} else if (person instanceof Landlord) {//the landlord's rental room message is passed to the registered housing tenants for the (person renter:renterlist) {Renter.getmessage ( message);}}}
6, the client test class Client.java

/** * Client Test class * @author  [email protected] * */public class Client {public static void main (string[] args) {//instantiate house intermediary Me Diator Mediator = new Housemediator (); Person Landlorda, Landlordb, Renter;landlorda = new landlord ("Landlord Lee", mediator); Landlordb = new landlord ("Landlord Li", mediator); R Enter = new Renter ("Small Lu", mediator);//Landlord Registration intermediary Mediator.registerlandlord (LANDLORDA); Mediator.registerlandlord ( LANDLORDB)///Mediator.registerrenter (renter);//Ask the tenant to send the rental information renter.sendmessage ("Rent a house near Tianhe Park, the price of about 1000 yuan one months"); System.out.println ("--------------------------");//landlord a Send home rental message landlorda.sendmessage ("Tianhe Park College Station Three Lane 27th, four floor has one room one hall for rent  1200/month  light good near bus station ");}}
7. Operation Result:

Landlord [landlord Lee] received a message from the intermediary: Rent a house near Tianhe Park, the price of about 1000 yuan one months landlord [landlord Li] received a message from the intermediary: Rent a house near Tianhe Park, Price of about 1000 yuan one months--------------------------for tenants [small LV] received the message from the intermediary: Tianhe Park College Station Three Lane 27th, the four floor has one room one hall rental  1200/month  light good near the bus station





"Mediator pattern", the mediator pattern of Java design patterns

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.