Java Design Pattern _ Broker Mode

Source: Internet
Author: User

/** *  @Title: country.java *  @Package  com.wangbiao.design.mediator * @ description: todo  *  @author  wangbiao    *  @date   2014-9-24  pm 12:02:02  *  @version  V1.0 */package  com.wangbiao.design.mediator;/** *  @Title: country.java *  @Package   com.wangbiao.design.mediator *  @Description: todo  *  @author  wangbiao     *  @date  2014-9-24  afternoon 12:02:02  *  @version  v1.0 */ Public abstract class country {    public unitednationsmediator  mediator;        public country ()  {         // TODO Auto-generated constructor stub     }        public  Country (Unitednationsmediator mediator)  {         this.mediator = mediator;    }         Public abstract void declare (string message);     public abstract  void getmessage (string message);    }class china extends  Country{        public china (Unitednationsmediator mediator)  {        super (mediator);    }          @Override     public void getmessage (String  message)  {        SYSTEM.OUT.PRINTLN ("China receives message  : " + Message);    }     @Override     public void  declare (String message)  {        mediator.declare (message, this);     }    }class USA extends Country{         public usa (Unitednationsmediator mediator)  {         super (mediator);    }     @Override      public void getmessage (string message)  {         SYSTEM.OUT.PRINTLN ("The United States received the message  : " +message);     }    @ Override    public void declare (string message)  {         mediator.declare (message, this);    }     }package com.wangbiao.design.mediator;/** *  *  @Title:  unitednationsmediator.java *  @Package com.wangbiao.design.mediator *  @Description: todo  *  @author   wangbiao    *  @date  2014-9-24  pm 01:57:02  *  @version   V1.0 */public abstract class unitednationsmediator {    public  abstract void declare (string message,country country);     }class  UnitedNationSecurityCouncil extends UnitedNationsMediator{         private China china;    private USA usa;         public china getchina ()  {         return china;    }    public void setchina ( China china)  {        this.china = china;     } &nbsP;  public usa getusa ()  {        return  Usa;    }    public void setusa (USA usa)  {         this.usa = usa;    }      @Override     public void declare (string message, country  Country)  {                 if  (Country instanceof china)  {             usa.getmessage (message);         }else {             china.getmessage (message);         }            } }/** *  @Title: &NBsp client.java *  @Package  com.wangbiao.design.mediator *  @Description: todo   *  @author  wangbiao    *  @date  2014-9-24  pm 01:56:55   *  @version  V1.0 */package com.wangbiao.design.mediator;/** *  @Title:  client.java *  @Package  com.wangbiao.design.mediator *  @Description: todo   *  @author  wangbiao    *  @date  2014-9-24  pm 01:56:55   *  @version  V1.0 */public class Client {    /**      *  @param  args     */    public  static void main (String[] args)  {                 unitednationsecuritycouncil mediator = new  unitednationsecuritycOuncil ();                 china  country1 = new china (mediator);        usa  Country2 = new usa (mediator);                 mediator.setchina (Country1);         Mediator.setusa (Country2);                 country2.declare ("Why do you have so much spending every year?");         country1.declare ("Your fart");     }}


Java Design Pattern _ Broker Mode

Related Article

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.