06-观察者模式实验参考模板_第1页
06-观察者模式实验参考模板_第2页
06-观察者模式实验参考模板_第3页
06-观察者模式实验参考模板_第4页
06-观察者模式实验参考模板_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

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

文档简介

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

2、ta3. 创建具体目标类concretedata4. 创建观察者类DataGridview5. 类图:1 / 7调试过程及实验结果总结1. 观察者模式可以实现表示层和数据逻辑层的分离,定义了稳定的消息更新传递机制,并抽象了更新接口,是得可以有各种各样不用的表示层充当具体观察者角色。2. 在观察者目标和观察者之间建立一个抽象的耦合,观察目标只需要维持一个抽像观察者的集合,无需了解其具体观察者。3. 在C#中实现Observer模式可以采用更有效的方法-.NET中提供了delegate,委托可以看作是一个稳定的接口(充当抽象观察者角色)。附录Student.cs:using System;usin

3、g System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace shiyan_6 class student private string name; private string sex; private int age; public student(string name,string sex,int age) = name; this.sex = sex; this.age = age; public string Name

4、 get return name; set name = value; public string Sex get return sex; set sex = value; public int Age get return age; set age = value; Data.cs:using System;using System.Collections;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace shiyan_6 ab

5、stract class data protected List<student> studentList = new List<student>(); protected ArrayList gridviewList = new ArrayList(); /添加观察者 public abstract void Attach(DataGridview dataGridview); /删除观察者 public abstract void Datach(DataGridview dataGridview); public abstract void AddStudent(s

6、tudent stu); public abstract void DeleteStudent(int loc); public abstract void Display(); /通知方法 public abstract void Notify(); concreteData.cs:using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace shiyan_6 class concreteData:data pub

7、lic override void Attach(DataGridview dataGridview) gridviewList.Add(dataGridview); public override void Datach(DataGridview dataGridview) gridviewList.Remove(dataGridview); /实现通知方法 public override void Notify() int i = 0; foreach(object obj in gridviewList) Console.Write("通知第0个观察者:",+i);

8、(DataGridview)obj).UpDate(this); /修改数据 public override void AddStudent(student stu) studentList.Add(stu); Notify(); public override void DeleteStudent(int loc) studentList.RemoveAt(loc); Notify(); public override void Display() foreach (student stu in studentList) Console.WriteLine(stu.Name+" &

9、quot;+stu.Age+" "+stu.Sex); dataGridview.cs:using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace shiyan_6 class DataGridview private data data = new concreteData(); public DataGridview(data data) this.data = data; Console.

10、WriteLine("创建一个DataGridview"); public void UpDate(data data) this.data = data; Console.WriteLine("数据改变!"); this.data.Display(); Program.cs:using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace shiyan_6 class Progr

11、am static void Main(string args) data data1 = new concreteData(); student stu; DataGridview dataGridview1 = new DataGridview(data1); data1.Attach(dataGridview1); DataGridview dataGridview2= new DataGridview(data1); data1.Attach(dataGridview2); /DataGridview dataGridview3 = new DataGridview(data1); /data1.Attach(dataGridview3); Console.WriteLine("给data添加数据"); for (int i = 0; i <3; i+) stu = new student("

温馨提示

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

评论

0/150

提交评论