中介者模式

canca17年前 (2007-03-26)设计模式327
//中介者模式
//CopyRight(C) CAnca software Office.2006
//Created by CAnca.

public class MediatorMode{
 public static void main(String[] args){
  Mediator mediator = new ConcreteMediator();
  Colleague coleague1 = new ConcreteColleagueA(mediator);
  Colleague coleague2 = new ConcreteColleagueB(mediator);
  mediator.setColleagueA(coleague1);
  mediator.setColleagueB(coleague2);
  coleague1.send("Hello.How are you!");
  coleague2.send("I'm fine.Thank you!");
 }
}

//Mediator
abstract class Mediator{
 abstract void send(String message,Colleague colleague);
 abstract void setColleagueA(Colleague colleague);
 abstract void setColleagueB(Colleague colleague);
}

class ConcreteMediator extends Mediator{
 private Colleague colleagueA,colleagueB;
 
 public void setColleagueA(Colleague colleague){
  this.colleagueA = colleague;
 } 
 
 public void setColleagueB(Colleague colleague){
  this.colleagueB = colleague;
 }
 
 public void send(String message,Colleague colleague){
  if(colleague == colleagueA){
   colleagueB.notify(message);
  }else{
   colleagueA.notify(message);
  }
 }
}

abstract class Colleague{
 protected Mediator mediator;
 public Colleague(Mediator mediator){
  this.mediator = mediator;
 }
 
 abstract void send(String str);
 abstract void notify(String str);
}

class ConcreteColleagueA extends Colleague{
 public ConcreteColleagueA(Mediator mediator){
  super(mediator);
 }
 
 public void send(String str){
  this.mediator.send(str,this);
 }
 
 public void notify(String str){
  System.out.println("ConcreteColleagueA.send() - " + str);
 }
}

class ConcreteColleagueB extends Colleague{
 public ConcreteColleagueB(Mediator mediator){
  super(mediator);
 }
 public void send(String str){
  this.mediator.send(str,this);
 }
 public void notify(String str){
  System.out.println("ConcreteColleagueB.send() - " + str);
 }
}

//中介者模式--现实例子
//CopyRight(C) CAnca software Office.2006
//Created by CAnca.
import java.util.*;
   
  // MainApp test application  
 
public class MediatorMode_RealWorld{ 
 public static void main(String[] args){ 
      // Create chatroom  
      Chatroom chatroom = new Chatroom(); 
      // Create participants and register them  
      Participant George = new Beatle("George"); 
      Participant Paul = new Beatle("Paul"); 
      Participant Ringo = new Beatle("Ringo"); 
      Participant John = new Beatle("John") ; 
      Participant Yoko = new NonBeatle("Yoko"); 
 
      chatroom.Register(George); 
      chatroom.Register(Paul); 
      chatroom.Register(Ringo); 
      chatroom.Register(John); 
      chatroom.Register(Yoko); 
 
      // Chatting participants  
      Yoko.Send ("John", "Hi John!"); 
      Paul.Send ("Ringo", "All you need is love"); 
      Ringo.Send("George", "My sweet Lord"); 
      Paul.Send ("John", "Can't buy me love"); 
      John.Send ("Yoko", "My sweet love"); 
    } 
  } 
 
  // "Mediator"  
 
  abstract class AbstractChatroom 
  { 
    abstract void Register(Participant participant); 
    abstract void Send(String from, String to, String message); 
  } 
 
  // "ConcreteMediator"  
 
  class Chatroom extends AbstractChatroom 
  { 
    private Hashtable participants = new Hashtable(); 
 
    public void Register(Participant participant) 
    { 
      if (participants.get(participant.getName()) == null) 
      {
       participants.put(participant.getName(),participant);
      } 
 
      participant.setChatroom(this); 
    } 
 
    public void Send(String from, String to, String message)
    { 
      Participant pto = (Participant)participants.get(to); 
      if (pto != null) 
      { 
        pto.Receive(from, message); 
      } 
    } 
  } 
 
  // "AbstractColleague"  
 
  class Participant 
  { 
    private Chatroom chatroom; 
    private String name; 
 
    // Constructor  
    public Participant(String name) 
    { 
      this.name = name; 
    } 
 
    // Properties  
    public String getName()
    { 
      return name; 
    } 
 
    public Chatroom getChatroom()
    {  
      return chatroom;
    }
    
    public void setChatroom(Chatroom chatroom){
     this.chatroom = chatroom;
    }
 
    public void Send(String to, String message)
    { 
      chatroom.Send(name, to, message); 
    } 
 
    public void Receive(String from, String message) 
    { 
      System.out.println(from + " to " + getName() + " : '" + message + "'"); 
    } 
  } 
 
  //" ConcreteColleague1"  
 
  class Beatle extends Participant 
  { 
    // Constructor  
    public Beatle(String name)
    { 
     super(name);
    } 
 
    public void Receive(String from, String message) 
    { 
      System.out.print("To a Beatle: "); 
      super.Receive(from, message);
    } 
  } 
 
  //" ConcreteColleague2"  
 
  class NonBeatle extends Participant 
  { 
    // Constructor  
    public NonBeatle(String name)
    {
     super(name);
    } 
 
    public void Receive(String from, String message) 
    { 
      System.out.print("To a non-Beatle: "); 
      super.Receive(from, message);
    } 
  }


相关文章

抽象工厂模式

//抽象工厂模式//Copyright CAnca Software office//Created by CAnca.2006public class AbstractFactory{ p...

单例模式

//CopyRight CAnca Software Office. 2006//Created by CAnca. import java.util.*; public class singleto...

工厂方法模式

//工厂方法模式:现实例子//CopyRight(C)CAnca Software Office. 2006//Created by CAnca. import java.util.*; public...

原型模式

//原型模式//CopyRight(C) CAnca Software Office.2006//Created by CAnca. public class PrototypeMode{ ...

代理模式

//代理模式--真实例子//CopyRight(C)CAnca Software Office.2006//Created by CAnca.   public class ProxyMod...

适配器模式

//适配器模式//CopyRight(C)CAnca Software Office.//Created by CAnca. public class AdapterMode{ public...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。