




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、青 島 理 工 大 學(xué)課程實(shí)驗(yàn)報(bào)告課程名稱軟件設(shè)計(jì)與體系結(jié)構(gòu)班級(jí)軟件132實(shí)驗(yàn)日期2015.06.02姓名田清華學(xué)號(hào)201307208實(shí)驗(yàn)成績(jī)實(shí)驗(yàn)名稱觀察者模式實(shí)驗(yàn)實(shí)驗(yàn)?zāi)康募耙螅?)熟悉觀察者模式的核心思想;(2)掌握觀察者模式解決問(wèn)題的方法。實(shí)驗(yàn)環(huán)境Win7, VS2010實(shí)驗(yàn)內(nèi)容1. 定義內(nèi)部數(shù)據(jù)列表(1)內(nèi)部數(shù)據(jù)列表提供數(shù)據(jù);(2) 每個(gè)數(shù)據(jù)列表項(xiàng)對(duì)應(yīng)界面表格中的一行。2. 定義界面控件DataGridview3. 利用觀察者模式實(shí)現(xiàn)內(nèi)部存儲(chǔ)的數(shù)據(jù)列表數(shù)據(jù)發(fā)生變化時(shí),自動(dòng)更新DataGridview中的內(nèi)容。算法描述及實(shí)驗(yàn)步驟1. 創(chuàng)建model類student2. 創(chuàng)建抽象目標(biāo)類da
2、ta3. 創(chuàng)建具體目標(biāo)類concretedata4. 創(chuàng)建觀察者類DataGridview5. 類圖:1 / 7調(diào)試過(guò)程及實(shí)驗(yàn)結(jié)果總結(jié)1. 觀察者模式可以實(shí)現(xiàn)表示層和數(shù)據(jù)邏輯層的分離,定義了穩(wěn)定的消息更新傳遞機(jī)制,并抽象了更新接口,是得可以有各種各樣不用的表示層充當(dāng)具體觀察者角色。2. 在觀察者目標(biāo)和觀察者之間建立一個(gè)抽象的耦合,觀察目標(biāo)只需要維持一個(gè)抽像觀察者的集合,無(wú)需了解其具體觀察者。3. 在C#中實(shí)現(xiàn)Observer模式可以采用更有效的方法-.NET中提供了delegate,委托可以看作是一個(gè)穩(wěn)定的接口(充當(dāng)抽象觀察者角色)。附錄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); /實(shí)現(xiàn)通知方法 public override void Notify() int i = 0; foreach(object obj in gridviewList) Console.Write("通知第0個(gè)觀察者:",+i);
8、(DataGridview)obj).UpDate(this); /修改數(shù)據(jù) 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("創(chuàng)建一個(gè)DataGridview"); public void UpDate(data data) this.data = data; Console.WriteLine("數(shù)據(jù)改變!"); 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添加數(shù)據(jù)"); for (int i = 0; i <3; i+) stu = new student("
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025-2026學(xué)年漣水縣三上數(shù)學(xué)期末達(dá)標(biāo)檢測(cè)模擬試題含解析
- 2025-2026學(xué)年江蘇省南京市鼓樓區(qū)鳳凰花園城小學(xué)數(shù)學(xué)三上期末綜合測(cè)試試題含解析
- 2025-2026學(xué)年福建省漳州市高新技術(shù)產(chǎn)業(yè)開發(fā)區(qū)數(shù)學(xué)三上期末監(jiān)測(cè)模擬試題含解析
- 2024年金華市義烏市三上數(shù)學(xué)期末考試試題含解析
- 人教新課標(biāo)五年級(jí)品德與社會(huì)下冊(cè)-生活中的快樂(lè)課件
- 棕色中國(guó)風(fēng)項(xiàng)羽劉邦
- 2025衛(wèi)生資格考試中的倫理道德問(wèn)題及試題與答案
- 執(zhí)業(yè)護(hù)士考試臨床技巧解析與考生真實(shí)體驗(yàn)分享的反思試題及答案
- 2025年經(jīng)濟(jì)法概論試題全面分析試題及答案
- 2025年醫(yī)師考試個(gè)人特色學(xué)習(xí)方法與小組學(xué)習(xí)的優(yōu)勢(shì)分析試題及答案
- 2025年入團(tuán)考試一覽無(wú)遺試題及答案
- 2025年《養(yǎng)老護(hù)理員》考試模擬練習(xí)題及答案
- 公司檔案及文件管理制度
- 2025年四川筠連縣國(guó)有資本投資運(yùn)營(yíng)有限公司招聘筆試參考題庫(kù)含答案解析
- 2024年貴州遵義公開招聘社區(qū)工作者考試試題答案解析
- 2025年全國(guó)低壓電工證(復(fù)審)考試筆試試題(300題)含答案
- 2025至2030中國(guó)注射用重組人腦利鈉肽行業(yè)運(yùn)行態(tài)勢(shì)及未來(lái)趨勢(shì)研究報(bào)告
- 文言常識(shí)測(cè)試題及答案
- 入團(tuán)考試測(cè)試題及答案
- 中班早期閱讀《跑跑鎮(zhèn)》課件
- 14.第十四周 繃緊“防震弦”奏響“安全曲”
評(píng)論
0/150
提交評(píng)論