报社更新报纸内容,读者获取更新
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DesignPytternDemo
{
/// <summary>
/// 利用事件实现观察者
/// </summary>
/// <param name="sender"></param>
public delegate void UpdatePaper(object sender);
/// <summary>
/// 报社
/// </summary>
public class Paper
{
public event UpdatePaper OnChange;
/// <summary>
/// 更新报纸了
/// </summary>
public void ChangeData()
{
if (null != OnChange)
{
OnChange.Invoke("new data");
}
}
}
/// <summary>
/// 读者
/// </summary>
public class Reader
{
public void ReadNews(object news)
{
Console.Write(news.ToString());
}
}
}
Paper p = new Paper();
Reader reader1 = new Reader();
Reader reader2 = new Reader();
p.OnChange += reader1.ReadNews;
p.OnChange += reader2.ReadNews;
p.ChangeData();
Console.ReadLine();