实现了观察者模式的用户控件。

本文介绍了一个结合观察者模式与用户控件的实例。通过定义接口和实现类,创建了一个能够响应数据变化并更新UI的ChartPanel用户控件。此外,还提供了一个简单的窗体应用程序来演示如何使用该控件。

在学习 IssueVision 过程中,提到过观察者模式的用户控件(ChartPanel?),我按自己的设计思路也做了个不伦不类的观察者模式+用户控件,记录下来。

首先,建立一个观察者模式的接口 IObserver.cs

 

None.gifusing System;
None.gif
using System.Collections.Generic;
None.gif
using System.Text;
None.gif
None.gif
namespace IssueVision
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif   
public interface  IObserver  //接口声明
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif       
void NotifyDataChange(ChartData nChartData); //事件,当数据改变时
ExpandedSubBlockEnd.gif
    }

InBlock.gif
InBlock.gif    
//ChartData 类,表示三类事务,open,work,close
InBlock.gif
    public class ChartData
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private int n_open;
InBlock.gif        
private int n_work;
InBlock.gif        
private int n_close;
InBlock.gif        
private int n_max;
InBlock.gif
InBlock.gif        
public int open
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return n_open;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                n_open 
= value > 0 ? value : 0;
InBlock.gif                n_max 
= value > n_max ? value : n_max;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public int work
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return n_work;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                n_work 
= value > 0 ? value : 0;
InBlock.gif                n_max 
= value > n_max ? value : n_max;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public int close
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return n_close;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                n_close 
= value > 0 ? value : 0;
InBlock.gif                n_max 
= value > n_max ? value : n_max;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public int max
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return n_max;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                n_max 
= value > 0 ? value : 0;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


 

接着声明主体 ISubject.cs

None.gifusing System;
None.gif
using System.Collections.Generic;
None.gif
using System.Text;
None.gif
None.gif
namespace IssueVision
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
public interface  ISubject  //声明主体接口
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif        
// 需实现这两个,实现注册和反注册
InBlock.gif
        void Register(IObserver anObject);
InBlock.gif        
void UnRegister(IObserver anObject);
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


 

现在新建一个用户控件,放上一个 lbltitle 表示标题,三个 label 分别表示 open , work , close,再放三个 progressbar 表示数量,因为懒得画条形图,所以直接用 progressbar , 不过,用 progressbar 很难看倒是真的:(。,用户控件叫 ChartPanel.cs,代码如下

 

None.gifusing System;
None.gif
using System.Collections.Generic;
None.gif
using System.ComponentModel;
None.gif
using System.Drawing;
None.gif
using System.Data;
None.gif
using System.Text;
None.gif
using System.Windows.Forms;
None.gif
None.gif
namespace IssueVision.UserControls
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
public partial class ChartPanel : UserControl , IObserver   //指定 IObserver ,表示实现它
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif        
private string m_title;
InBlock.gif        
private int n_max;
InBlock.gif        
private int n_open;
InBlock.gif        
private int n_working;
InBlock.gif        
private int n_closed;
InBlock.gif
InBlock.gif        [Description(
"图表标题")]
InBlock.gif        
public string Title
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return m_title;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                m_title 
= value;
InBlock.gif                Invalidate();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [Description(
"最大数量")]
InBlock.gif        
public int NMAX
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return n_max;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                n_max 
= value > 0 ? value : 0;
InBlock.gif                Invalidate();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [Description(
"打开任务数量")]
InBlock.gif        
public int NOpen
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return n_open;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                n_open 
= value > 0 ? value : 0;
InBlock.gif                Invalidate();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [Description(
"进行的任务数量")]
InBlock.gif        
public int NWorking
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return n_working;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                n_working 
= value > 0 ? value : 0;
InBlock.gif                Invalidate();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [Description(
"关闭的任务数量")]
InBlock.gif        
public int NClosed
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return n_closed;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                n_closed 
= value > 0 ? value : 0;
InBlock.gif                Invalidate();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void NotifyDataChange(ChartData nChartData)  //实现观察者的 NotifyDataChage,表示当接受到数据变更时应用的动作。
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            
//
InBlock.gif
            n_max = nChartData.max;
InBlock.gif            n_open 
= nChartData.open;
InBlock.gif            n_working 
= nChartData.work;
InBlock.gif            n_closed 
= nChartData.close;
InBlock.gif            Invalidate();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public ChartPanel()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            InitializeComponent();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void ChartPanel_Paint(object sender, PaintEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.lbTitle.Text = m_title;
InBlock.gif            pbOpen.Minimum 
= 0;
InBlock.gif            pbWorking.Minimum 
= 0;
InBlock.gif            pbClosed.Minimum 
= 0;
InBlock.gif
InBlock.gif            pbOpen.Maximum 
= n_max;
InBlock.gif            pbWorking.Maximum 
= n_max;
InBlock.gif            pbClosed.Maximum 
= n_max;
InBlock.gif
InBlock.gif            pbOpen.Value 
= n_open > n_max ? n_max : n_open;
InBlock.gif            pbWorking.Value 
= n_working > n_max ? n_max : n_working;
InBlock.gif            pbClosed.Value 
= n_closed > n_max ? n_max : n_closed;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


下面是设计器形成的,ChartPanel的设计代码,一并留下

 

None.gifnamespace IssueVision.UserControls
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    partial 
class ChartPanel
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary> 
InBlock.gif        
/// 必需的设计器变量。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private System.ComponentModel.IContainer components = null;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary> 
InBlock.gif        
/// 清理所有正在使用的资源。
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>

InBlock.gif        protected override void Dispose(bool disposing)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (disposing && (components != null))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                components.Dispose();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
base.Dispose(disposing);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
组件设计器生成的代码#region 组件设计器生成的代码
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary> 
InBlock.gif        
/// 设计器支持所需的方法 - 不要
InBlock.gif        
/// 使用代码编辑器修改此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            System.ComponentModel.ComponentResourceManager resources 
= new System.ComponentModel.ComponentResourceManager(typeof(ChartPanel));
InBlock.gif            
this.pnlTop = new System.Windows.Forms.Panel();
InBlock.gif            
this.lbTitle = new System.Windows.Forms.Label();
InBlock.gif            
this.lblOpen = new System.Windows.Forms.Label();
InBlock.gif            
this.lblWorking = new System.Windows.Forms.Label();
InBlock.gif            
this.lblclosed = new System.Windows.Forms.Label();
InBlock.gif            
this.pbOpen = new System.Windows.Forms.ProgressBar();
InBlock.gif            
this.pbWorking = new System.Windows.Forms.ProgressBar();
InBlock.gif            
this.pbClosed = new System.Windows.Forms.ProgressBar();
InBlock.gif            
this.pnlTop.SuspendLayout();
InBlock.gif            
this.SuspendLayout();
InBlock.gif            
// 
InBlock.gif            
// pnlTop
InBlock.gif            
// 
InBlock.gif
            this.pnlTop.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("pnlTop.BackgroundImage")));
