标签:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/*
观察者模式
* 当一个对象的改变需要同时改变其它对象时
* 不知具体会同时改变多少个对象时
*
*/
namespace App_MYCS.HDL_SJMS.GCZMS
{
class my_GCZMS
{
public void dy()
{
ConcreteSubject s = new ConcreteSubject();
s.Attach(new ConcreteObserver(s,"x"));
s.Attach(new ConcreteObserver(s,"y"));
s.Subjectstate = "abc";
s.Notify();
}
}
//抽像观察者
abstract class Observer
{
public abstract void Update();
}
//抽像通知者
abstract class subject
{
private IList<Observer> observers = new List<Observer>();
//增加观察者
public void Attach(Observer observer)
{
observers.Add(observer);
}
//移除观察者
public void Detach(Observer observer)
{
observers.Remove(observer);
}
//通知
public void Notify()
{
foreach (Observer ob in observers)
{
ob.Update();
}
}
}
//通知者
class ConcreteSubject : subject
{
private string subjectstate;
/// <summary>
/// 具体被观察者状态
/// </summary>
public string Subjectstate
{
get { return subjectstate; }
set { subjectstate = value; }
}
}
//观察者
class ConcreteObserver : Observer
{
private string name;
private string observerState;
private ConcreteSubject subject;
public ConcreteObserver(ConcreteSubject subject, string name)
{
this.subject = subject;
this.name = name;
}
public override void Update()
{
observerState = subject.Subjectstate;
Console.WriteLine("观察者{0}的新状态是{1}",name,observerState);
}
public ConcreteSubject Subject
{
get { return subject; }
set { subject = value; }
}
}
}
//例子
namespace App_MYCS.GCZMS
{
class csdy_class
{
public void dy()
{
secretary boos = new secretary();
stockobserver sb = new stockobserver("a",boos);
stockobserver1 sb2 = new stockobserver1("b",boos);
boos.attach(sb);
boos.SubjectState = "状态改变";
boos.notify();
}
}
//
interface subject
{
void attach(Observer observer);
void detach(Observer observer);
void notify();
string SubjectState
{
get;
set;
}
}
abstract class Observer
{
protected string name;
protected subject sub;
public Observer(string name, subject sub)
{
this.name = name;
this.sub = sub;
}
public abstract void Update();
}
//观察对象
class secretary:subject
{
private IList<Observer> observers = new List<Observer>();
private string action;
#region subject 成员
public void attach(Observer observer)
{
observers.Add(observer);
}
public void detach(Observer observer)
{
observers.Remove(observer);
}
public void notify()
{
foreach (Observer ob in observers)
{
ob.Update();
}
}
private string subjectState;
public string SubjectState
{
get { return subjectState; }
set { subjectState = value; }
}
#endregion
}
//通知对象
class stockobserver : Observer
{
public stockobserver(string name,subject sub):base(name,sub)
{
}
public override void Update()
{
//状态改变后对应的操作
}
}
class stockobserver1 : Observer
{
public stockobserver1(string name, subject sub)
: base(name, sub)
{
}
public override void Update()
{
//状态改变后对应的操作
}
}
}
namespace App_MYCS.HDL_CS1
{
class csdy_class
{
public void dy()
{
secretary boos = new secretary();
stockobserver sb = new stockobserver("a", boos);
stockobserver1 sb2 = new stockobserver1("b", boos);
boos.evt_Updata += new EventHandleer(sb.Update);
boos.evt_Updata += new EventHandleer(sb2.Update);
boos.SubjectState = "状态改变";
boos.notify();
}
}
delegate void EventHandleer();
//
abstract class subject
{
public void notify()
{
}
string SubjectState
{
get;
set;
}
}
//观察对象
class secretary : subject
{
private string action;
public event EventHandleer evt_Updata;
public void notify()
{
evt_Updata();
}
private string subjectState;
public string SubjectState
{
get { return subjectState; }
set { subjectState = value; }
}
}
//通知对象
abstract class Observer
{
protected string name;
protected subject sub;
public Observer(string name, subject sub)
{
this.name = name;
}
public abstract void Update();
}
class stockobserver : Observer
{
public stockobserver(string name, subject sub)
: base(name, sub)
{
}
public override void Update()
{
//状态改变后对应的操作
}
}
class stockobserver1 : Observer
{
public stockobserver1(string name, subject sub)
: base(name, sub)
{
}
public override void Update()
{
//状态改变后对应的操作
}
}
}
标签:
原文地址:http://www.cnblogs.com/longkai178/p/5815102.html