java設計模式-監聽器模式

來源:互聯網
上載者:User

標籤:stat   throw   logs   事件   out   技術分享   sts   注意   封裝   

監聽器模式有三個要素——事件來源、事件對象、監聽器。

事件來源:顧名思義,事件發生的源頭,比如點擊的按鈕,屬於被監聽的對象;

事件對象:這個經常和事件來源混淆,它經常被用來封裝事件來源,切記,它畢竟是個事件,比如點擊事件,和事件來源的區別自己感受,木有栗子;

監聽器:這個是監聽器模式的核心,定義事件發生後的動作,通常事件對象作為監聽器中定義的函數入參。

下面舉個簡單的栗子:

故事背景是,小明是個不講衛生的孩子,他媽媽很擔心他的健康,規定必須飯前洗手。

定義一個熊孩子。熊孩子就是被監聽的對象,是事件來源,一切事件都是事件來源發出,這似乎是句廢話。

public class Child {    private String name;    private RemindListener remindListener;    public Child(String name){        this.name = name;    }    public void eat() {        if(null!=remindListener){            remindListener.remind(new RemindWashingHandsEvent(this));        }        System.out.println("Child eat...");    }    public void addListener(RemindListener listener){        remindListener = listener;    }}

接下來是看看事件對象,事件對象正如上面所述,封裝了事件來源。我們在這裡定義一個飯前洗手事件。

public class RemindWashingHandsEvent {    private Child child;    public RemindWashingHandsEvent(Child child){        this.child = child;    }}

事件對象定義了事件的屬性、狀態。

緊接著是定義事件發生後,監聽器的動作,在這裡是提醒洗手。

public class RemindListener {    public void remind(RemindWashingHandsEvent remindWashingHandsEvent){        System.out.println("listen to mom, washing hands before eating...");    }}

注意,監聽器主要封裝了動作,僅此而已。

以上代碼,只是為了說明監聽器模式原理,代碼通俗,不太優雅。

下面繼承或實現java標準庫,又隨手寫了一對代碼,夜深了,有時間再解釋。

public class Kid{    private String name;    private List<Listener> liteners;    public Kid(String name) {        this.name = name;        this.liteners = Lists.newArrayList();    }    public void eat(){        for(Listener listener:liteners){            if(listener instanceof WashingHandsListener){                WashingHandsListener washingHandsListener = (WashingHandsListener) listener;                washingHandsListener.fireAfterEventInvoked(new WashingHandsEvent(this,"洗手"));            }        }        System.out.println("吃飯...");    }    public void addListener(Listener listener){        liteners.add(listener);    }    }

  

public class Event extends EventObject {    /**     * Constructs a prototypical Event.     *     * @param source The object on which the Event initially occurred.     * @throws IllegalArgumentException if source is null.     */    public Event(Object source) {        super(source);    }}

  

public class WashingHandsEvent extends Event{    private String eventName;    /**     * Constructs a prototypical Event.     *     * @param source The object on which the Event initially occurred.     * @throws IllegalArgumentException if source is null.     */    public WashingHandsEvent(Object source,String eventName) {        super(source);        this.eventName = eventName;    }    public String getEventName() {        return eventName;    }    public void setEventName(String eventName) {        this.eventName = eventName;    }}

  

public interface Listener extends java.util.EventListener{    public void fireAfterEventInvoked(Event event);}

  

public class WashingHandsListener implements Listener{    @Override    public void fireAfterEventInvoked(Event event) {        WashingHandsEvent washingHandsEvent = (WashingHandsEvent) event;        System.out.println("飯前準備"+ washingHandsEvent.getEventName());    }}

  

public class Test {    public static void main(String[] args) {        Kid xiaoming = new Kid("xiaoming");        xiaoming.addListener(new WashingHandsListener());        xiaoming.eat();    }}

輸出結果:

 

java設計模式-監聽器模式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.