The mediator pattern for Java design patterns

Source: Internet
Author: User

Broker mode

Encapsulates a series of object interactions with a mediation object. The mediator makes the objects not need to explicitly reference each other, so that they are loosely coupled, and can independently change the interaction between them.

Broker Mode UML Diagram

Broker Pattern Code

 

Package com.roc.mediator;/** * Abstract Mediator class * @author LIAOWP * */public abstract class Mediator {public    abstract void Send ( String Message,colleague colleague);    }
Package Com.roc.mediator;public class Concretemediator extends mediator{    private Colleague1 colleague1;    Private Colleague2 colleague2;        public void setColleague1 (Colleague1 colleague1) {        this.colleague1 = colleague1;    }    public void SetColleague2 (Colleague2 colleague2) {        this.colleague2 = colleague2;    }    public void Send (String message, colleague colleague) {        if (colleague = = colleague1) {            colleague2. Notify (message);        } else{            colleague1. Notify (message);}}}                    
Package com.roc.mediator;/** * Abstract Colleague Class * @author LIAOWP * */public Abstract class Colleague {    protected mediator Mediat or;        Public colleague (mediator mediator) {        this.mediator=mediator;    }}
Package Com.roc.mediator;public class Colleague1 extends colleague{public    Colleague1 (mediator mediator) {        Super (mediator);    }    public void Send (String message) {        mediator.send (message, this);    }        public void Notify (String message) {        System.out.println ("Colleague 1 gets the message:" +message);}    }
Package Com.roc.mediator;public class Colleague2 extends colleague{public    Colleague2 (mediator mediator) {        Super (mediator);    }    public void Send (String message) {        mediator.send (message, this);    }        public void Notify (String message) {        System.out.println ("Colleague 2 gets the message:" +message);}    }
Package com.roc.mediator;/** * Broker Mode * @author LIAOWP * */public class Client {public        static void Main (string[] args) {        concretemediator mediator=new concretemediator ();        Colleague1 colleague1=new Colleague1 (mediator);        Colleague2 colleague2=new Colleague2 (mediator);        Mediator.setcolleague1 (colleague1);        Mediator.setcolleague2 (colleague2);        Colleague1.send ("How are you doing?" ");        Colleague2.send ("Not bad");    }    }
The intermediary mode applicable scenario
    • A set of objects communicates in a well-defined but complex way. The resulting interdependence structure is confusing and difficult to understand.
    • An object that references many other objects and communicates directly with those objects makes it difficult to reuse the object.
    • You want to customize a behavior that is distributed across multiple classes without having to generate too many subclasses.
    • Typically applied to a set of objects in a well-defined but complex way to communicate.

The mediator pattern for 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.