中介者模式(Mediator Pattern)
生活随笔
收集整理的這篇文章主要介紹了
中介者模式(Mediator Pattern)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
中介者模式
一、概述
??中介者模式(Mediator Pattern)是用來降低多個對象和類之間的通信復雜性。這種模式提供了一個中介類,該類通常處理不同類之間的通信,并支持松耦合,使代碼易于維護。中介者模式屬于行為型模式。
二、介紹
??用一個中介對象來封裝一系列的對象交互,中介者使各對象不需要顯式地相互引用,從而使其耦合松散,而且可以獨立地改變它們之間的交互。
??中介者模式將多個類相互耦合,形成的網狀結構,分離為星型結構。
關鍵代碼:對象 Colleague 之間的通信封裝到一個類中單獨處理。
它主要包含如下幾個角色:
- Mediator: 抽象中介者。定義了同事對象到中介者對象之間的接口。
- ConcreteMediator: 具體中介者。實現抽象中介者的方法,它需要知道所有的具體同事類,同時需要從具體的同事類那里接收信息,并且向具體的同事類發送信息。
- Colleague: 抽象同事類。
- ConcreteColleague: 具體同事類。每個具體同事類都只需要知道自己的行為即可,但是他們都需要認識中介者。
優點:
缺點:中介者會龐大,變得復雜難以維護。
使用場景:
三、代碼實現
抽象中介類
package com.designpattern.mediatorPattern;/*** 抽象中介類** @author zhongtao on 2018/12/19*/ public abstract class Mediator {public abstract void contact(String content, Colleague colleague); }抽象同事類
package com.designpattern.mediatorPattern;/*** 抽象同事類** @author zhongtao on 2018/12/19*/ public class Colleague {protected String name;protected Mediator mediator;public Colleague(String name, Mediator mediator) {this.name = name;this.mediator = mediator;} }具體同事類A和B
package com.designpattern.mediatorPattern;/*** 具體同事類A** @author zhongtao on 2018/12/19*/ public class ColleagueA extends Colleague {public ColleagueA(String name, Mediator mediator) {super(name, mediator);}public void getMessage(String message) {System.out.println("同事A:" + name + " 獲取信息:" + message);}public void contact(String message) {mediator.contact(message, this);} }/*** 具體同事類B** @author zhongtao on 2018/12/19*/ public class ColleagueB extends Colleague {public ColleagueB(String name, Mediator mediator) {super(name, mediator);}public void getMessage(String message) {System.out.println("同事B:" + name + " 獲取信息:" + message);}public void contact(String message) {mediator.contact(message, this);} }具體中介類
package com.designpattern.mediatorPattern;/*** 具體中介類** @author zhongtao on 2018/12/19*/ public class ConcreteMediator extends Mediator {private ColleagueA colleagueA;private ColleagueB colleagueB;public ColleagueA getColleagueA() {return colleagueA;}public void setColleagueA(ColleagueA colleagueA) {this.colleagueA = colleagueA;}public ColleagueB getColleagueB() {return colleagueB;}public void setColleagueB(ColleagueB colleagueB) {this.colleagueB = colleagueB;}@Overridepublic void contact(String content, Colleague colleague) {if (colleague == colleagueA) {colleagueB.getMessage(content);} else {colleagueA.getMessage(content);}} }客戶端 測試中介者模式
package com.designpattern.mediatorPattern;/*** 客戶端 測試中介者模式** @author zhongtao on 2018/12/19*/ public class Client {public static void main(String[] args) {ConcreteMediator mediator = new ConcreteMediator();ColleagueA peter = new ColleagueA("Peter", mediator);ColleagueB lina = new ColleagueB("Lina", mediator);mediator.setColleagueA(peter);mediator.setColleagueB(lina);peter.contact("我是peter,想請lina晚上看電影");lina.contact("我是lina,可以滴");} }測試結果
同事B:Lina 獲取信息:我是peter,想請lina晚上看電影 同事A:Peter 獲取信息:我是lina,可以滴源碼地址:https://github.com/zt19994/designPattern
轉載于:https://www.cnblogs.com/zt19994/p/10143697.html
總結
以上是生活随笔為你收集整理的中介者模式(Mediator Pattern)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 判断数组对象里面的某个属性全部为true
- 下一篇: wpf mvvm模式下CommandPa