[TOC] csdn連接:smilenicky.blog.csdn.net/article/det…java
中介者模式(Mediator Pattern):中介者模式就是用一箇中介對象來封裝一系列的對象的交互,使各對象之間不須要顯式地相互做用,下降對象之間的耦合度,中介者是一種對象行爲型模式。設計模式
因此中介者模式適用於對象之間存在大量的關聯的狀況,假如一個對象改變了,咱們就須要跟蹤其關聯對象,作出對於調整,耦合度是很大的,因此就能夠用中介者模式來下降耦合度。bash
中介者模式包括以下角色:ui
中介者模式起到中轉的做用,當同事類須要調用時,調用中介者就好,不須要調用同事類,中介者模式將同事對象之間的關係行爲進行封裝,起到了協調的做用this
中介者模式優勢:spa
中介者模式缺點:.net
抽象中介者類:設計
public abstract class Mediator
{
protected ArrayList colleagues;
public void register(Colleague colleague)
{
colleagues.add(colleague);
}
public abstract void operation();
}
複製代碼
具體中介者類:code
public class ConcreteMediator extends Mediator
{
public void operation()
{
......
((Colleague)(colleagues.get(0))).method1();
......
}
}
複製代碼
抽象同事類:對象
public abstract class Colleague
{
protected Mediator mediator;
public Colleague(Mediator mediator)
{
this.mediator=mediator;
}
public abstract void method1();
public abstract void method2();
}
複製代碼
具體同事類:
public class ConcreteColleague extends Colleague
{
public ConcreteColleague(Mediator mediator)
{
super(mediator);
}
public void method1()
{
......
}
public void method2()
{
mediator.operation1();
}
}
複製代碼
例子來自:《設計模式》一書
實例:虛擬聊天室 某論壇系統欲增長一個虛擬聊天室,容許論壇會員經過該聊天室進行信息交流,普通會員(CommonMember)能夠給其餘會員發送文本信息,鑽石會員(DiamondMember)既能夠給其餘會員發送文本信息,還能夠發送圖片信息。該聊天室能夠對不雅字符進行過濾,還能夠對發送的圖片大小進行控制。用中介者模式設計該虛擬聊天室。
抽象同事類 定義一個Member類,屬於抽象同事類:
public abstract class Member
{
protected AbstractChatroom chatroom;
protected String name;
public Member(String name)
{
this.name=name;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name=name;
}
public AbstractChatroom getChatroom()
{
return chatroom;
}
public void setChatroom(AbstractChatroom chatroom)
{
this.chatroom=chatroom;
}
public abstract void sendText(String to,String message);
public abstract void sendImage(String to,String image);
public void receiveText(String from,String message)
{
System.out.println(from + "發送文本給" + this.name + ",內容爲:" + message);
}
public void receiveImage(String from,String image)
{
System.out.println(from + "發送圖片給" + this.name + ",內容爲:" + image);
}
}
複製代碼
具體同事類 具體同事類,繼承抽象同事類Member:
普通會員
public class CommonMember extends Member
{
public CommonMember(String name)
{
super(name);
}
public void sendText(String to,String message)
{
System.out.println("普通會員發送信息:");
chatroom.sendText(name,to,message); //發送
}
public void sendImage(String to,String image)
{
System.out.println("普通會員不能發送圖片!");
}
}
複製代碼
磚石會員
public class DiamondMember extends Member
{
public DiamondMember(String name)
{
super(name);
}
public void sendText(String to,String message)
{
System.out.println("鑽石會員發送信息:");
chatroom.sendText(name,to,message); //發送
}
public void sendImage(String to,String image)
{
System.out.println("鑽石會員發送圖片:");
chatroom.sendImage(name,to,image); //發送
}
}
複製代碼
抽象中介者類
抽象的中介者類,定義聊天室具體有功能方法
public abstract class AbstractChatroom
{
public abstract void register(Member member);
public abstract void sendText(String from,String to,String message);
public abstract void sendImage(String from,String to,String message);
}
複製代碼
具體中介者類
聊天室功能實現,不須要同事類之間相互調用
import java.util.*;
public class ChatGroup extends AbstractChatroom
{
private Hashtable members=new Hashtable();
public void register(Member member)
{
if(!members.contains(member))
{
members.put(member.getName(),member);
member.setChatroom(this);
}
}
public void sendText(String from,String to,String message)
{
Member member=(Member)members.get(to);
String newMessage=message;
newMessage=message.replaceAll("不雅字符","*");
member.receiveText(from,newMessage);
}
public void sendImage(String from,String to,String image)
{
Member member=(Member)members.get(to);
//模擬圖片大小判斷
if(image.length()>5)
{
System.out.println("圖片太大,發送失敗!");
}
else
{
member.receiveImage(from,image);
}
}
}
複製代碼