Default Adapter概述

Default Adapter - 图1

玩具代码案例 - 事件监听器

监听器接口

IEventListener

  1. package online.javabook.gof.structural.patterns1.adapter.defaultadapter.listener.api;
  2. import java.awt.*;
  3. public interface IEventListener {
  4. public void onClick(Event e);
  5. public void onDbClick(Event e);
  6. public void onMouseDown(Event e);
  7. public void onMouseOver(Event e);
  8. public void onMouseUp(Event e);
  9. public void onMouseOut(Event e);
  10. public void onMouseMove(Event e);
  11. }

事件类型

Event

  1. package org.gof.structural.patterns1.adapter.defaultadapter.listener.api;
  2. public class Event {
  3. }

默认适配器类型

EventListenerDefaultAdapter

  1. package online.javabook.gof.structural.patterns1.adapter.defaultadapter.listener.impl;
  2. import online.javabook.gof.structural.patterns1.adapter.defaultadapter.listener.api.IEventListener;
  3. import java.awt.*;
  4. public abstract class EventListenerDefaultAdapter implements IEventListener {
  5. @Override
  6. public void onClick(Event e) {
  7. // DefaultAdapter method
  8. }
  9. @Override
  10. public void onDbClick(Event e) {
  11. // DefaultAdapter method
  12. }
  13. @Override
  14. public void onMouseDown(Event e) {
  15. // DefaultAdapter method
  16. }
  17. @Override
  18. public void onMouseOver(Event e) {
  19. // DefaultAdapter method
  20. }
  21. @Override
  22. public void onMouseUp(Event e) {
  23. // DefaultAdapter method
  24. }
  25. @Override
  26. public void onMouseOut(Event e) {
  27. // DefaultAdapter method
  28. }
  29. @Override
  30. public void onMouseMove(Event e) {
  31. // DefaultAdapter method
  32. }
  33. }

不基于默认适配器模式的实现

OnClickEventListener1

  1. package online.javabook.gof.structural.patterns1.adapter.defaultadapter.listener.impl;
  2. import online.javabook.gof.structural.patterns1.adapter.defaultadapter.listener.api.IEventListener;
  3. import java.awt.*;
  4. public class OnClickEventListener1 implements IEventListener {
  5. @Override
  6. public void onClick(Event e) {
  7. System.out.println("Button is clicked.");
  8. }
  9. @Override
  10. public void onDbClick(Event e) {
  11. }
  12. @Override
  13. public void onMouseDown(Event e) {
  14. }
  15. @Override
  16. public void onMouseOver(Event e) {
  17. }
  18. @Override
  19. public void onMouseUp(Event e) {
  20. }
  21. @Override
  22. public void onMouseOut(Event e) {
  23. }
  24. @Override
  25. public void onMouseMove(Event e) {
  26. }
  27. }

基于默认适配器模式的实现

OnClickEventListener2

package online.javabook.gof.structural.patterns1.adapter.defaultadapter.listener.impl;

import java.awt.*;

public class OnClickEventListener2 extends EventListenerDefaultAdapter {

    @Override
    public void onClick(Event e) {
        System.out.println("Button is clicked.");
    }

    @Override
    public void onDbClick(Event e) {
        System.out.println("Button is dbclicked.");
    }
}

现实世界中的默认适配器模式

java.awt 客户端编程中的鼠标移动监听器,MouseMotionAdapter就是一个标准的默认适配器模式的实现,避免开发者直接与MouseMotionListener打交道,导致即便只想对mouseDragged事件进行处理,却不得不也要实现mouseMoved方法(即便什么都不做),在这个案例中MouseMotionListener监听器需要实现的接口还不算多,只有两个。对于那些有更多监听接口的监听器,如果每个方法都要去实现一遍,工作量就大了很多。所有默认适配器模式就起到了承上启下的作用,在中间做了一层隔离。

/*
 * 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 mouse motion events.
 * The methods in this class are empty. This class exists as
 * convenience for creating listener objects.
 * <P>
 * Mouse motion events occur when a mouse is moved or dragged.
 * (Many such events will be generated in a normal program.
 * To track clicks and other mouse events, use the MouseAdapter.)
 * <P>
 * Extend this class to create a <code>MouseEvent</code> listener
 * and override the methods for the events of interest. (If you implement the
 * <code>MouseMotionListener</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 component using the component's <code>addMouseMotionListener</code>
 * method. When the mouse is moved or dragged, the relevant method in the
 * listener object is invoked and the <code>MouseEvent</code> is passed to it.
 *
 * @author Amy Fowler
 *
 * @see MouseEvent
 * @see MouseMotionListener
 * @see <a href="https://docs.oracle.com/javase/tutorial/uiswing/events/mousemotionlistener.html">Tutorial: Writing a Mouse Motion Listener</a>
 *
 * @since 1.1
 */
public abstract class MouseMotionAdapter implements MouseMotionListener {
    /**
     * Invoked when a mouse button is pressed on a component and then
     * dragged.  Mouse drag events will continue to be delivered to
     * the component where the first originated until the mouse button is
     * released (regardless of whether the mouse position is within the
     * bounds of the component).
     */
    public void mouseDragged(MouseEvent e) {}

    /**
     * Invoked when the mouse button has been moved on a component
     * (with no buttons no down).
     */
    public void mouseMoved(MouseEvent e) {}
}