自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

設計模式系列—中介者模式

開發(fā) 前端
本篇和大家一起來學習中介者模式相關內(nèi)容。

模式定義

定義一個中介對象來封裝一系列對象之間的交互,使原有對象之間的耦合松散,且可以獨立地改變它們之間的交互。中介者模式又叫調(diào)停模式,它是迪米特法則的典型應用。

 

  • 迪米特法則(Law of Demeter,LoD)又叫作最少知識原則(Least Knowledge Principle,LKP),產(chǎn)生于 1987 年美國東北大學(Northeastern University)的一個名為迪米特(Demeter)的研究項目,由伊恩·荷蘭(Ian Holland)提出,被 UML 創(chuàng)始者之一的布奇(Booch)普及,后來又因為在經(jīng)典著作《程序員修煉之道》(The Pragmatic Programmer)提及而廣為人知。
  • 迪米特法則的定義是:只與你的直接朋友交談,不跟“陌生人”說話(Talk only to your immediate friends and not to strangers)。其含義是:如果兩個軟件實體無須直接通信,那么就不應當發(fā)生直接的相互調(diào)用,可以通過第三方轉(zhuǎn)發(fā)該調(diào)用。其目的是降低類之間的耦合度,提高模塊的相對獨立性。
  • 迪米特法則中的“朋友”是指:當前對象本身、當前對象的成員對象、當前對象所創(chuàng)建的對象、當前對象的方法參數(shù)等,這些對象同當前對象存在關聯(lián)、聚合或組合關系,可以直接訪問這些對象的方法。

