Java design Pattern (14): Behavioral-Mediator Mode (mediator)

Source: Internet
Author: User
Tags dname

Scene

If there is no general manager. The following three departments: Finance Department, marketing department, Research and Development department. Finance department to pay, let everyone check the company needs to market and Research and development department to Ventilate; The marketing department needs to take a new project, the development department needs to deal with the technology, the financial department to finance. The marketing department deals with various departments. Although there are only three departments, the relationship is very messy.


In fact, the company has a general manager. Each department has everything to inform the general manager here, the general manager again informs each related department.

This is a typical broker pattern.
The general manager played an intermediary and coordinated role


Core

If the connection between objects in a system is rendered as a mesh structure, there are a number of many-to-many relationships between objects that will result in relationships and their complexities, called "colleague Objects"

We can introduce a mediator object so that each colleague object only deals with the intermediary object, and the complex mesh structure is dissolved into the following star-like structure.


The essence of the intermediary model

Decouple the interaction between multiple colleague objects. Each object holds a reference to the Mediator object, dealing only with the Mediator object. We manage these interactions uniformly through the Mediator object.

Public interface Department {/**/void selfaction ();/* Make a request to the general Manager */void Outaction ();} public class development implements department{/* holds a reference to the intermediary */private mediator Mediator;public development (mediator Mediator) {super (); this.mediator = Mediator;mediator.register ("development", this);} @Overridepublic void Selfaction () {//TODO auto-generated method StubSystem.out.println ("Focus on research and development");} @Overridepublic void Outaction () {//TODO auto-generated Method StubSystem.out.println ("Report work: no money, need financial support"); Mediator.command ("finacial");}} public class Finacial implements department{/* holds a reference to the intermediary */private mediator Mediator;public finacial (mediator mediator) { Super (); this.mediator = Mediator;mediator.register ("finacial", this);} @Overridepublic void Selfaction () {//TODO auto-generated method StubSystem.out.println ("Count Money"); @Overridepublic void Outaction () {//TODO auto-generated Method StubSystem.out.println ("Report work: Too much money, how to spend");}} public class market implements department{/* holds a reference to the intermediary */private Mediator Mediator;public Market (MediaTor mediator) {super (); this.mediator = Mediator;mediator.register ("Market", this);} @Overridepublic void Selfaction () {//TODO auto-generated method StubSystem.out.println ("Connect Project"); @Overridepublic void Outaction () {//TODO auto-generated method StubSystem.out.println ("Reporting work: Requires financial support"); Mediator.command ("finacial");}} Public interface Mediator {void Register (String dname,department dept), void Command (string dname);} Public class president implements Mediator{private map<string, department> Map = new hashmap<string, department& gt; (); @Overridepublic void Register (String dname, Department dept) {//TODO auto-generated method Stubmap.put (dname, Dept );} @Overridepublic void Command (String dname) {//TODO auto-generated method Stubmap.get (dname). Selfaction ();}} public class Client {public static void main (string[] args) {//TODO auto-generated method Stubmediator m = new President ( ); Market Market = New Market (m);D evelopment DEVP = new development (m); Finacial f = new finacial (m); market.selfactiOn (); Market.outaction ();}} 


Java design Pattern (14): Behavioral-Mediator Mode (mediator)

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.