观察者模式实验.doc

观察者模式实验.doc

ID:58631450

大小:98.50 KB

页数:5页

时间:2020-10-17

观察者模式实验.doc_第1页
观察者模式实验.doc_第2页
观察者模式实验.doc_第3页
观察者模式实验.doc_第4页
观察者模式实验.doc_第5页
资源描述:

《观察者模式实验.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库

1、青岛理工大学课程实验报告课程名称软件设计与体系结构班级实验日期2015.06.02姓名学号实验成绩实验名称观察者模式实验实验目的及要求(1)熟悉观察者模式的核心思想;(2)掌握观察者模式解决问题的方法。实验环境Win7,VS2010实验内容1.定义内部数据列表(1)内部数据列表提供数据;(2)每个数据列表项对应界面表格中的一行。2.定义界面控件DataGridview3.利用观察者模式实现内部存储的数据列表数据发生变化时,自动更新DataGridview中的内容。算法描述及实验步骤1.新建项目2.依次添加抽象目标类InteriorData,具体目标类

2、ConcreteInteriorData,抽象观察者类IObserver,具体观察者类DataGridview3.依次为4个创建的类和主程序中添加代码4.调试并运行程序调试过程及实验结果总结观察者模式的主要内容其实就是为抽象目标对象创建具体目标类和观察者类,通过在具体目标类中使用foreach语句遍历观察者的集合并调用每个观察者的响应方法来实现在目标类发生变化时所有观察者类的实例一同跟着变化,用于实现多个对象之间联动。InteriorData.cs附录usingSystem;usingSystem.Collections.Generic;usingS

3、ystem.Linq;usingSystem.Text;namespaceObserver{abstractclassInteriorData{protectedstringdataList;protectedListdata=newList();publicvoidSetDataList(stringdataList){this.dataList=dataList;}publicstringGetDataList(){returnthis.dataList;}publicvoidInput(IObserv

4、erobs){Console.WriteLine("把{0}写入{1}数据表",obs.Name,this.dataList);data.Add(obs);}publicvoidDelete(IObserverobs){Console.WriteLine("把{0}从{1}数据表删除",obs.Name,this.dataList);data.Remove(obs);}publicabstractvoidNotifyObserver(stringname);}}ConcreteInteriorData.csusingSystem;usingSyste

5、m.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceObserver{classConcreteInteriorData:InteriorData{publicConcreteInteriorData(stringdataList){Console.WriteLine("{0}数据表创建成功",dataList);Console.WriteLine("--------------------------------");this.dataList=dataList;}pub

6、licoverridevoidNotifyObserver(stringname){Console.WriteLine("{0}数据表更新通知,表内{1}被修改",this.dataList,name);foreach(objectobsindata){if(!((IObserver)obs).Name.Equals(name)){((IObserver)obs).ViewChange();}}//thrownewNotImplementedException();}}}IObserver.csusingSystem;usingSystem.Coll

7、ections.Generic;usingSystem.Linq;usingSystem.Text;namespaceObserver{interfaceIObserver{stringName{get;set;}voidViewChange();voidDataChange(InteriorDataacc);}}DataGridview.csusingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceObserver{classData

8、Gridview:IObserver{privatestringname;publicDataGridvie

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。