c#中观查者模式实现(委托版)
2008-11-14 00:18
274 查看
using System;
using System.Collections.Generic;
using System.Text;
namespace ObserverDemo
{
class 观查者模式委托版
{
public static void Main(string[] args)
{
BankAccount count = new BankAccount();
count.WithDrawEvent += new BankAccount.WithDrawEventHandler( new Email().Update);
count.WithDraw(50);
Console.Read();
}
}
abstract class ISubject
{
public delegate void WithDrawEventHandler(object sender, UserMsgInfoEventArgs args);
public event WithDrawEventHandler WithDrawEvent;
public void OnWithDrawEvent(UserMsgInfoEventArgs args)
{
if (this.WithDrawEvent != null)
{
this.WithDrawEvent(this, args);
}
}
}
class BankAccount : ISubject
{
public int money = 90 ;
public void WithDraw( int money )
{
this.money -= money;
OnWithDrawEvent(new UserMsgInfoEventArgs(string.Format("取走{0}钱,还有{1}钱", money, this.money)));
}
}
class UserMsgInfoEventArgs : EventArgs
{
//传送信息
public string sendinfo;
public UserMsgInfoEventArgs(string info)
{
this.sendinfo = info;
}
}
class Email
{
public void Update(object sender, UserMsgInfoEventArgs args)
{
Console.WriteLine( args.sendinfo );
}
}
}
using System.Collections.Generic;
using System.Text;
namespace ObserverDemo
{
class 观查者模式委托版
{
public static void Main(string[] args)
{
BankAccount count = new BankAccount();
count.WithDrawEvent += new BankAccount.WithDrawEventHandler( new Email().Update);
count.WithDraw(50);
Console.Read();
}
}
abstract class ISubject
{
public delegate void WithDrawEventHandler(object sender, UserMsgInfoEventArgs args);
public event WithDrawEventHandler WithDrawEvent;
public void OnWithDrawEvent(UserMsgInfoEventArgs args)
{
if (this.WithDrawEvent != null)
{
this.WithDrawEvent(this, args);
}
}
}
class BankAccount : ISubject
{
public int money = 90 ;
public void WithDraw( int money )
{
this.money -= money;
OnWithDrawEvent(new UserMsgInfoEventArgs(string.Format("取走{0}钱,还有{1}钱", money, this.money)));
}
}
class UserMsgInfoEventArgs : EventArgs
{
//传送信息
public string sendinfo;
public UserMsgInfoEventArgs(string info)
{
this.sendinfo = info;
}
}
class Email
{
public void Update(object sender, UserMsgInfoEventArgs args)
{
Console.WriteLine( args.sendinfo );
}
}
}
相关文章推荐
- 建造者模式的C#实现
- 利用C#实现标准的Dispose模式
- 用C#实现MVC(Model View Control)模式介绍
- c#单例(Singleton)模式实现
- 设计模式-基于C#的工程化实现及扩展 (目录)
- C#中标准Dispose模式的实现(转载)
- 在C#中使用装饰器模式和扩展方法实现Fluent Interface
- C#中标准Dispose模式的实现
- C++ c# 分别实现单件模式
- 《模式——工程化实现及扩展》(设计模式C# 版)《连贯接口 Fluent Interface》——“自我检验"
- 解析C#设计模式编程中适配器模式的实现
- c#观察者模式的一般实现
- 设计模式之C#实现---Builder
- C/S模式在线升级之C#一种实现(一)
- C#实现的基于单件模式的ADO.NET数据库
- C#的单例模式实现
- 静态构造函数与Singleton模式在C#中的实现
- 设计模式(基于c#的工程化实现及扩展)文摘
- 设计模式之C#实现---Builder
- 从C#的Singleton设计模式实现看.NET Framework特性对开发者的重要性