Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1743110
  • 博文数量: 297
  • 博客积分: 285
  • 博客等级: 二等列兵
  • 技术积分: 3006
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-06 22:04
个人简介

Linuxer, ex IBMer. GNU https://hmchzb19.github.io/

文章分类

全部博文(297)

文章存档

2020年(11)

2019年(15)

2018年(43)

2017年(79)

2016年(79)

2015年(58)

2014年(1)

2013年(8)

2012年(3)

分类: Java

2017-02-16 11:20:53

调解人 调停
The major participants of the Mediator pattern are:
Mediator: Defines an interface for communicating with Colleague objects.
ConcreteMediator: implements cooperative behavior by coordinating Colleague objects,
it also knows and maintains its colleagues.
Colleague Classes: Each Colleague class knows its Mediator objects, Each colleague communicates with its mediator whenever it would have otherwise communicated with another colleague.

Mediator Pattern in JDK:
java.util.Timer class schedule XXX() methods.
java Concurrency Executor execute() method.
java.lang.reflect.Method invoke() method.

代码如下

点击(此处)折叠或打开

  1. package mediatorpattern;

  2. /* Colleagure A wants to talk, and Colleague B to fight with mediator.
  3.  * when they do some action(i.e., doSomething()),
  4.  * they invoke mediator to do that.
  5.  */


  6. interface IMediator {
  7.     public void talk();
  8.     public void fight();
  9.     public void registerA(ColleagueA a);
  10.     public void registerB(ColleagueB b);
  11. }


  12. class MyConcreteMediator implements IMediator{
  13.     ColleagueA talk;
  14.     ColleagueB fight;

  15.     @Override
  16.     public void talk(){
  17.         System.out.println("Mediator is talking");
  18.         //let the talk colleague do some stuff
  19.     }

  20.     @Override
  21.     public void fight(){
  22.         System.out.println("Mediator is fighting");
  23.         //let the fight colleague do some stuff
  24.     }

  25.     @Override
  26.     public void registerA(ColleagueA a){
  27.         this.talk = a;
  28.     }

  29.     @Override
  30.     public void registerB(ColleagueB b){
  31.         this.fight = b;
  32.     }
  33. }

  34. abstract class Colleague{
  35.     IMediator mediator;
  36.     public abstract void doSomething();
  37. }

  38. class ColleagueA extends Colleague{
  39.     public ColleagueA(IMediator mediator){
  40.         this.mediator = mediator;
  41.         this.mediator.registerA(this);
  42.     }

  43.     @Override
  44.     public void doSomething(){
  45.         this.mediator.talk();
  46.     }
  47. }

  48. class ColleagueB extends Colleague{
  49.     public ColleagueB(IMediator mediator){
  50.         this.mediator = mediator;
  51.         this.mediator.registerB(this);
  52.     }

  53.     @Override
  54.     public void doSomething(){
  55.         this.mediator.fight();
  56.     }
  57. }

  58. public class MediatorPatternDemo{
  59.     public static void main(String[] args){
  60.         IMediator mediator = new MyConcreteMediator();

  61.         ColleagueA talkColleague = new ColleagueA(mediator);
  62.         ColleagueB fightColleague = new ColleagueB(mediator);

  63.         talkColleague.doSomething();
  64.         fightColleague.doSomething();
  65.     }
  66. }


阅读(536) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~