Java設計模式--預設適配器模式

來源:互聯網
上載者:User

標籤:java設計   open   client   ocs   child   cal   適配   swing   ges   

我認為這個模式比較常見,還記得我們學習Swing的時候嗎,有沒有見過很多Adapter?那時候不知道Adapter的意義所在,但至少知道他能夠省去我們不需要的實現。

 

這個社會有N中職業(job),但是每個人(people)只可能從事其中一種或者幾種,職業類型設成一個介面,難道每次給人設定職業的時候要全部實現嗎?在這裡就要有一個預設適配器,預設適配器是個抽象類別,僅僅implements而不實現。然後用戶端直接使用Adapter即可選擇需要實現的方法,而不用實現全部。

 

Job

package com.design.adapter.defaultadapter;public interface Job {    void police();    void programmer();    void teacher();    void productManager();}

DefaultJobAdapter

package com.design.adapter.defaultadapter;public abstract class DefaultJobAdapter implements Job {    @Override    public void police() {    }    @Override    public void programmer() {    }    @Override    public void teacher() {    }    @Override    public void productManager() {    }}

 

用戶端

package com.design.adapter.defaultadapter;public class Client {    public static void main(String[] args){        People people = new People();        people.addJob(new DefaultJobAdapter() {            @Override            public void programmer() {                System.out.println("寫代碼,與產品撕逼");            }        });    }}class People{    public void addJob(Job job){    }}

這樣,就不需要寫不需要的代碼啦。

 

 看一下WindowAdapter

 用戶端調用情況

package com.design.adapter.defaultadapter;import javax.swing.*;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;public class TestWindowsAdapter {    public static void main(String[] args){        JFrame frame = new JFrame("Hello");        frame.addWindowListener(new WindowAdapter() {            @Override            public void windowOpened(WindowEvent e) {                super.windowOpened(e);            }            @Override            public void windowClosed(WindowEvent e) {                super.windowClosed(e);            }            @Override            public void windowLostFocus(WindowEvent e) {                super.windowLostFocus(e);            }        });    }}

 

addWindowListener的實現

    /**     * Adds the specified window listener to receive window events from     * this window.     * If l is null, no exception is thrown and no action is performed.     * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"     * >AWT Threading Issues</a> for details on AWT‘s threading model.     *     * @param   l the window listener     * @see #removeWindowListener     * @see #getWindowListeners     */    public synchronized void addWindowListener(WindowListener l) {        if (l == null) {            return;        }        newEventsOnly = true;        windowListener = AWTEventMulticaster.add(windowListener, l);    }

 

WindowAdapter的實現

/* * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package java.awt.event;/** * An abstract adapter class for receiving window events. * The methods in this class are empty. This class exists as * convenience for creating listener objects. * <P> * Extend this class to create a <code>WindowEvent</code> listener * and override the methods for the events of interest. (If you implement the * <code>WindowListener</code> interface, you have to define all of * the methods in it. This abstract class defines null methods for them * all, so you can only have to define methods for events you care about.) * <P> * Create a listener object using the extended class and then register it with * a Window using the window‘s <code>addWindowListener</code> * method. When the window‘s status changes by virtue of being opened, * closed, activated or deactivated, iconified or deiconified, * the relevant method in the listener * object is invoked, and the <code>WindowEvent</code> is passed to it. * * @see WindowEvent * @see WindowListener * @see <a href="https://docs.oracle.com/javase/tutorial/uiswing/events/windowlistener.html">Tutorial: Writing a Window Listener</a> * * @author Carl Quinn * @author Amy Fowler * @author David Mendenhall * @since 1.1 */public abstract class WindowAdapter    implements WindowListener, WindowStateListener, WindowFocusListener{    /**     * Invoked when a window has been opened.     */    public void windowOpened(WindowEvent e) {}    /**     * Invoked when a window is in the process of being closed.     * The close operation can be overridden at this point.     */    public void windowClosing(WindowEvent e) {}    /**     * Invoked when a window has been closed.     */    public void windowClosed(WindowEvent e) {}    /**     * Invoked when a window is iconified.     */    public void windowIconified(WindowEvent e) {}    /**     * Invoked when a window is de-iconified.     */    public void windowDeiconified(WindowEvent e) {}    /**     * Invoked when a window is activated.     */    public void windowActivated(WindowEvent e) {}    /**     * Invoked when a window is de-activated.     */    public void windowDeactivated(WindowEvent e) {}    /**     * Invoked when a window state is changed.     * @since 1.4     */    public void windowStateChanged(WindowEvent e) {}    /**     * Invoked when the Window is set to be the focused Window, which means     * that the Window, or one of its subcomponents, will receive keyboard     * events.     *     * @since 1.4     */    public void windowGainedFocus(WindowEvent e) {}    /**     * Invoked when the Window is no longer the focused Window, which means     * that keyboard events will no longer be delivered to the Window or any of     * its subcomponents.     *     * @since 1.4     */    public void windowLostFocus(WindowEvent e) {}}

 

 

看一下WindowListener

/* * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package java.awt.event;import java.util.EventListener;/** * The listener interface for receiving window events. * The class that is interested in processing a window event * either implements this interface (and all the methods it * contains) or extends the abstract <code>WindowAdapter</code> class * (overriding only the methods of interest). * The listener object created from that class is then registered with a * Window using the window‘s <code>addWindowListener</code> * method. When the window‘s status changes by virtue of being opened, * closed, activated or deactivated, iconified or deiconified, * the relevant method in the listener object is invoked, and the * <code>WindowEvent</code> is passed to it. * * @author Carl Quinn * * @see WindowAdapter * @see WindowEvent * @see <a href="https://docs.oracle.com/javase/tutorial/uiswing/events/windowlistener.html">Tutorial: How to Write Window Listeners</a> * * @since 1.1 */public interface WindowListener extends EventListener {    /**     * Invoked the first time a window is made visible.     */    public void windowOpened(WindowEvent e);    /**     * Invoked when the user attempts to close the window     * from the window‘s system menu.     */    public void windowClosing(WindowEvent e);    /**     * Invoked when a window has been closed as the result     * of calling dispose on the window.     */    public void windowClosed(WindowEvent e);    /**     * Invoked when a window is changed from a normal to a     * minimized state. For many platforms, a minimized window     * is displayed as the icon specified in the window‘s     * iconImage property.     * @see java.awt.Frame#setIconImage     */    public void windowIconified(WindowEvent e);    /**     * Invoked when a window is changed from a minimized     * to a normal state.     */    public void windowDeiconified(WindowEvent e);    /**     * Invoked when the Window is set to be the active Window. Only a Frame or     * a Dialog can be the active Window. The native windowing system may     * denote the active Window or its children with special decorations, such     * as a highlighted title bar. The active Window is always either the     * focused Window, or the first Frame or Dialog that is an owner of the     * focused Window.     */    public void windowActivated(WindowEvent e);    /**     * Invoked when a Window is no longer the active Window. Only a Frame or a     * Dialog can be the active Window. The native windowing system may denote     * the active Window or its children with special decorations, such as a     * highlighted title bar. The active Window is always either the focused     * Window, or the first Frame or Dialog that is an owner of the focused     * Window.     */    public void windowDeactivated(WindowEvent e);}

 

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.