設(shè)計(jì)模式系列—中介者模式
模式定義
定義一個(gè)中介對(duì)象來封裝一系列對(duì)象之間的交互,使原有對(duì)象之間的耦合松散,且可以獨(dú)立地改變它們之間的交互。中介者模式又叫調(diào)停模式,它是迪米特法則的典型應(yīng)用。
- 迪米特法則(Law of Demeter,LoD)又叫作最少知識(shí)原則(Least Knowledge Principle,LKP),產(chǎn)生于 1987 年美國東北大學(xué)(Northeastern University)的一個(gè)名為迪米特(Demeter)的研究項(xiàng)目,由伊恩·荷蘭(Ian Holland)提出,被 UML 創(chuàng)始者之一的布奇(Booch)普及,后來又因?yàn)樵诮?jīng)典著作《程序員修煉之道》(The Pragmatic Programmer)提及而廣為人知。
- 迪米特法則的定義是:只與你的直接朋友交談,不跟“陌生人”說話(Talk only to your immediate friends and not to strangers)。其含義是:如果兩個(gè)軟件實(shí)體無須直接通信,那么就不應(yīng)當(dāng)發(fā)生直接的相互調(diào)用,可以通過第三方轉(zhuǎn)發(fā)該調(diào)用。其目的是降低類之間的耦合度,提高模塊的相對(duì)獨(dú)立性。
- 迪米特法則中的“朋友”是指:當(dāng)前對(duì)象本身、當(dāng)前對(duì)象的成員對(duì)象、當(dāng)前對(duì)象所創(chuàng)建的對(duì)象、當(dāng)前對(duì)象的方法參數(shù)等,這些對(duì)象同當(dāng)前對(duì)象存在關(guān)聯(lián)、聚合或組合關(guān)系,可以直接訪問這些對(duì)象的方法。
模板實(shí)現(xiàn)如下:
- package com.niuh.designpattern.mediator.v1;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * <p>
- * 中介者模式
- * </p>
- */
- public class MediatorPattern {
- public static void main(String[] args) {
- Mediator md = new ConcreteMediator();
- Colleague c1, c2;
- c1 = new ConcreteColleague1();
- c2 = new ConcreteColleague2();
- md.register(c1);
- md.register(c2);
- c1.send();
- System.out.println("==============");
- c2.send();
- }
- }
- //抽象中介者
- abstract class Mediator {
- public abstract void register(Colleague colleague);
- public abstract void relay(Colleague cl); //轉(zhuǎn)發(fā)
- }
- //具體中介者
- class ConcreteMediator extends Mediator {
- private List<Colleague> colleagues = new ArrayList<Colleague>();
- public void register(Colleague colleague) {
- if (!colleagues.contains(colleague)) {
- colleagues.add(colleague);
- colleague.setMedium(this);
- }
- }
- public void relay(Colleague cl) {
- for (Colleague ob : colleagues) {
- if (!ob.equals(cl)) {
- ((Colleague) ob).receive();
- }
- }
- }
- }
- //抽象同事類
- abstract class Colleague {
- protected Mediator mediator;
- public void setMedium(Mediator mediator) {
- this.mediator = mediator;
- }
- public abstract void receive();
- public abstract void send();
- }
- //具體同事類
- class ConcreteColleague1 extends Colleague {
- public void receive() {
- System.out.println("具體同事類1收到請(qǐng)求。");
- }
- public void send() {
- System.out.println("具體同事類1發(fā)出請(qǐng)求。");
- mediator.relay(this); //請(qǐng)中介者轉(zhuǎn)發(fā)
- }
- }
- //具體同事類
- class ConcreteColleague2 extends Colleague {
- public void receive() {
- System.out.println("具體同事類2收到請(qǐng)求。");
- }
- public void send() {
- System.out.println("具體同事類2發(fā)出請(qǐng)求。");
- mediator.relay(this); //請(qǐng)中介者轉(zhuǎn)發(fā)
- }
- }
結(jié)果實(shí)現(xiàn)如下:
- 具體同事類1發(fā)出請(qǐng)求。
- 具體同事類2收到請(qǐng)求。
- 具體同事類2發(fā)出請(qǐng)求。
- 具體同事類1收到請(qǐng)求。
解決的問題
對(duì)象與對(duì)象之間存在大量的關(guān)聯(lián)關(guān)系,這樣勢(shì)必會(huì)導(dǎo)致系統(tǒng)的結(jié)構(gòu)變得很復(fù)雜,同時(shí)若一個(gè)對(duì)象發(fā)生改變,我們也需要跟蹤與之相關(guān)聯(lián)的對(duì)象,同時(shí)做出相應(yīng)的處理。
模式組成
中介者模式實(shí)現(xiàn)的關(guān)鍵是找出“中介者”。
實(shí)例說明
實(shí)例概況
用中介者模式編寫一個(gè)“北京房地產(chǎn)交流平臺(tái)”程序。
分析:北京房地產(chǎn)交流平臺(tái)是“房地產(chǎn)中介公司”提供給“賣方客戶”與“買方客戶”進(jìn)行信息交流的平臺(tái),比較適合用中介者模式來實(shí)現(xiàn)。
使用步驟
步驟1:定義一個(gè)中介公司(Medium)接口,它是抽象中介者,它包含了客戶注冊(cè)方法 register(Customer member) 和信息轉(zhuǎn)發(fā)方法 relay(String from,String ad);
- interface Medium {
- //客戶注冊(cè)
- void register(Customer member);
- //轉(zhuǎn)發(fā)
- void relay(String from, String ad);
- }
步驟2:定義一個(gè)北京房地產(chǎn)中介(EstateMedium)公司,它是具體中介者類,它包含了保存客戶信息的 List 對(duì)象,并實(shí)現(xiàn)了中介公司中的抽象方法。
- //具體中介者:房地產(chǎn)中介
- class EstateMedium implements Medium {
- private List<Customer> members = new ArrayList<Customer>();
- public void register(Customer member) {
- if (!members.contains(member)) {
- members.add(member);
- member.setMedium(this);
- }
- }
- public void relay(String from, String ad) {
- for (Customer ob : members) {
- String name = ob.getName();
- if (!name.equals(from)) {
- ((Customer) ob).receive(from, ad);
- }
- }
- }
- }
步驟3:定義一個(gè)客戶(Qistomer)類,它是抽象同事類,其中包含了中介者的對(duì)象,和發(fā)送信息的 send(String ad) 方法與接收信息的 receive(String from,Stringad) 方法的接口,由于本程序是窗體程序,所以本類繼承 JPmme 類,并實(shí)現(xiàn)動(dòng)作事件的處理方法 actionPerformed(ActionEvent e)。
- //抽象同事類:客戶
- abstract class Customer extends JFrame implements ActionListener {
- private static final long serialVersionUID = -7219939540794786080L;
- protected Medium medium;
- protected String name;
- JTextField SentText;
- JTextArea ReceiveArea;
- public Customer(String name) {
- super(name);
- this.name = name;
- }
- void ClientWindow(int x, int y) {
- Container cp;
- JScrollPane sp;
- JPanel p1, p2;
- cp = this.getContentPane();
- SentText = new JTextField(18);
- ReceiveArea = new JTextArea(10, 18);
- ReceiveArea.setEditable(false);
- p1 = new JPanel();
- p1.setBorder(BorderFactory.createTitledBorder("接收內(nèi)容:"));
- p1.add(ReceiveArea);
- sp = new JScrollPane(p1);
- cp.add(sp, BorderLayout.NORTH);
- p2 = new JPanel();
- p2.setBorder(BorderFactory.createTitledBorder("發(fā)送內(nèi)容:"));
- p2.add(SentText);
- cp.add(p2, BorderLayout.SOUTH);
- SentText.addActionListener(this);
- this.setLocation(x, y);
- this.setSize(250, 330);
- this.setResizable(false); //窗口大小不可調(diào)整
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setVisible(true);
- }
- public void actionPerformed(ActionEvent e) {
- String tempInfo = SentText.getText().trim();
- SentText.setText("");
- this.send(tempInfo);
- }
- public String getName() {
- return name;
- }
- public void setMedium(Medium medium) {
- this.medium = medium;
- }
- public abstract void send(String ad);
- public abstract void receive(String from, String ad);
- }
步驟4:定義賣方(Seller)類和買方(Buyer)類,它們是具體同事類,是客戶(Customer)類的子類,它們實(shí)現(xiàn)了父類中的抽象方法,通過中介者類進(jìn)行信息交流。
- //具體同事類:賣方
- class Seller extends Customer {
- private static final long serialVersionUID = -1443076716629516027L;
- public Seller(String name) {
- super(name);
- ClientWindow(50, 100);
- }
- public void send(String ad) {
- ReceiveArea.append("我(賣方)說: " + ad + "\n");
- //使?jié)L動(dòng)條滾動(dòng)到最底端
- ReceiveArea.setCaretPosition(ReceiveArea.getText().length());
- medium.relay(name, ad);
- }
- public void receive(String from, String ad) {
- ReceiveArea.append(from + "說: " + ad + "\n");
- //使?jié)L動(dòng)條滾動(dòng)到最底端
- ReceiveArea.setCaretPosition(ReceiveArea.getText().length());
- }
- }
- //具體同事類:買方
- class Buyer extends Customer {
- private static final long serialVersionUID = -474879276076308825L;
- public Buyer(String name) {
- super(name);
- ClientWindow(350, 100);
- }
- public void send(String ad) {
- ReceiveArea.append("我(買方)說: " + ad + "\n");
- //使?jié)L動(dòng)條滾動(dòng)到最底端
- ReceiveArea.setCaretPosition(ReceiveArea.getText().length());
- medium.relay(name, ad);
- }
- public void receive(String from, String ad) {
- ReceiveArea.append(from + "說: " + ad + "\n");
- //使?jié)L動(dòng)條滾動(dòng)到最底端
- ReceiveArea.setCaretPosition(ReceiveArea.getText().length());
- }
- }
輸出結(jié)果
優(yōu)點(diǎn)
- 降低了對(duì)象之間的耦合性,使得對(duì)象易于獨(dú)立地被復(fù)用。
- 將對(duì)象間的一對(duì)多關(guān)聯(lián)轉(zhuǎn)變?yōu)橐粚?duì)一的關(guān)聯(lián),提高系統(tǒng)的靈活性,使得系統(tǒng)易于維護(hù)和擴(kuò)展。
缺點(diǎn)
當(dāng)同事類太多時(shí),中介者的職責(zé)將很大,它會(huì)變得復(fù)雜而龐大,以至于系統(tǒng)難以維護(hù)。
應(yīng)用場景
- 當(dāng)對(duì)象之間存在復(fù)雜的網(wǎng)狀結(jié)構(gòu)關(guān)系而導(dǎo)致依賴關(guān)系混亂且難以復(fù)用時(shí)。
- 當(dāng)想創(chuàng)建一個(gè)運(yùn)行于多個(gè)類之間的對(duì)象,又不想生成新的子類時(shí)。
模式的擴(kuò)展
在實(shí)際開發(fā)中,通常采用以下兩種方法來簡化中介者模式,使開發(fā)變得更簡單。
- 不定義中介者接口,把具體中介者對(duì)象實(shí)現(xiàn)成為單例。
- 同事對(duì)象不持有中介者,而是在需要的時(shí)候直接獲取中介者對(duì)象并調(diào)用。
程序代碼如下:
- package com.niuh.designpattern.mediator.v3;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * <p>
- * 簡化中介者模式
- * </p>
- */
- public class SimpleMediatorPattern {
- public static void main(String[] args) {
- SimpleColleague c1, c2;
- c1 = new SimpleConcreteColleague1();
- c2 = new SimpleConcreteColleague2();
- c1.send();
- System.out.println("==============");
- c2.send();
- }
- }
- //簡單單例中介者
- class SimpleMediator {
- private static SimpleMediator smd = new SimpleMediator();
- private List<SimpleColleague> colleagues = new ArrayList<SimpleColleague>();
- private SimpleMediator() {
- }
- public static SimpleMediator getMedium() {
- return (smd);
- }
- public void register(SimpleColleague colleague) {
- if (!colleagues.contains(colleague)) {
- colleagues.add(colleague);
- }
- }
- public void relay(SimpleColleague scl) {
- for (SimpleColleague ob : colleagues) {
- if (!ob.equals(scl)) {
- ((SimpleColleague) ob).receive();
- }
- }
- }
- }
- //抽象同事類
- interface SimpleColleague {
- void receive();
- void send();
- }
- //具體同事類
- class SimpleConcreteColleague1 implements SimpleColleague {
- SimpleConcreteColleague1() {
- SimpleMediator smd = SimpleMediator.getMedium();
- smd.register(this);
- }
- public void receive() {
- System.out.println("具體同事類1:收到請(qǐng)求。");
- }
- public void send() {
- SimpleMediator smd = SimpleMediator.getMedium();
- System.out.println("具體同事類1:發(fā)出請(qǐng)求...");
- smd.relay(this); //請(qǐng)中介者轉(zhuǎn)發(fā)
- }
- }
- //具體同事類
- class SimpleConcreteColleague2 implements SimpleColleague {
- SimpleConcreteColleague2() {
- SimpleMediator smd = SimpleMediator.getMedium();
- smd.register(this);
- }
- public void receive() {
- System.out.println("具體同事類2:收到請(qǐng)求。");
- }
- public void send() {
- SimpleMediator smd = SimpleMediator.getMedium();
- System.out.println("具體同事類2:發(fā)出請(qǐng)求...");
- smd.relay(this); //請(qǐng)中介者轉(zhuǎn)發(fā)
- }
- }
輸出結(jié)果如下:
- 具體同事類1:發(fā)出請(qǐng)求...
- 具體同事類2:收到請(qǐng)求。
- 具體同事類2:發(fā)出請(qǐng)求...
- 具體同事類1:收到請(qǐng)求。
源碼中的應(yīng)用
- java.util.Timer
- java.util.concurrent.Executer#execute()
- java.util.concurrent.ExecuterService#submit()
- java.lang.reflect.Method#invoke()
PS:以上代碼提交在 Github :
https://github.com/Niuh-Study/niuh-designpatterns.git