C#设计模式——观察者模式!
2016-12-05 11:25
239 查看
观察者模式是使用频率非常非常高的设计模式,在MVC框架中也会经常用到,重中之重,必须掌握!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ObservePingPongBall
{
public interface IObserve
{
void observer(PlayCry playcry);//定义接口方法,将要观察的目标传入方法中
}
}
运行结果:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ObservePingPongBall { public class Customer:IObserve { public string name { get; set; }//定义事件的对象,封装属性 public Customer(string name) { this.name = name; } public void observer(PlayCry playcry) { Console.WriteLine("{0}"+"在观众席看到{1}"+"对她的孩子{2}"+"说,下面的每个人都被{3}"+"{4}过。",name,playcry.name1,playcry.name2,playcry.name3,playcry.action); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ObservePingPongBall { public class RealPlayCry:PlayCry { public RealPlayCry(string name1,string name2,string name3,string action):base(name1,name2,name3,action) { }//子类,用于里氏替换原则使用参数 } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ObservePingPongBall { public abstract class PlayCry { private List<IObserve> ObserveList = new List<IObserve>();//定义订阅者集合 public string name1 { set; get; } public string name2 { set; get; } public string name3 { set; get; } public string action{ set; get; } public PlayCry(string name1, string name2,string name3, string action)//定义目标属性 { this.name1 = name1; this.name2 = name2; this.name3 = name3; this.action = action; } public void ObserveAdd(IObserve ob)//添加订阅者 { ObserveList.Add(ob); } public void UpDate()//更新订阅者,将订阅人取出集合,并放入接口方法中 { foreach (IObserve ob in ObserveList) { if (ob != null) { ob.observer(this); } } } } }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ObservePingPongBall
{
public interface IObserve
{
void observer(PlayCry playcry);//定义接口方法,将要观察的目标传入方法中
}
}
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ObservePingPongBall { class Program { static void Main(string[] args) { PlayCry playcrying = new RealPlayCry("张怡宁","xxx","妈妈","打哭");//历史替换,添加具体参数 playcrying.ObserveAdd(new Customer("观众"));//添加具体对象 playcrying.UpDate();//调用,取出订阅 Console.ReadLine();//通过观察者读取 Console.ReadKey(); } } }
运行结果:
相关文章推荐
- C#设计模式之十六观察者模式(Observer Pattern)【行为型】
- c#设计模式-观察者模式
- 【C#设计模式-观察者模式】
- [导入]C#面向对象设计模式纵横谈(19):(行为型模式) Observer 观察者模式.zip(10.27 MB)
- C#设计模式-观察者observer模式实现
- C#设计模式之20——观察者模式
- C#中利用代理实现观察者设计模式
- C# 设计模式系列教程-观察者模式
- C#设计模式——观察者模式(Observer Pattern)
- C# 设计模式之观察者模式
- 面试习题之设计模式 C#观察者模式(猫叫老鼠惊走主人醒)
- 乐在其中设计模式(C#) - 观察者模式(Observer Pattern)
- C#设计模式-观察者模式(Observer Pattern)[FreeDownload]
- c# 设计模式:观察者模式
- 乐在其中设计模式(C#) - 观察者模式(Observer Pattern)
- C#设计模式(4)——观察者模式(Observer Pattern)
- 乐在其中设计模式(C#) - 观察者模式(Observer Pattern)
- C#中利用代理实现观察者设计模式
- C# 设计模式-观察者(Observer)
- C#设计模式之Observer观察者模式解决牛顿童鞋成绩问题示例