Mediator Pattern

解決的問(wèn)題

所有做過(guò)前端的人都應(yīng)該使用過(guò)該模式。你要開(kāi)發(fā)一個(gè)界面,界面由選項(xiàng)列表(OptionList),文本框(TextBox)和按鈕(Button)組成。在選項(xiàng)列表完成選擇后,文本框就會(huì)顯示選擇的內(nèi)容,按鈕會(huì)變成可點(diǎn)擊的。

代碼

EventHandler:

package com.cong.designpattern.mediator;

public interface EventHandler {
    public void handle();
}

UIControl:

package com.cong.designpattern.mediator;

import java.util.ArrayList;
import java.util.List;

public class UIControl {
    private List<EventHandler> eventHandlers = new ArrayList<>();
    public void addEventHandler(EventHandler eventHandler) {
        this.eventHandlers.add(eventHandler);
    }
    public void removeEventHandler(EventHandler eventHandler) {
        this.eventHandlers.remove(eventHandler);
    }
    protected void notifyEventHandlers() {
        for (EventHandler eventHandler : this.eventHandlers) eventHandler.handle();
    }
}

OptionList:

package com.cong.designpattern.mediator;

public class OptionList extends UIControl {
    private String selection;
    public String getSelection() {
        return selection;
    }
    public void setSelection(String selection) {
        this.selection = selection;
        this.notifyEventHandlers();
    }
}

TextBox:

package com.cong.designpattern.mediator;

public class TextBox extends UIControl {
    private String content;
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
        this.notifyEventHandlers();
    }
}

Button:

package com.cong.designpattern.mediator;

public class Button extends UIControl{
    private boolean isEnabled = false;
    public boolean isEnabled() {
        return isEnabled;
    }
    public void setEnabled(boolean enabled) {
        isEnabled = enabled;
        this.notifyEventHandlers();
    }
}

DialogBox:

package com.cong.designpattern.mediator;

public class DialogBox {
    private Button button = new Button();
    private TextBox textBox = new TextBox();
    private OptionList optionList = new OptionList();
    public DialogBox() {
        this.optionList.addEventHandler(() -> {
            String selection = this.optionList.getSelection();
            this.textBox.setContent(selection);
            this.button.setEnabled(selection != null && !selection.isEmpty());
        });
    }
    public void simulateUserInteraction() {
        this.optionList.setSelection("Hello Mediator Pattern!");
        System.out.println("Text box content: " + this.textBox.getContent());
        System.out.println("Button is enable: " + this.button.isEnabled());
    }
}

Test code:

DialogBox dialogBox = new DialogBox();
dialogBox.simulateUserInteraction();

UML

Mediator Pattern UML
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 【學(xué)習(xí)難度:★★★☆☆,使用頻率:★★☆☆☆】直接出處:中介者模式梳理和學(xué)習(xí):https://github.com...
    BruceOuyang閱讀 929評(píng)論 0 2
  • 1、窗體 1、常用屬性 (1)Name屬性:用來(lái)獲取或設(shè)置窗體的名稱(chēng),在應(yīng)用程序中可通過(guò)Name屬性來(lái)引用窗體。 ...
    Moment__格調(diào)閱讀 4,805評(píng)論 0 11
  • 一、窗體 1.常用屬性(1)Name屬性:用來(lái)獲取或設(shè)置窗體的名稱(chēng),在應(yīng)用程序中可通過(guò)Name屬性來(lái)引用窗體。(2...
    五維思考閱讀 1,256評(píng)論 0 1
  • 單選題 1、C#中,新建一字符串變量 str,并將字符串"He’s a student"保存到串中,則應(yīng)該使用下列...
    Yohann丶blog閱讀 729評(píng)論 0 0
  • 1、常用屬性 Name屬性:用來(lái)獲取或設(shè)置窗體的名稱(chēng),在應(yīng)用程序中可通過(guò)Name屬性來(lái)引用窗體。 WindowSt...
    五維思考閱讀 1,230評(píng)論 0 0

友情鏈接更多精彩內(nèi)容