设计模式之1 observer模式.docx_第1页
设计模式之1 observer模式.docx_第2页
设计模式之1 observer模式.docx_第3页
设计模式之1 observer模式.docx_第4页
设计模式之1 observer模式.docx_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

设计模式总章:设计模式就是把简单的问题复杂化,带来的好处是系统的可扩展性。用了设计模式以后:复杂度增加开发成本增加维护成本降低设计模式的基本原则是,添加,而不是修改。1. 什么叫分析?(确定需求是什么)分析就是要知道我要做什么。确定系统实现什么样的功能。2. 什么叫设计?(实现需求)设计就是我要怎么实现。设计就是多种实现的手段当中选取一个。实现某个功能是使用什么样的方式。用一个接口来封装一系列共同具有的特点。各个监听类都去实现这个接口。在被监听的类上加上addxxxlistener方法。awt当中的事件:事件源可以是,buttion,textField,textArea事件是:ActionEvent相对应的监听器是actionlistener。通过配置文件来管理observer。配置文件:文件名:perties文件的内容:observers=com.bjsxt.dp.observer.Dad,com.bjsxt.dp.observer.GrandFather,com.bjsxt.dp.observer.Dog访问当前配置的文件的main方法:package com.bjsxt.dp.observer;import java.io.IOException;import java.util.ArrayList;import java.util.List;import java.util.Properties;/事件类class WakenUpEvent private long time;private String loc;private Child source;/事件方法public WakenUpEvent(long time, String loc, Child source) super();this.time = time;this.loc = loc;this.source = source;public long getTime() return time;public void setTime(long time) this.time = time;public String getLoc() return loc;public void setLoc(String loc) this.loc = loc;public Child getSource() return source;public void setSource(Child source) this.source = source;/被监听类。class Child implements Runnable private List wakenUpListeners = new ArrayList();public void addWakenUpListener(WakenUpListener l) wakenUpListeners.add(l);void wakeUp() for(int i=0; iwakenUpListeners.size(); i+) WakenUpListener l = wakenUpListeners.get(i);l.ActionToWakenUp(new WakenUpEvent(System.currentTimeMillis(), bed, this);public void run() try Thread.sleep(5000); catch (InterruptedException e) e.printStackTrace();this.wakeUp();/监听类class Dad implements WakenUpListener public void ActionToWakenUp(WakenUpEvent wakenUpEvent) System.out.println(feed child);/监听类class GrandFather implements WakenUpListener public void ActionToWakenUp(WakenUpEvent wakenUpEvent) System.out.println(hug child);/监听类class Dog implements WakenUpListener public void ActionToWakenUp(WakenUpEvent arg0) System.out.println(wang wang .);/监听接口interface WakenUpListener public void ActionToWakenUp(WakenUpEvent wakenUpEvent);/主类public class Test public static void main(String args) Child c = new Child();/通过split方法得到来自配置文件当中的多个observers.String observers = PropertyMgr.getProperty(observers).split(,);/对于每一个observer,把它添加上去。for(String s : observers) try /c是被监听类,也就是child/s是监听类,也就是observers/通过反射找到该类,并new出一个对象来,因为new出来的是object,所以转换成为wakenuplistener,并把这个wakenuplistener添加到chid上。c.addWakenUpListener(WakenUpListener)(Class.forName(s).newInstance(); catch (InstantiationException e) e.printStackTrace(); catch (IllegalAccessException e) e.printStackTrace(); catch (ClassNotFoundException e) e.printStackTrace();new Thread(c).start();/配置文件管理类。class PropertyMgr /单例模式private static Properties props = new Properties();static try /找到配置文件props.load(Test.class.getClassLoader().getResourceAsStream(com/bjsxt/dp/observer/perties); catch (IOException e) e.printStackTrace();public static String getProperty(String key) /拿到属性的值return props.getProperty(key);/预留为其他的类做准备。class CryEvent abstract class Event 模拟的awt事件响应:package com.bjsxt.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();/button类,也就是被监听类。class Button /监听类的listprivate List actionListeners = new ArrayList();/触发事件的方法public void buttonPressed() ActionEvent e = new ActionEvent(System.currentTimeMillis(),this);/每个监听类都去响应actionperformed方法。只是每个监听器可以有不同的具体实现。for(int i=0; iactionListeners.size(); i+) ActionListener l = actionListeners.get(i);/调用actionperformed方法。l.actionPerformed(e);/添加监听器的方法public void addActionListener(ActionListener l) actionListeners.add(l);/监听器接口interface ActionListener public void actionPerformed(ActionEvent e);/监听类class MyActionListener implements ActionListener public void actionPerformed(ActionEvent e) System.out.println(button pressed!);/监听类class MyActionListener2 implements ActionListener public void actionPerformed(ActionEvent e) System.out.println(button pressed 2!);/事件类

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论