一、模拟AWT事件处理          
回顾一下JDK里面按下一个Button,有件事发生,这个东西怎么写:
package com.cy.dp.observer.awt;

import java.awt.Button;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent; public class TestFrame extends Frame{ public void launch(){
Button b = new Button("press me");
b.addActionListener(new MyActionListener());
b.addActionListener(new MyActionListener2());
this.add(b);
this.pack(); this.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}); this.setVisible(true);
} public static void main(String[] args) {
new TestFrame().launch();
} private class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("button pressed!");
}
} private class MyActionListener2 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("button pressed 2!");
}
}
}

二、自己手写模拟AWT事件处理        

代码:

package com.cy.dp.observer.awt;

import java.util.ArrayList;
import java.util.List; public class Test {
public static void main(String[] args) {
Button b = new Button();
b.addActionListener(new MyActionListener());
b.addActionListener(new MyActionListener2());
b.buttonPressed();
}
} class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("button pressed, time:"+e.getWhen()+", source:"+e.getSource());
}
}
class MyActionListener2 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("button pressed 2, time:"+e.getWhen()+", source:"+e.getSource());
}
} /********************************对于使用java.awt包,下面代码就不可见了****************************************/
/**
* 自己模拟的Button
*/
class Button{
private List<ActionListener> listeners = new ArrayList<ActionListener>(); public void addActionListener(ActionListener l){
this.listeners.add(l);
} //模拟button被按下
public void buttonPressed() {
ActionEvent e = new ActionEvent(this, System.currentTimeMillis());
for(ActionListener listener : listeners){
listener.actionPerformed(e);
}
} } interface ActionListener{
public void actionPerformed(ActionEvent e);
} /**
* 自己模拟的事件
*/
class ActionEvent{
private long when;
private Object source; public ActionEvent(Object source, long when){
this.source = source;
this.when = when;
} public long getWhen(){
return when;
}
public Object getSource(){
return source;
}
}

console:

button pressed, time:1529588100490, source:com.cy.dp.observer.awt.Button@2a139a55
button pressed 2, time:1529588100490, source:com.cy.dp.observer.awt.Button@2a139a55

java设计模式-Observer(2)的更多相关文章

  1. Java 设计模式 – Observer 观察者模式

    目录 [隐藏] 1 代码 1.1 观察者接口: 1.2 被观察者: 1.3 观众类 : 1.4 电影类: 1.5 效果如下: 代码 说明都在注释: 观察者接口: package ObserverMod ...

  2. Java设计模式——Observer(观察者)模式

    在多个对象之间建立一对多的关系,以便当一个对象状态改变的时候.其它全部依赖于这个对象的对象都能得到通知,并被自己主动更新. 适用情况: 当一个抽象模型有两个方面,当中一个方面依赖于还有一方面. 将这二 ...

  3. Java 设计模式实现 不错的引用

    这段时间有兴趣重新温习一下设计模式在Java中的实现,碰巧看到一个不错的设计模式总结,这里引用一下作为参考. 创建型模式: JAVA设计模式-Singleton JAVA设计模式-Factory JA ...

  4. java设计模式--观察者模式(Observer)

    java设计模式--观察者模式(Observer) java设计模式--观察者模式(Observer) 观察者模式的定义: 定义对象间的一种一对多的依赖关系.当一个对象的状态发生改变时,所有依赖于它的 ...

  5. Java设计模式(20)观察者模式(Observer模式)

    Java深入到一定程度,就不可避免的碰到设计模式(design pattern)这一概念,了解设计模式,将使自己对java中的接口或抽象类应用有更深的理解.设计模式在java的中型系统中应用广泛,遵循 ...

  6. Java 设计模式系列(十五)观察者模式(Observer)

    Java 设计模式系列(十五)观察者模式(Observer) Java 设计模式系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) Java ...

  7. java设计模式解析(1) Observer观察者模式

      设计模式系列文章 java设计模式解析(1) Observer观察者模式 java设计模式解析(2) Proxy代理模式 java设计模式解析(3) Factory工厂模式 java设计模式解析( ...

  8. Java设计模式(17)——行为模式之观察者模式(Observer)

    一.概述 概念 UML简图 我们根据一个示例得类图来分析角色 角色 抽象主题:保存观察者聚集(集合),管理(增删)观察者 抽象观察者:定义具体观察者的抽象接口,在得到主题通知后更新自己 具体主题:将有 ...

  9. 《JAVA设计模式》之观察者模式(Observer)

    在阎宏博士的<JAVA与模式>一书中开头是这样描述观察者(Observer)模式的: 观察者模式是对象的行为模式,又叫发布-订阅(Publish/Subscribe)模式.模型-视图(Mo ...

随机推荐

  1. 51nod- 【1042 数字0-9的数量 】

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1042 题目: 1042 数字0-9的数量 基准时间限制:1  ...

  2. 软工实践第二次作业—Wordcount

    Git仓库地址:https://github.com/cwabc/PersonProject-C 一.问题描述 输入一个txt文件名,以命令行参数传入,程序能够统计txt文件中的以下几个指标: 统计文 ...

  3. Java 源码解析

    Object equals方法对比两个对象是否是内存中同一个物理地址 hashCode规定,当两个对象相等时,必须返回相等的hashCode,所以重写equals方法有必要重写hashCode方法 如 ...

  4. plot 函数常用参数

  5. hdu4135 Co-prime 容斥原理

    Given a number N, you are asked to count the number of integers between A and B inclusive which are ...

  6. hdu3605 Escape 二分图多重匹配/最大流

    2012 If this is the end of the world how to do? I do not know how. But now scientists have found tha ...

  7. vuex简介(转载)

    安装.使用 vuex 首先我们在 vue.js 2.0 开发环境中安装 vuex : npm install vuex --save 然后 , 在 main.js 中加入 : import vuex ...

  8. CentOS7.1 KVM虚拟化之环境准备

    备注:实验没有问题,只是暂时还不知道弄这个用来干嘛,不过先留着以后查看吧 一.基础平台 1.一台装有VMware的Windows系统 (可联网) 2.CentOS7.1 64bit 镜像 二.最小化安 ...

  9. 数学与猜想 合情推理模式 (G. 波利亚 著)

    第十二章 几个著名模式 (已看) $1. 证实一个结论 $2. 连续证实几个结论 $3. 证实一个未必可信的结论 $4. 类比推理 $5. 加深类比 $6. 被隐没的类比推理 第十三章 更多的模式与最 ...

  10. C# 获取机器码

    using System.Runtime.InteropServices; using System.Management; using System; public class HardwareIn ...