设计模式之1 observer模式

上传人:fgh****35 文档编号:172625668 上传时间:2022-12-05 格式:DOCX 页数:7 大小:25.56KB
返回 下载 相关 举报
设计模式之1 observer模式_第1页
第1页 / 共7页
设计模式之1 observer模式_第2页
第2页 / 共7页
设计模式之1 observer模式_第3页
第3页 / 共7页
点击查看更多>>
资源描述
设计模式总章:设计模式就是把简单的问题复杂化,带来的好处是系统的可扩展性。用了设计模式以后:复杂度增加开发成本增加维护成本降低设计模式的基本原则是,添加,而不是修改。1. 什么叫分析?(确定需求是什么)分析就是要知道我要做什么。确定系统实现什么样的功能。2. 什么叫设计?(实现需求)设计就是我要怎么实现。设计就是多种实现的手段当中选取一个。实现某个功能是使用什么样的方式。用一个接口来封装一系列共同具有的特点。各个监听类都去实现这个接口。在被监听的类上加上addxxxlistener方法。awt当中的事件:事件源可以是,buttion,textField,textArea事件是:ActionEvent相对应的监听器是actionlistener。通过配置文件来管理observer。配置文件:文件名:observer.properties文件的内容: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/observer.properties); 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!);/事件类class ActionEvent long when;Object source;/事件方法public ActionEvent(long when, Object source) super();this.when = when;this.source = source;public long getWhen() return when;public Object getSource() return source;
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 商业管理 > 营销创新


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!