1.Form1.cs 数据
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace Observer
{
/// <summary>
/// 主窗体实现了接口.Subject
/// </summary>
public class Form1 : System.Windows.Forms.Form, Subject
{
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.RadioButton opRed;
private System.Windows.Forms.RadioButton opGreen;
private System.Windows.Forms.RadioButton opBlue;
private ArrayList observers;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
init();
}
private void init() {
EventHandler evh = new EventHandler (opButton_Click);
opRed.Click += evh;
opBlue.Click += evh;
opGreen.Click += evh;
observers = new ArrayList ();
//创建观察者1-ListObs:将选择中颜色增加在列表框中
ListObs lobs = new ListObs (this);//复杂:进行了两次相互调用,从而可以日后调用.
lobs.Show ();
//创建观察者2-ColObServer:将选择中颜色显示出来
ColObserver colObs = new ColObserver (this);
colObs.Show();
}
//注册该类中感兴趣的数据
public void registerInterest(Observer obs ) {
observers.Add (obs);
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.opRed = new System.Windows.Forms.RadioButton();
this.opGreen = new System.Windows.Forms.RadioButton();
this.opBlue = new System.Windows.Forms.RadioButton();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
this.opBlue,
this.opGreen,
this.opRed});
this.groupBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.groupBox1.Location = new System.Drawing.Point(32, 24);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(160, 144);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Select colors";
//
// opRed
//
this.opRed.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.opRed.ForeColor = System.Drawing.Color.Red;
this.opRed.Location = new System.Drawing.Point(32, 32);
this.opRed.Name = "opRed";
this.opRed.TabIndex = 0;
this.opRed.Text = "Red";
//
// opGreen
//
this.opGreen.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.opGreen.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(0)), ((System.Byte)(192)), ((System.Byte)(0)));
this.opGreen.Location = new System.Drawing.Point(32, 64);
this.opGreen.Name = "opGreen";
this.opGreen.Size = new System.Drawing.Size(96, 16);
this.opGreen.TabIndex = 1;
this.opGreen.Text = "Green";
//
// opBlue
//
this.opBlue.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.opBlue.ForeColor = System.Drawing.Color.Blue;
this.opBlue.Location = new System.Drawing.Point(32, 88);
this.opBlue.Name = "opBlue";
this.opBlue.Size = new System.Drawing.Size(80, 16);
this.opBlue.TabIndex = 2;
this.opBlue.Text = "Blue";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(224, 205);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.groupBox1});
this.Name = "Form1";
this.Text = "Observer demo";
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void opButton_Click(object sender, System.EventArgs e) {
RadioButton but = (RadioButton)sender;
//遍历观察者集合,并调用每个观察者的方法sendNotify
for(int i=0; i< observers.Count ; i++ ) {
Observer obs = (Observer)observers[i];
obs.sendNotify (but.Text );//将单选的文本以消息发送
}
}
}
}
2.Observer.cs 观察者接口
using System;
namespace Observer
{
/// <summary>
/// 观察者.
/// </summary>
public interface Observer {
void sendNotify(string message);
}
}
3.Subject.cs 数据接口
using System;
namespace Observer
{
/// <summary>
/// 目标.
/// </summary>
public interface Subject {
void registerInterest(Observer obs);
}
}
4.ColObserver.cs (观察者1)
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace Observer
{
/// <summary>
/// Summary description for ColObserver.
/// </summary>
public class ColObserver : System.Windows.Forms.Form, Observer {
private System.ComponentModel.Container components = null;
private Brush bBrush;
private System.Windows.Forms.PictureBox pic;
private Font fnt;
private Hashtable colors;
private string colName;
//-----
public ColObserver(Subject subj) {
InitializeComponent();
init(subj);
}
//-----
private void init(Subject subj) {
subj.registerInterest (this);
fnt = new Font("arial", 18, FontStyle.Bold);
bBrush = new SolidBrush(Color.Black);
pic.Paint+= new PaintEventHandler (paintHandler);
colors = new Hashtable ();
colors.Add("red", Color.Red );
colors.Add ("blue", Color.Blue );
colors.Add ("green", Color.Green );
colName = "";
}
//-----
public void sendNotify(string message) {
//接收消息
colName = message;
message = message.ToLower ();
Color col = (Color)colors[message];
pic.BackColor = col;
}
//-----
private void paintHandler(object sender, PaintEventArgs e) {
Graphics g = e.Graphics ;
g.DrawString(colName, fnt, bBrush, 20, 40);
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.pic = new System.Windows.Forms.PictureBox();
this.SuspendLayout();
//
// pic
//
this.pic.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.pic.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.pic.Location = new System.Drawing.Point(24, 24);
this.pic.Name = "pic";
this.pic.Size = new System.Drawing.Size(184, 152);
this.pic.TabIndex = 0;
this.pic.TabStop = false;
//
// ColObserver
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(248, 213);
this.Controls.AddRange(n
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录
C#设计模式资料整理.rar (54个子文件)
C#设计模式
创建型模式
~$$抽象工厂模式(花园).~vsd 4KB
单件模式.txt 101B
简单工厂模式(源码).txt 2KB
何时使用工厂模式.txt 125B
创建型模式小结.txt 497B
工厂方法模式.txt 274B
基础.txt 161B
C#中的克隆.txt 115B
单件创建(静态方法2_异常).txt 1023B
抽象工厂模式(花园).vsd 170KB
简单工厂模式.txt 208B
生成器模式(源码).txt 7KB
生成器模式.txt 287B
抽象工厂.txt 91B
原型模式.txt 214B
单件创建(静态方法1).txt 734B
UML中的组合.txt 27B
行为型模式
中介者模式(Mediator Pattern).txt 210B
访问者模式(Vistor Pattern).txt 64B
难度大的模式.txt 8B
迭代器模式(Iterator Pattern).txt 79B
命令模式(代码-操作与界面分离2-最好).txt 6KB
备忘录模式(Memento Pattern).txt 296B
解释器模式(Interpreter Pattern).txt 206B
命令模式(Command Pattern).txt 111B
职责链(代码-解释器).txt 7KB
职责链(Chain of Responsibility).txt 170B
状态模式(State Pattern).txt 180B
模板方法模式(Template Method Pattern).txt 195B
基础.txt 56B
观察者模式(代码1).txt 10KB
职责链(代码-帮助系统).txt 3KB
中介者模式(代码).txt 7KB
命令模式(代码-操作与界面分离).txt 4KB
策略模式(Stractegy Pattern).txt 80B
迭代器模式(代码-1).txt 1KB
访问者模式(代码).txt 7KB
观察者模式(Observer Pattern).txt 123B
顺序初始化数组.txt 322B
结构型模式
代理模式(Proxy Pattern).txt 282B
组成模式(代码).txt 9KB
结构型模式.txt 171B
装饰模式(代码-CoolButton).txt 4KB
享元模式(代码).txt 7KB
桥接模式.txt 209B
适配器模式.txt 153B
装饰模式(Decorator Pattern).txt 76B
适配器模式(代码).txt 6KB
组成模式.txt 186B
代理模式(代码).txt 2KB
总结.txt 566B
桥接模式(代码).txt 3KB
外观模式.txt 82B
享元模式(Flyweight Pattern).txt 80B
共 54 条
- 1
pingchangxinnet
- 粉丝: 1
- 资源: 70
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- HTML5小游戏【阳光高尔夫球-优秀H5小游戏合集】游戏源码分享下载 - golfblast.zip
- 二层半独栋别墅结构水电施工图结构施工图.dwg
- 数据结构,常用的数据结构
- 某三层别墅建筑施工图编号D061-三层-12.54&12.24米-施工图.dwg
- 某三层别墅施工图带效果图D060-三层-23.04&15.39米- 施工图.dwg
- 流水灯,8个灯,不同花样
- TikTokPlugin-1.39-for-rezvorck.apk
- 某三层豪华型别墅建筑施工图D059-三层-27.00&16.00米- 施工图.dwg
- 11111111111111111111
- process-data-final.c
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0