Intermediary mode in Design Mode

Source: Internet
Author: User

Definition: a mediation object is used to encapsulate a series of object interactions. The intermediary makes the objects do not need to be explicitly referenced to each other, so that the coupling is loose and the interaction between them can be changed independently.

Structure:


Sample Code:

Public abstract class mediator {public abstract void send (string message, colleague);} public class concretemediator extends mediator {collect colleague1; Collect colleague2; Public concretecolleague1 getcolleague1 () {return colleague1 ;} public void setcolleague1 (concretecolleague1 colleague1) {This. colleague1 = colleague1;} public concretecolleague2 getcolleague2 () {return colleague2;} public void setcolleague2 (concretecolleague2 colleague2) {This. colleague2 = colleague2;} @ overridepublic void send (string message, colleague) {// todo auto-generated method stubif (colleague = colleague1) {colleague2.notify (Message );} else {colleague1.notify (Message) ;}} public abstract class colleague {protected mediator; Public colleague (mediator) {This. mediator = mediator ;}} public class concretecolleague1 extends colleague {public concretecolleague1 (mediator) {super (mediator); // todo auto-generated constructor stub} public void send (string message) {mediator. send (message, this);} public void receivy (string message) {system. out. println ("colleague 1 get message:" + message) ;}} public class concretecolleague2 extends colleague {public concretecolleague2 (mediator) {super (mediator ); // todo auto-generated constructor stub} public void send (string message) {mediator. send (message, this);} public void receivy (string message) {system. out. println ("colleague 2 get message:" + message );}}
The client code is as follows:

Public class client {public static void main (string [] ARGs) {concretemediator M = new concretemediator (); concretecolleague1 C1 = new concretecolleague1 (m ); concretecolleague2 C2 = new concretecolleague2 (m); M. colleague1 = C1; M. colleague2 = c2; c1.send ("have you eaten? "); C2.send (" no, are you going to treat me? ");}}

Output result:

Colleague 2 got the message: Have you ever eaten?

Colleague 1 received a message: No. Are you planning to treat me?


Intermediary mode in Design Mode

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.