I. Overview
Encapsulates a series of object interactions with a mediation object (mediator) that causes the objects to be loosely coupled and can independently alter the interaction between them without explicitly referencing each other. The mediator pattern, also known as the mediator pattern, is an object-behavior pattern.
Second, the application of the scene
Coordinates the interaction between multiple objects.
Third, UML class diagram
Iv. participants
1, Mediator (abstract intermediary): 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 each colleague object.
3, colleague (abstract colleague Class): It defines the public method 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 the abstract colleague class subclass; Each colleague object communicates with the mediator when it needs to communicate with other colleague objects, and communicates with the other colleague classes indirectly through the broker An abstract method declared in an abstract colleague class is implemented in a specific colleague class.
V. Use case learning [use case background: tenants-rent Agency-landlord story]
1. Abstract colleague class: Person.java
/**
* Abstract Colleague Class:
* @author lvzb.software@qq.com * * * * * *
/Public
abstract class Person {
// Maintaining an abstract intermediary's citation
protected mediator mediator;
protected String name;
Public person (String name, mediator mediator) {
this.mediator = mediator;
this.name = name;
/**
* Set Mediator Object
* @param mediator *
/public void Setmediator (mediator mediator) {
This.mediator = Mediator;
}
/**
* Sends a message to the intermediary
/protected abstract void SendMessage (String msg);
/**
* Get message from intermediary
/protected abstract void GetMessage (String msg);
2, housing tenants Renter.java
/**
* Specific colleague class: The role here is the tenant
* @author lvzb.software@qq.com * * * * * */public
class Renter extends person {Public
renter (String name, mediator mediator) {
super (name, mediator);
}
@Override
protected void SendMessage (String msg) {
mediator.operation (this, msg);
@Override
protected void GetMessage (String msg) {
System.out.println ("renter [" + name +]] received a message from the broker: "+ msg";
}
}
3, the specific colleague Class house lessor [landlord] Landlord.java
/**
* Specific colleague class: The role here is the landlord
* @author lvzb.software@qq.com * */Public
class landlord Extends person {public
landlord (String name, mediator mediator) {
super (name,mediator);
}
@Override
protected void SendMessage (String msg) {
mediator.operation (this, msg);
@Override
protected void GetMessage (String msg) {
System.out.println ("landlord [+ name +"] received a message from the broker: "+ msg);
}
}
4, Abstract intermediary class: Mediator.java
/**
* Abstract Intermediary class
* @author lvzb.software@qq.com
* * */Public
abstract class Mediator {
// Used to add storage "landlord" role
protected list<person> landlordlist = new arraylist<person> ();
Used to add the store "Ask for rent" 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 tenants Information
* @param landlord landlord entity/public
void Registerrenter (person landlord) {
Renterlist.add (landlord);
}
/**
* Declares an abstract method to implement the relay and coordination of messages by a specific intermediary subclass
/public abstract void operation (people person, String message);
}
5, the specific intermediary category: Housing intermediary Housemediator.java
/**
* Specific intermediary class: The role is the housing rental intermediary
* @author lvzb.software@qq.com * * * */public
class Housemediator Extends Mediator {
@Override public
void operation (person person, String message) {
if (person instanceof Renter) {
//Send the demand message of the rental house to the registered landlord for
(person landlord:landlordlist) {
landlord.getmessage (messages);
}
else if (person instanceof Landlord) {
//pass the landlord's rental room message to the registered housing tenants for
(persons renter:renterlist) {
renter.getmessage (message);}}}
6. Client Test class Client.java
/**
* Client Test class
* @author lvzb.software@qq.com
* * */Public
class Client {public
static void Main (string[] args) {
//instantiation of the housing intermediary
Mediator mediator = new Housemediator ();
Person Landlorda, Landlordb, renter;
Landlorda = new landlord ("Landlord Lee", mediator);
Landlordb = new landlord ("Landlord Li", mediator);
Renter = new Renter ("Xiao Lu", mediator);
Landlord Registration Intermediary
Mediator.registerlandlord (Landlorda);
Mediator.registerlandlord (LANDLORDB);
The tenant registration intermediary
Mediator.registerrenter (renter);
Ask the tenant to send for rent message
renter.sendmessage ("In Tianhe Park, rent a house near the price of 1000 yuan or about one months");
System.out.println ("--------------------------");
Landlord a sent a house rental message
landlorda.sendmessage ("Tianhe Park College Station Three Lane 27th, the four floor has a room to rent 1200/month light good near public transport Station");
}
7. Operation Result:
Landlord [landlord Lee] received a message from the intermediary: in Tianhe Park near the rental house, the price of about 1000 yuan one months
landlord [landlord Li] received the intermediary sent the news: in the Tianhe Park near the rental house, the price of 1000 yuan for one months
---------------- ----------
ask for a tenant [Xiao Lu] received a message from the intermediary: Tianhe Park College Station Three Lane 27th, the four floor has a room to rent 1200/month light good near the bus station