我认为这个模式比较常见,还记得我们学习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设计模式--缺省适配器模式的更多相关文章

  1. java 设计模式-缺省适配器模式

    本文转载地址:http://www.cnblogs.com/iyangyuan/archive/2013/03/11/2954808.html 在程序设计过程中,读者很可能遇到这样一种困境:设计了一个 ...

  2. 重学 Java 设计模式:实战适配器模式

    作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 擦屁屁纸80%的面积都是保护手的! 工作到3年左右很大一部分程序员都想提升自己的技术 ...

  3. Java设计模式系列之适配器模式

    适配器模式的定义 将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作.(就类似于我们充电器的转接头将220V的电压转换成我们的手机端 ...

  4. 《JAVA设计模式》之适配器模式(Adapter)

    在阎宏博士的<JAVA与模式>一书中开头是这样描述适配器(Adapter)模式的: 适配器模式把一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能 ...

  5. Java设计模式7:适配器模式

    适配器模式 适配器模式说的是,可以把一个类的接口变换成客户端所期待的另一种接口,使得原本因接口不匹配而无法在一起工作的两个类可以一起工作. 适配器模式的用途 适配器模式的用途,在网上找了一幅图,挺形象 ...

  6. java设计模式笔记(1)-适配器模式

    适配器的定义 适配器就是一个接口转换器,它可以是一个独立的硬件接口设备,允许硬件或电子接口与其它硬件或电子接口相连,也可以是信息接口.比如:电源适配器.三角架基座转接部件.USB与串口的转接设备等. ...

  7. java设计模式自我总结---适配器模式

    上一篇博客说完了 java 23 中设计模式中的五种 创建性模式,由于篇幅过长,新开一贴今天开始学习结构型模式, 结构型模式包括以下七种:适配器模式.装饰模式.代理模式.外观模式.桥接模式.组合模式. ...

  8. JAVA设计模式初探之适配器模式

    http://blog.csdn.net/jason0539/article/details/22468457 1. 概述 将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接 ...

  9. Java设计模式学习记录-适配器模式

    前言 之前已经将五个创建型设计模式介绍完了,从这一篇开始介绍结构型设计模式,适配器模式就是结构型模式的一种,适配器要实现的效果是把“源”过渡到“目标”. 适配器模式 在开发过程中,使用一个已经存在的类 ...

随机推荐

  1. MySQL高可用方案-PXC环境部署记录

    之前梳理了Mysql+Keepalived双主热备高可用操作记录,对于mysql高可用方案,经常用到的的主要有下面三种: 一.基于主从复制的高可用方案:双节点主从 + keepalived 一般来说, ...

  2. Ceph常规操作及常见问题梳理

    Ceph集群管理 每次用命令启动.重启.停止Ceph守护进程(或整个集群)时,必须指定至少一个选项和一个命令,还可能要指定守护进程类型或具体例程. **命令格式如 {commandline} [opt ...

  3. cross-env简介

    是什么 运行跨平台设置和使用环境变量的脚本 出现原因 当您使用NODE_ENV =production, 来设置环境变量时,大多数Windows命令提示将会阻塞(报错). (异常是Windows上的B ...

  4. Python_试题_23

    # Python基础数据类型考试题# 考试时间:两个半小时 满分100分(80分以上包含80分及格)# 一,基础题.# 1,简述变量命名规范(3分)# 答:变量名是由数字.字母.下划线任意组合,变量名 ...

  5. B. Views Matter

    链接 [http://codeforces.com/contest/1061/problem/B] 题意 问你最多去掉多少块使得从上和右看,投影图不变 分析 注意细节,尤其第一列 代码 #includ ...

  6. Finished yeah!

    终于到了最后的博客阶段,这时候才知道博客此时此刻是多么的惬意,它成了书写心声的自由平台!耗时一天完成这作业说起来也是蛮辛苦的,编译器需要新装,IDE需要熟悉,当然最主要的是之前浅入浅出的C++功底在此 ...

  7. Linux内核及分析 第六周 分析Linux内核创建一个新进程的过程

    实验过程 1.github上克隆相应的mengning/menu.git 2.测试menuOS,测试fork直接执行结果 3.配置调试系统,进入gdb调试,利用file linux-3.18.6/vm ...

  8. 冲刺Two之站立会议9

    今天我们团队主要针对软件的功能进行了改进.因为它目前可以实现视频通话,语音聊天,文件传输和文字聊天的通信功能,我们想要在它的基础上实现临时局域群聊和群聊视频的功能,目前还没有完全实现.

  9. filter运行出现 <filter object at 0x000001B68F052828> 判断素数

    刚接触filter时  运行总是出现<filter object at 0x000001B68F052828>  得不到想要的数据 后来发现是因为filter的结果是一个数组 需要 lis ...

  10. 文件系统的block 数据库中的block 以及内存中的page基础知识汇总(自己理解 可能有误)

    1. 操作系统文件系统中的的block 文件系统中的block 是文件系统最小的读写单元,在HDD的磁盘时代, 一般block的大小可能与磁盘的扇区大小一致为 512bytes 也因为这个原因MBR启 ...