InBlock.gif            
this.pnlTop.Controls.Add(this.lbTitle);
InBlock.gif            
this.pnlTop.Dock = System.Windows.Forms.DockStyle.Top;
InBlock.gif            
this.pnlTop.Location = new System.Drawing.Point(00);
InBlock.gif            
this.pnlTop.Name = "pnlTop";
InBlock.gif            
this.pnlTop.Size = new System.Drawing.Size(20516);
InBlock.gif            
this.pnlTop.TabIndex = 0;
InBlock.gif            
// 
InBlock.gif            
// lbTitle
InBlock.gif            
// 
InBlock.gif
            this.lbTitle.AutoSize = true;
InBlock.gif            
this.lbTitle.BackColor = System.Drawing.Color.Transparent;
InBlock.gif            
this.lbTitle.Dock = System.Windows.Forms.DockStyle.Fill;
InBlock.gif            
this.lbTitle.Location = new System.Drawing.Point(00);
InBlock.gif            
this.lbTitle.Name = "lbTitle";
InBlock.gif            
this.lbTitle.Size = new System.Drawing.Size(7712);
InBlock.gif            
this.lbTitle.TabIndex = 0;
InBlock.gif            
this.lbTitle.Text = "标题标题标题";
InBlock.gif            
this.lbTitle.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
InBlock.gif            
// 
InBlock.gif            
// lblOpen
InBlock.gif            
// 
InBlock.gif
            this.lblOpen.AutoSize = true;
InBlock.gif            
this.lblOpen.Location = new System.Drawing.Point(939);
InBlock.gif            
this.lblOpen.Name = "lblOpen";
InBlock.gif            
this.lblOpen.Size = new System.Drawing.Size(2912);
InBlock.gif            
this.lblOpen.TabIndex = 1;
InBlock.gif            
this.lblOpen.Text = "打开";
InBlock.gif            
// 
InBlock.gif            
// lblWorking
InBlock.gif            
// 
InBlock.gif
            this.lblWorking.AutoSize = true;
InBlock.gif            
this.lblWorking.Location = new System.Drawing.Point(977);
InBlock.gif            
this.lblWorking.Name = "lblWorking";
InBlock.gif            
this.lblWorking.Size = new System.Drawing.Size(2912);
InBlock.gif            
this.lblWorking.TabIndex = 2;
InBlock.gif            
this.lblWorking.Text = "进行";
InBlock.gif            
// 
InBlock.gif            
// lblclosed
InBlock.gif            
// 
InBlock.gif
            this.lblclosed.AutoSize = true;
InBlock.gif            
this.lblclosed.Location = new System.Drawing.Point(9112);
InBlock.gif            
this.lblclosed.Name = "lblclosed";
InBlock.gif            
this.lblclosed.Size = new System.Drawing.Size(2912);
InBlock.gif            
this.lblclosed.TabIndex = 3;
InBlock.gif            
this.lblclosed.Text = "关闭";
InBlock.gif            
// 
InBlock.gif            
// pbOpen
InBlock.gif            
// 
InBlock.gif
            this.pbOpen.Location = new System.Drawing.Point(4428);
