2022年设计模式之observer模式参照 .pdf
《2022年设计模式之observer模式参照 .pdf》由会员分享,可在线阅读,更多相关《2022年设计模式之observer模式参照 .pdf(7页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、设计模式总章:设计模式就是把简单的问题复杂化,带来的好处是系统的可扩展性。用了设计模式以后:复杂度增加开发成本增加维护成本降低设计模式的基本原则是,添加,而不是修改。1.什么叫分析?(确定需求是什么)分析就是要知道我要做什么。确定系统实现什么样的功能。2.什么叫设计?(实现需求)设计就是我要怎么实现。设计就是多种实现的手段当中选取一个。实现某个功能是使用什么样的方式。用一个接口来封装一系列共同具有的特点。各个监听类都去实现这个接口。在被监听的类上加上addxxxlistener 方法。awt 当中的事件:事件源可以是,buttion,textField,textArea 事件是:ActionE
2、vent 相对应的监听器是actionlistener。通过配置文件来管理observer。配置文件:文件名:observer.properties 文件的内容:observers=com.bjsxt.dp.observer.Dad,com.bjsxt.dp.observer.GrandFather,com.bjsxt.dp.observer.Dog 访问当前配置的文件的main 方法:packagecom.bjsxt.dp.observer;importjava.io.IOException;名师资料总结-精品资料欢迎下载-名师精心整理-第 1 页,共 7 页 -importjava.uti
3、l.ArrayList;importjava.util.List;importjava.util.Properties;/事件类classWakenUpEvent privatelongtime;private String loc;private Child source;/事件方法publicWakenUpEvent(long time,String loc,Child source)super();this.time=time;this.loc =loc;this.source=source;publiclonggetTime()returntime;publicvoidsetTime(
4、long time)this.time=time;publicStringgetLoc()returnloc;publicvoidsetLoc(String loc)this.loc =loc;public Child getSource()returnsource;publicvoidsetSource(Child source)this.source=source;/被监听类。class Child implements Runnable private ListwakenUpListeners=new ArrayList();publicvoidaddWakenUpListener(Wa
5、kenUpListener l)wakenUpListeners.add(l);名师资料总结-精品资料欢迎下载-名师精心整理-第 2 页,共 7 页 -voidwakeUp()for(int i=0;iwakenUpListeners.size();i+)WakenUpListener l=wakenUpListeners.get(i);l.ActionToWakenUp(new WakenUpEvent(System.currentTimeMillis(),bed,this);publicvoid run()try Thread.sleep(5000);catch(InterruptedEx
6、ception e)e.printStackTrace();this.wakeUp();/监听类class Dad implementsWakenUpListener publicvoidActionToWakenUp(WakenUpEventwakenUpEvent)System.out.println(feed child);/监听类classGrandFatherimplementsWakenUpListener publicvoidActionToWakenUp(WakenUpEventwakenUpEvent)System.out.println(hug child);/监听类cla
7、ss Dog implementsWakenUpListener 名师资料总结-精品资料欢迎下载-名师精心整理-第 3 页,共 7 页 -publicvoidActionToWakenUp(WakenUpEvent arg0)System.out.println(wangwang.);/监听接口interfaceWakenUpListener publicvoidActionToWakenUp(WakenUpEventwakenUpEvent);/主类publicclass Test publicstaticvoid main(String args)Child c=new Child();/
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 2022年设计模式之observer模式参照 2022 设计 模式 observer 参照
限制150内