【观察者模式应用场景举例】 比如在玩极品飞车这款游戏每一个车手到达终点的时候都会在其它车手的屏幕上显示某某人以多少时间到达终点的提示其实这就是一个典型的观察者模式的应用观察者模式即是典型的双向一对多的应用场景下用一的一端来通知多的一端 【观察者模式解释】 类型行为模式 定义了一种一对多的依赖关系让多个观察者对象同时监听某一个主题对象这个主题对象的状态发生改变时会通知所有的观察者对象使它们能够自己更新自己 【观察者模式UML图】 【观察者模式JAVA代码实现】 新建抽象发布通知父类 packagecarer_subject; importjavautilArrayList; importjavautilList; importcarer_observercarer_observer; publicinterfacecarer_subject_parent{ publicvoidset_state(Stringstate_string); publicStringget_state(); publicvoidadd_observer(carer_observercarer_observer_ref); publicvoidsub_observer(carer_observercarer_observer_ref); publicvoidnotityAllCarer(); } 新建发布通知实现类 packagecarer_subject; importjavautilArrayList; importjavautilList; importcarer_observercarer_observer; publicclasscarer_subject_subimplementscarer_subject_parent{ List<carer_observer>list_observer=newArrayList<carer_observer>(); privateStringstate; publicvoidset_state(Stringstate_string){ thisstate=state_string; } publicStringget_state(){ returnstate; } publicvoidadd_observer(carer_observercarer_observer_ref){ list_observeradd(carer_observer_ref); } publicvoidsub_observer(carer_observercarer_observer_ref){ list_observerremove(carer_observer_ref); } publicvoidnotityAllCarer(){ state=到达终点; for(inti=;i<list_observersize();i++){ list_observerget(i)show_info(); } } } 新建观察者接口 packagecarer_observer; importcarer_subjectcarer_subject_parent; publicinterfacecarer_observer{ publicvoidshow_info(); } 新建观察者实现类 packagecarer_observer; importcarer_subjectcarer_subject_parent; publicclasscarer_observer_impleimplementscarer_observer{ privatecarer_subject_parentcarer_subject_parent_ref; publiccarer_subject_parentgetCarer_subject_parent_ref(){ returncarer_subject_parent_ref; } publicvoidsetCarer_subject_parent_ref( carer_subject_parentcarer_subject_parent_ref){ thiscarer_subject_parent_ref=carer_subject_parent_ref; } publiccarer_observer_imple(carer_subject_parentcarer_subject_parent_ref){ super(); thiscarer_subject_parent_ref=carer_subject_parent_ref; } publicvoidshow_info(){ Systemoutprintln(carer_subject_parent_refget_state()); } } 新建客户端运行类 packagerun_main; importcarer_observercarer_observer_imple; importcarer_subjectcarer_subject_parent; importcarer_subjectcarer_subject_sub; publicclassrun_main{ publicstaticvoidmain(String[]args){ carer_subject_parentcarer_subject_parent_ref=newcarer_subject_sub(); carer_observer_implecarer_observer_imple_ref=newcarer_observer_imple( carer_subject_parent_ref); carer_observer_implecarer_observer_imple_ref=newcarer_observer_imple( carer_subject_parent_ref); carer_observer_implecarer_observer_imple_ref=newcarer_observer_imple( carer_subject_parent_ref); carer_observer_implecarer_observer_imple_ref=newcarer_observer_imple( carer_subject_parent_ref); carer_subject_parent_refadd_observer(carer_observer_imple_ref); carer_subject_parent_refadd_observer(carer_observer_imple_ref); carer_subject_parent_refadd_observer(carer_observer_imple_ref); carer_subject_parent_refadd_observer(carer_observer_imple_ref); carer_subject_parent_refsub_observer(carer_observer_imple_ref); carer_subject_parent_refset_state(到达终点); carer_subject_parent_refnotityAllCarer(); } } 程序运行结果如下 到达终点 到达终点 到达终点 以一对多的方式进行对一方状态的改变而去通知多方观察者模式也叫发布/订阅模式一方进行发布而多方进行订阅和生活中看报纸一样你喜欢订城市晚报就会发给你 【观察者模式MyEclipse项目JAVA代码实现】 观察者模式observer示例代码点击 |