1. 仲裁者模式
1.1 簡介
??仲裁者模式又稱為調(diào)停者模式或中介者模式薯鳍,主要是仲裁和中介的作用咖气,幫助其它類之間進行通信,降低多個對象和類之間的通信復(fù)雜性挖滤。主要包括仲裁者(Mediator)和組員(Colleague)崩溪,組員的動作需要會向仲裁者匯報,仲裁者會根據(jù)全局的實際情況向其他Colleague作出指示斩松,共同完成一定的邏輯功能伶唯。仲裁者通常處理不同類之間的通信,并支持松耦合惧盹,使代碼易于維護乳幸。作為行為型的模式之一,應(yīng)用場景有限并不被大家熟知钧椰。
1.2 仲裁者結(jié)構(gòu)
仲裁者uml:
仲裁者uml.jpg
仲裁者成員:
- 抽象仲裁者(mediator):定義一個接口用于和對象通信(SmartDevice)
- 具體仲裁者(concretemediator):協(xié)調(diào)各同事對象實現(xiàn)協(xié)作粹断,了解維護各個同事()
- 抽象同事角色(colleague):規(guī)定了同事的基本類型
- 具體同事角色(concreteColleague):每個同事都知道仲裁者對象,要與同事通信則把通信告訴仲裁者
2. 示例
??我們以房屋出租為例演侯,房東通過中介將房子租給租客姿染,直接從中介處拿到租金,租客通過中介找到房東進行維修,同時物業(yè)通過中介向房東收取物業(yè)費悬赏。在此例中狡汉,中介相當(dāng)于仲裁者或者中介者,房東闽颇、租客和物業(yè)就是組員盾戴,組員之間通過中介進行通信,實現(xiàn)收租兵多、維修尖啡、收物業(yè)費等動作。
仲裁者接口類:
public interface Mediator {
void collectRents(int money);
void repair();
void collectPropertyFee(int money);
}
房屋中介:
public class HouseMediator implements Mediator {
private Person houseRenter;
private Person renter;
private Person property;
public HouseMediator(Person houseRenter, Person renter,
Person property) {
this.houseRenter = houseRenter;
this.renter = renter;
this.property = property;
}
@Override
public void collectRents(int money) {
}
public void repair() {
}
@Override
public void collectPropertyFee(int money) {
}
public Person getHouseRenter() {
return houseRenter;
}
public void setHouseRenter(Person houseRenter) {
this.houseRenter = houseRenter;
}
public Person getRenter() {
return renter;
}
public void setRenter(Person renter) {
this.renter = renter;
}
public Person getProperty() {
return property;
}
public void setProperty(Person property) {
this.property = property;
}
}
組員虛擬類:
public abstract class Person {
private Mediator houseMediator;
public abstract void action();
public Mediator getHouseMediator() {
return houseMediator;
}
public void setHouseMediator(Mediator houseMediator) {
this.houseMediator = houseMediator;
}
}
房東:
public class HouseOwner extends Person {
@Override
public void action() {
this.getHouseMediator().collectRents(1000);
}
}
租客:
public class Renter extends Person {
@Override
public void action() {
this.getHouseMediator().repair();
}
}
物業(yè):
public class Property extends Person {
@Override
public void action() {
this.getHouseMediator().collectPropertyFee(300);
}
}
客戶端調(diào)用:
public static void main(String[] args) {
Person houseRenter = new HouseOwner();
Person renter = new Renter();
Person property = new Property();
Mediator mediator = new HouseMediator(houseRenter, renter, property);
houseRenter.setHouseMediator(mediator);
renter.setHouseMediator(mediator);
property.setHouseMediator(mediator);
houseRenter.action();
renter.action();
property.action();
}
3. 總結(jié)
仲裁者模式的優(yōu)點:
- 降低了類的復(fù)雜度剩膘,將一對多轉(zhuǎn)化成了一對一衅斩。
- 各個類之間的解耦,減少對象之間的關(guān)聯(lián)性怠褐,讓每一個對象都能夠獨立
- 符合迪米特原則畏梆。
- 不會引入太多其他的系統(tǒng)
- 系統(tǒng)被依賴的程度降低
仲裁者模式的缺點:
- 中介者會龐大,變得復(fù)雜難以維護奈懒。
- 如果游戲中有大量的功能模塊需要中介者奠涌,那么擔(dān)任中介者的角色類將會因為擔(dān)任大量的中介者角色而容易產(chǎn)生操作接口爆炸的情況。為避免這種情況磷杏,我們可以搭配其他設(shè)計模式使用溜畅。
仲裁者模式的使用場景:
- 系統(tǒng)中對象之間存在比較復(fù)雜的引用關(guān)系,導(dǎo)致它們之間的依賴關(guān)系結(jié)構(gòu)混亂而且難以復(fù)用該對象极祸。
- 想通過一個中間類來封裝多個類中的行為慈格,而又不想生成太多的子類。
- 機場調(diào)度系統(tǒng)贿肩。
- MVC 框架峦椰,其中C(控制器)就是 M(模型)和 V(視圖)的中介者龄寞。
- 數(shù)據(jù)庫引擎:內(nèi)部可以分成數(shù)個子系統(tǒng),有專門負(fù)責(zé)數(shù)據(jù)庫連接地功能與產(chǎn)生數(shù)據(jù)庫操作語句地功能,兩個子功能之間地溝通可以通過中介者模式(Mediator)來進行,讓兩者之間不相互依賴,方便抽換另一個子系統(tǒng)
- 游戲內(nèi)各系統(tǒng)的整合