|
直接贴代码吧,刚刚学C#没多久,不知道算不算是C#的观察者模式。。。各位多多指教啊!
将那些错误挖出来。- class Subscriber
- {
- private Subject subject ;
- public Subject Subject
- {
- get { return this.subject; }
- set { this.subject = (Subject)value; }
- }
- public Subscriber()
- {
- subject = new Subject();
- this.Subject.Public +=new Subject.PublicerEventHandler(Subject_Public);
- }
- //事件处理方法
- private void Subject_Public(object sender, EventArgs e)
- {
- System.Console.WriteLine("订阅者事件被引发!");
- }
- }
复制代码-
- class Publicer
- {
- public void observer(Object sender ,EventArgs e)
- {
- System.Console.WriteLine("---------observer start------------");
- Subscriber sub = new Subscriber();
- sub.Subject.PublicerEvent();
- }
- }
复制代码-
- class Subject
- {
- public delegate void PublicerEventHandler(object sender , EventArgs e);
- public virtual event PublicerEventHandler Public;
- //委托的事件处理方法
- public void PublicerEvent()
- {
- if(this.Public != null)
- this.Public(this,EventArgs.Empty);
- }
- }
复制代码 |
|