Default Adapter概述
玩具代码案例 - 事件监听器
监听器接口
IEventListener
package online.javabook.gof.structural.patterns1.adapter.defaultadapter.listener.api;
import java.awt.*;
public interface IEventListener {
public void onClick(Event e);
public void onDbClick(Event e);
public void onMouseDown(Event e);
public void onMouseOver(Event e);
public void onMouseUp(Event e);
public void onMouseOut(Event e);
public void onMouseMove(Event e);
}
事件类型
Event
package org.gof.structural.patterns1.adapter.defaultadapter.listener.api;
public class Event {
}
默认适配器类型
EventListenerDefaultAdapter
package online.javabook.gof.structural.patterns1.adapter.defaultadapter.listener.impl;
import online.javabook.gof.structural.patterns1.adapter.defaultadapter.listener.api.IEventListener;
import java.awt.*;
public abstract class EventListenerDefaultAdapter implements IEventListener {
@Override
public void onClick(Event e) {
// DefaultAdapter method
}
@Override
public void onDbClick(Event e) {
// DefaultAdapter method
}
@Override
public void onMouseDown(Event e) {
// DefaultAdapter method
}
@Override
public void onMouseOver(Event e) {
// DefaultAdapter method
}
@Override
public void onMouseUp(Event e) {
// DefaultAdapter method
}
@Override
public void onMouseOut(Event e) {
// DefaultAdapter method
}
@Override
public void onMouseMove(Event e) {
// DefaultAdapter method
}
}
不基于默认适配器模式的实现
OnClickEventListener1
package online.javabook.gof.structural.patterns1.adapter.defaultadapter.listener.impl;
import online.javabook.gof.structural.patterns1.adapter.defaultadapter.listener.api.IEventListener;
import java.awt.*;
public class OnClickEventListener1 implements IEventListener {
@Override
public void onClick(Event e) {
System.out.println("Button is clicked.");
}
@Override
public void onDbClick(Event e) {
}
@Override
public void onMouseDown(Event e) {
}
@Override
public void onMouseOver(Event e) {
}
@Override
public void onMouseUp(Event e) {
}
@Override
public void onMouseOut(Event e) {
}
@Override
public void onMouseMove(Event e) {
}
}
基于默认适配器模式的实现
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) {}
}