InBlock.gif            
this.pbOpen.Name = "pbOpen";
InBlock.gif            
this.pbOpen.Size = new System.Drawing.Size(15523);
InBlock.gif            
this.pbOpen.Step = 1;
InBlock.gif            
this.pbOpen.TabIndex = 4;
InBlock.gif            
// 
InBlock.gif            
// pbWorking
InBlock.gif            
// 
InBlock.gif
            this.pbWorking.Location = new System.Drawing.Point(4466);
InBlock.gif            
this.pbWorking.Name = "pbWorking";
InBlock.gif            
this.pbWorking.Size = new System.Drawing.Size(15523);
InBlock.gif            
this.pbWorking.TabIndex = 5;
InBlock.gif            
// 
InBlock.gif            
// pbClosed
InBlock.gif            
// 
InBlock.gif
            this.pbClosed.Location = new System.Drawing.Point(45101);
InBlock.gif            
this.pbClosed.Name = "pbClosed";
InBlock.gif            
this.pbClosed.Size = new System.Drawing.Size(15423);
InBlock.gif            
this.pbClosed.TabIndex = 6;
InBlock.gif            
// 
InBlock.gif            
// ChartPanel
InBlock.gif            
// 
InBlock.gif
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
InBlock.gif            
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
InBlock.gif            
this.Controls.Add(this.pbClosed);
InBlock.gif            
this.Controls.Add(this.pbWorking);
InBlock.gif            
this.Controls.Add(this.pbOpen);
InBlock.gif            
this.Controls.Add(this.lblclosed);
InBlock.gif            
this.Controls.Add(this.lblWorking);
InBlock.gif            
this.Controls.Add(this.lblOpen);
InBlock.gif            
this.Controls.Add(this.pnlTop);
InBlock.gif            
this.Name = "ChartPanel";
InBlock.gif            
this.Size = new System.Drawing.Size(205135);
InBlock.gif            
this.Paint += new System.Windows.Forms.PaintEventHandler(this.ChartPanel_Paint);
InBlock.gif            
this.pnlTop.ResumeLayout(false);
InBlock.gif            
this.pnlTop.PerformLayout();
InBlock.gif            
this.ResumeLayout(false);
InBlock.gif            
this.PerformLayout();
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
private System.Windows.Forms.Panel pnlTop;
InBlock.gif        
private System.Windows.Forms.Label lbTitle;
InBlock.gif        
private System.Windows.Forms.Label lblOpen;
InBlock.gif        
private System.Windows.Forms.Label lblWorking;
InBlock.gif        
private System.Windows.Forms.Label lblclosed;
InBlock.gif        
private System.Windows.Forms.ProgressBar pbOpen;
InBlock.gif        
private System.Windows.Forms.ProgressBar pbWorking;
InBlock.gif        
private System.Windows.Forms.ProgressBar pbClosed;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

新建一个窗体,放上刚才的用户控件 chartpanel1,放置 textbox2,3,4  ,放一个按钮。

 

None.gif    public class SubjectImpl:ISubject //被观察者的实现
ExpandedBlockStart.gifContractedBlock.gif
    dot.gif{
InBlock.gif        
protected Hashtable _observerContaind = new Hashtable();
InBlock.gif
InBlock.gif        
public void Register(IObserver anObserver)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            _observerContaind.Add(anObserver,anObserver);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void UnRegister(IObserver anObserver)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            _observerContaind.Remove(anObserver);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void NotifyObservers(ChartData n_data)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
foreach (IObserver anObserver in _observerContaind.Keys)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                anObserver.NotifyDataChange(n_data);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif
None.gif    
public class MyData :SubjectImpl
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
private ChartData _cd = new ChartData();
InBlock.gif
InBlock.gif        
public ChartData CD
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _cd;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                _cd 
= value;
InBlock.gif                
base.NotifyObservers(_cd);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedBlockEnd.gif    }

None.gif

 

 

None.gif        private void button1_Click(object sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            ChartData mycd
=new ChartData();
InBlock.gif            mycd.open
=Convert.ToInt16(textBox2.Text);
InBlock.gif            mycd.work
=Convert.ToInt16(textBox3.Text);
InBlock.gif            mycd.close
=Convert.ToInt16(textBox4.Text);
InBlock.gif            MyData md 
= new MyData();
InBlock.gif            md.Register(chartPanel1);
InBlock.gif            md.CD 
= mycd;
InBlock.gif
ExpandedBlockEnd.gif        }

None.gif


 


 

呼,终于完了,点此 button 后,chartpanel 将会更改,不过,这么做会不会太麻烦了?

转载于:https://www.cnblogs.com/qufo/archive/2007/08/14/855595.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值