模板實現(xiàn)如下:

  1. package com.niuh.designpattern.mediator.v1; 
  2.  
  3. import java.util.ArrayList; 
  4. import java.util.List; 
  5.  
  6. /** 
  7.  * <p> 
  8.  * 中介者模式 
  9.  * </p> 
  10.  */ 
  11. public class MediatorPattern { 
  12.  
  13.     public static void main(String[] args) { 
  14.         Mediator md = new ConcreteMediator(); 
  15.         Colleague c1, c2; 
  16.         c1 = new ConcreteColleague1(); 
  17.         c2 = new ConcreteColleague2(); 
  18.         md.register(c1); 
  19.         md.register(c2); 
  20.         c1.send(); 
  21.         System.out.println("=============="); 
  22.         c2.send(); 
  23.     } 
  24.  
  25. //抽象中介者 
  26. abstract class Mediator { 
  27.     public abstract void register(Colleague colleague); 
  28.  
  29.     public abstract void relay(Colleague cl); //轉(zhuǎn)發(fā) 
  30.  
  31. //具體中介者 
  32. class ConcreteMediator extends Mediator { 
  33.     private List<Colleague> colleagues = new ArrayList<Colleague>(); 
  34.  
  35.     public void register(Colleague colleague) { 
  36.         if (!colleagues.contains(colleague)) { 
  37.             colleagues.add(colleague); 
  38.             colleague.setMedium(this); 
  39.         } 
  40.     } 
  41.  
  42.     public void relay(Colleague cl) { 
  43.         for (Colleague ob : colleagues) { 
  44.             if (!ob.equals(cl)) { 
  45.                 ((Colleague) ob).receive(); 
  46.             } 
  47.         } 
  48.     } 
  49.  
  50. //抽象同事類 
  51. abstract class Colleague { 
  52.     protected Mediator mediator; 
  53.  
  54.     public void setMedium(Mediator mediator) { 
  55.         this.mediator = mediator; 
  56.     } 
  57.  
  58.     public abstract void receive(); 
  59.  
  60.     public abstract void send(); 
  61.  
  62. //具體同事類 
  63. class ConcreteColleague1 extends Colleague { 
  64.     public void receive() { 
  65.         System.out.println("具體同事類1收到請求。"); 
  66.     } 
  67.  
  68.     public void send() { 
  69.         System.out.println("具體同事類1發(fā)出請求。"); 
  70.         mediator.relay(this); //請中介者轉(zhuǎn)發(fā) 
  71.     } 
  72.  
  73. //具體同事類 
  74. class ConcreteColleague2 extends Colleague { 
  75.     public void receive() { 
  76.         System.out.println("具體同事類2收到請求。"); 
  77.     } 
  78.  
  79.     public void send() { 
  80.         System.out.println("具體同事類2發(fā)出請求。"); 
  81.         mediator.relay(this); //請中介者轉(zhuǎn)發(fā) 
  82.     } 

結(jié)果實現(xiàn)如下:

  • 具體同事類1發(fā)出請求。
  • 具體同事類2收到請求。
  • 具體同事類2發(fā)出請求。
  • 具體同事類1收到請求。

解決的問題

對象與對象之間存在大量的關聯(lián)關系,這樣勢必會導致系統(tǒng)的結(jié)構變得很復雜,同時若一個對象發(fā)生改變,我們也需要跟蹤與之相關聯(lián)的對象,同時做出相應的處理。

模式組成

 

中介者模式實現(xiàn)的關鍵是找出“中介者”。

實例說明

實例概況

用中介者模式編寫一個“北京房地產(chǎn)交流平臺”程序。

 

分析:北京房地產(chǎn)交流平臺是“房地產(chǎn)中介公司”提供給“賣方客戶”與“買方客戶”進行信息交流的平臺,比較適合用中介者模式來實現(xiàn)。

使用步驟

 

步驟1:定義一個中介公司(Medium)接口,它是抽象中介者,它包含了客戶注冊方法 register(Customer member) 和信息轉(zhuǎn)發(fā)方法 relay(String from,String ad);

  1. interface Medium { 
  2.     //客戶注冊 
  3.     void register(Customer member); 
  4.  
  5.     //轉(zhuǎn)發(fā) 
  6.     void relay(String from, String ad); 

步驟2:定義一個北京房地產(chǎn)中介(EstateMedium)公司,它是具體中介者類,它包含了保存客戶信息的 List 對象,并實現(xiàn)了中介公司中的抽象方法。

  1. //具體中介者:房地產(chǎn)中介 
  2. class EstateMedium implements Medium { 
  3.     private List<Customer> members = new ArrayList<Customer>(); 
  4.  
  5.     public void register(Customer member) { 
  6.         if (!members.contains(member)) { 
  7.             members.add(member); 
  8.             member.setMedium(this); 
  9.         } 
  10.     } 
  11.  
  12.     public void relay(String from, String ad) { 
  13.         for (Customer ob : members) { 
  14.             String name = ob.getName(); 
  15.             if (!name.equals(from)) { 
  16.                 ((Customer) ob).receive(from, ad); 
  17.             } 
  18.         } 
  19.     } 

步驟3:定義一個客戶(Qistomer)類,它是抽象同事類,其中包含了中介者的對象,和發(fā)送信息的 send(String ad) 方法與接收信息的 receive(String from,Stringad) 方法的接口,由于本程序是窗體程序,所以本類繼承 JPmme 類,并實現(xiàn)動作事件的處理方法 actionPerformed(ActionEvent e)。

  1. //抽象同事類:客戶 
  2. abstract class Customer extends JFrame implements ActionListener { 
  3.     private static final long serialVersionUID = -7219939540794786080L; 
  4.     protected Medium medium; 
  5.     protected String name
  6.     JTextField SentText; 
  7.     JTextArea ReceiveArea; 
  8.  
  9.     public Customer(String name) { 
  10.         super(name); 
  11.         this.name = name
  12.     } 
  13.  
  14.     void ClientWindow(int x, int y) { 
  15.         Container cp; 
  16.         JScrollPane sp; 
  17.         JPanel p1, p2; 
  18.         cp = this.getContentPane(); 
  19.         SentText = new JTextField(18); 
  20.         ReceiveArea = new JTextArea(10, 18); 
  21.         ReceiveArea.setEditable(false); 
  22.         p1 = new JPanel(); 
  23.         p1.setBorder(BorderFactory.createTitledBorder("接收內(nèi)容:")); 
  24.         p1.add(ReceiveArea); 
  25.         sp = new JScrollPane(p1); 
  26.         cp.add(sp, BorderLayout.NORTH); 
  27.         p2 = new JPanel(); 
  28.         p2.setBorder(BorderFactory.createTitledBorder("發(fā)送內(nèi)容:")); 
  29.         p2.add(SentText); 
  30.         cp.add(p2, BorderLayout.SOUTH); 
  31.         SentText.addActionListener(this); 
  32.         this.setLocation(x, y); 
  33.         this.setSize(250, 330); 
  34.         this.setResizable(false); //窗口大小不可調(diào)整 
  35.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
  36.         this.setVisible(true); 
  37.     } 
  38.  
  39.     public void actionPerformed(ActionEvent e) { 
  40.         String tempInfo = SentText.getText().trim(); 
  41.         SentText.setText(""); 
  42.         this.send(tempInfo); 
  43.     } 
  44.  
  45.     public String getName() { 
  46.         return name
  47.     } 
  48.  
  49.     public void setMedium(Medium medium) { 
  50.         this.medium = medium; 
  51.     } 
  52.  
  53.     public abstract void send(String ad); 
  54.  
  55.     public abstract void receive(String from, String ad); 

步驟4:定義賣方(Seller)類和買方(Buyer)類,它們是具體同事類,是客戶(Customer)類的子類,它們實現(xiàn)了父類中的抽象方法,通過中介者類進行信息交流。

  1. //具體同事類:賣方 
  2. class Seller extends Customer { 
  3.     private static final long serialVersionUID = -1443076716629516027L; 
  4.  
  5.     public Seller(String name) { 
  6.         super(name); 
  7.         ClientWindow(50, 100); 
  8.     } 
  9.  
  10.     public void send(String ad) { 
  11.         ReceiveArea.append("我(賣方)說: " + ad + "\n"); 
  12.         //使?jié)L動條滾動到最底端 
  13.         ReceiveArea.setCaretPosition(ReceiveArea.getText().length()); 
  14.         medium.relay(name, ad); 
  15.     } 
  16.  
  17.     public void receive(String from, String ad) { 
  18.         ReceiveArea.append(from + "說: " + ad + "\n"); 
  19.         //使?jié)L動條滾動到最底端 
  20.         ReceiveArea.setCaretPosition(ReceiveArea.getText().length()); 
  21.     } 
  22.  
  23. //具體同事類:買方 
  24. class Buyer extends Customer { 
  25.     private static final long serialVersionUID = -474879276076308825L; 
  26.  
  27.     public Buyer(String name) { 
  28.         super(name); 
  29.         ClientWindow(350, 100); 
  30.     } 
  31.  
  32.     public void send(String ad) { 
  33.         ReceiveArea.append("我(買方)說: " + ad + "\n"); 
  34.         //使?jié)L動條滾動到最底端 
  35.         ReceiveArea.setCaretPosition(ReceiveArea.getText().length()); 
  36.         medium.relay(name, ad); 
  37.     } 
  38.  
  39.     public void receive(String from, String ad) { 
  40.         ReceiveArea.append(from + "說: " + ad + "\n"); 
  41.         //使?jié)L動條滾動到最底端 
  42.         ReceiveArea.setCaretPosition(ReceiveArea.getText().length()); 
  43.     } 

輸出結(jié)果

優(yōu)點

  1. 降低了對象之間的耦合性,使得對象易于獨立地被復用。
  2. 將對象間的一對多關聯(lián)轉(zhuǎn)變?yōu)橐粚σ坏年P聯(lián),提高系統(tǒng)的靈活性,使得系統(tǒng)易于維護和擴展。

缺點

當同事類太多時,中介者的職責將很大,它會變得復雜而龐大,以至于系統(tǒng)難以維護。

應用場景

  • 當對象之間存在復雜的網(wǎng)狀結(jié)構關系而導致依賴關系混亂且難以復用時。
  • 當想創(chuàng)建一個運行于多個類之間的對象,又不想生成新的子類時。

模式的擴展

在實際開發(fā)中,通常采用以下兩種方法來簡化中介者模式,使開發(fā)變得更簡單。

 

  1. 不定義中介者接口,把具體中介者對象實現(xiàn)成為單例。
  2. 同事對象不持有中介者,而是在需要的時候直接獲取中介者對象并調(diào)用。

程序代碼如下:

  1. package com.niuh.designpattern.mediator.v3; 
  2.  
  3. import java.util.ArrayList; 
  4. import java.util.List; 
  5.  
  6. /** 
  7.  * <p> 
  8.  * 簡化中介者模式 
  9.  * </p> 
  10.  */ 
  11. public class SimpleMediatorPattern { 
  12.     public static void main(String[] args) { 
  13.         SimpleColleague c1, c2; 
  14.         c1 = new SimpleConcreteColleague1(); 
  15.         c2 = new SimpleConcreteColleague2(); 
  16.         c1.send(); 
  17.         System.out.println("=============="); 
  18.         c2.send(); 
  19.     } 
  20.  
  21. //簡單單例中介者 
  22. class SimpleMediator { 
  23.     private static SimpleMediator smd = new SimpleMediator(); 
  24.     private List<SimpleColleague> colleagues = new ArrayList<SimpleColleague>(); 
  25.  
  26.     private SimpleMediator() { 
  27.     } 
  28.  
  29.     public static SimpleMediator getMedium() { 
  30.         return (smd); 
  31.     } 
  32.  
  33.     public void register(SimpleColleague colleague) { 
  34.         if (!colleagues.contains(colleague)) { 
  35.             colleagues.add(colleague); 
  36.         } 
  37.     } 
  38.  
  39.     public void relay(SimpleColleague scl) { 
  40.         for (SimpleColleague ob : colleagues) { 
  41.             if (!ob.equals(scl)) { 
  42.                 ((SimpleColleague) ob).receive(); 
  43.             } 
  44.         } 
  45.     } 
  46.  
  47. //抽象同事類 
  48. interface SimpleColleague { 
  49.     void receive(); 
  50.  
  51.     void send(); 
  52.  
  53. //具體同事類 
  54. class SimpleConcreteColleague1 implements SimpleColleague { 
  55.     SimpleConcreteColleague1() { 
  56.         SimpleMediator smd = SimpleMediator.getMedium(); 
  57.         smd.register(this); 
  58.     } 
  59.  
  60.     public void receive() { 
  61.         System.out.println("具體同事類1:收到請求。"); 
  62.     } 
  63.  
  64.     public void send() { 
  65.         SimpleMediator smd = SimpleMediator.getMedium(); 
  66.         System.out.println("具體同事類1:發(fā)出請求..."); 
  67.         smd.relay(this); //請中介者轉(zhuǎn)發(fā) 
  68.     } 
  69.  
  70. //具體同事類 
  71. class SimpleConcreteColleague2 implements SimpleColleague { 
  72.     SimpleConcreteColleague2() { 
  73.         SimpleMediator smd = SimpleMediator.getMedium(); 
  74.         smd.register(this); 
  75.     } 
  76.  
  77.     public void receive() { 
  78.         System.out.println("具體同事類2:收到請求。"); 
  79.     } 
  80.  
  81.     public void send() { 
  82.         SimpleMediator smd = SimpleMediator.getMedium(); 
  83.         System.out.println("具體同事類2:發(fā)出請求..."); 
  84.         smd.relay(this); //請中介者轉(zhuǎn)發(fā) 
  85.     } 

輸出結(jié)果如下:

  • 具體同事類1:發(fā)出請求...
  • 具體同事類2:收到請求。
  • 具體同事類2:發(fā)出請求...
  • 具體同事類1:收到請求。

 

源碼中的應用

  1. java.util.Timer 
  2. java.util.concurrent.Executer#execute() 
  3. java.util.concurrent.ExecuterService#submit() 
  4. java.lang.reflect.Method#invoke() 

PS:以上代碼提交在 Github :

https://github.com/Niuh-Study/niuh-designpatterns.git

 

責任編輯:姜華 來源: 今日頭條
相關推薦

2023-05-26 08:41:23

模式Go設計模式

2021-07-20 08:52:20

命令模式中介者模式設計模式

2020-10-20 13:33:00

建造者模式

2021-10-26 00:21:19

設計模式建造者

2021-01-21 05:34:14

設計模式建造者

2020-10-26 08:45:39

觀察者模式

2021-07-08 11:28:43

觀察者模式設計

2013-11-26 17:09:57

Android設計模式

2023-10-07 00:17:06

AirDrop中介者模式

2022-01-29 22:12:35

前端模式觀察者

2024-06-05 09:41:41

2020-10-23 09:40:26

設計模式

2020-11-03 13:05:18

命令模式

2020-11-04 08:54:54

狀態(tài)模式

2022-01-12 13:33:25

工廠模式設計

2020-11-09 08:20:33

解釋器模式

2012-01-13 15:59:07

2021-10-28 19:09:09

模式原型Java

2020-10-28 11:56:47

橋接模式

2021-06-09 08:53:34

設計模式策略模式工廠模式
點贊
收藏

51CTO技術棧公眾號