private static void Main(string[] args) { var s = new ConcreteSubject(); s.Attach(new ConcreteObserver(s, "X")); s.Attach(new ConcreteObserver(s, "Y")); s.Attach(new ConcreteObserver(s, "Z")); s.SubjectState = "ABC"; s.Notify(); Console.WriteLine(); new Demo(); Console.ReadKey(); }
public static void Main(string[] args) { ConcreteSubject araba = new ConcreteSubject(); araba.Subscribe(new ConcreteObserver.Galerici()); araba.Subscribe(new ConcreteObserver.Sahsisurucu()); araba.Subscribe(new ConcreteObserver.Koleksiyoner()); araba.Tipi = "Otomobil"; araba.Model = 2002; araba.Hacim = 1800; araba.Degeri = 45000; araba.MTVguncellendi = true; Console.ReadKey(true); }
private static void Main(string[] args) { var s = new ConcreteSubject(); s.Attach(new ConcreteObserver(s, "X")); s.Attach(new ConcreteObserver(s, "Y")); s.Attach(new ConcreteObserver(s, "Z")); s.SubjectState = "ABC"; s.Notify(); Console.WriteLine(); new Demo(); Console.ReadKey(); }
static void Main(string[] args) { //观察者模式:观察者模式也叫发布订阅模式,观察者模式建立了一种一对多的依赖关系, //让多个观察者对象同时监听同一个主题的对象,这个主题对象在发生改变时会通知所有的观察者, //使他们能够自动更新自己。 //介绍 //意图:定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新。 //主要解决:一个对象状态改变给其他对象通知的问题,而且要考虑到易用和低耦合,保证高度的协作。 //何时使用:一个对象(目标对象)的状态发生改变,所有的依赖对象(观察者对象)都将得到通知,进行广播通知。 //如何解决:使用面向对象技术,可以将这种依赖关系弱化。 //关键代码:在抽象类里有一个 ArrayList 存放观察者们。 //应用实例: 1、拍卖的时候,拍卖师观察最高标价,然后通知给其他竞价者竞价。 2、西游记里面悟空请求菩萨降服红孩儿,菩萨洒了一地水招来一个老乌龟,这个乌龟就是观察者,他观察菩萨洒水这个动作。 //优点: 1、观察者和被观察者是抽象耦合的。 2、建立一套触发机制。 //缺点: 1、如果一个被观察者对象有很多的直接和间接的观察者的话,将所有的观察者都通知到会花费很多时间。 2、如果在观察者和观察目标之间有循环依赖的话,观察目标会触发它们之间进行循环调用,可能导致系统崩溃。 3、观察者模式没有相应的机制让观察者知道所观察的目标对象是怎么发生变化的,而仅仅只是知道观察目标发生了变化。 //使用场景: //一个抽象模型有两个方面,其中一个方面依赖于另一个方面。将这些方面封装在独立的对象中使它们可以各自独立地改变和复用。 //一个对象的改变将导致其他一个或多个对象也发生改变,而不知道具体有多少对象将发生改变,可以降低对象之间的耦合度。 //一个对象必须通知其他对象,而并不知道这些对象是谁。 //需要在系统中创建一个触发链,A对象的行为将影响B对象,B对象的行为将影响C对象……,可以使用观察者模式创建一种链式触发机制。 //注意事项: 避免循环引用、如果顺序执行,某一观察者错误会导致系统卡壳,一般采用异步方式。 #region 观察者模式 var subject = new ConcreteSubject(); subject.SubjectState = "ABC"; var observerA = new ConcreteObserver("A", subject); var observerB = new ConcreteObserver("B", subject); subject.Attach(observerA); subject.Attach(observerB); subject.Notify(); var bossSubject = new BossSubject(); bossSubject.SubjectState = "老板来了。。。"; var stockObserver = new StockObserver("大刘", bossSubject); var nbaObserver = new NBAObserver("小王", bossSubject); bossSubject.Update += stockObserver.Update; bossSubject.Update += nbaObserver.Update; bossSubject.Notify(); Console.ReadLine(); #endregion }
static void Main(string[] args) { //Creating new Subject and Observers ConcreteSubject source = new ConcreteSubject(35, 30); ConcreteObserver stationAlpha = new ConcreteObserver("Alpha"); ConcreteObserver stationBeta = new ConcreteObserver("Beta"); //Registering Observers to Subject source.Register(stationAlpha); source.Register(stationBeta); //Updating the status of the Subject source.UpdateData(38, 33); //Removing an Observer source.Remove(stationAlpha); // Again, updating the status of the Subject source.UpdateData(36, 31); }
static void Main(string[] args) { //Creating new Subject and Observers ConcreteSubject source = new ConcreteSubject(35,30); ConcreteObserver stationAlpha = new ConcreteObserver("Alpha"); ConcreteObserver stationBeta = new ConcreteObserver("Beta"); //Registering Observers to Subject source.Register(stationAlpha); source.Register(stationBeta); //Updating the status of the Subject source.UpdateData(38,33); //Removing an Observer source.Remove(stationAlpha); // Again, updating the status of the Subject source.UpdateData(36,31); }
static void Main(string[] args) { ConcreteSubject cs; ConcreteObserver co1, co2, co3; cs = new ConcreteSubject(); co1 = new ConcreteObserver(cs); co2 = new ConcreteObserver(cs); co3 = new ConcreteObserver(cs); cs.Attach(co1); cs.Attach(co2); cs.Attach(co3); cs.State = 1; cs.State = 2; cs.State = 100; Console.ReadKey(); }
static void Main(string[] args) { Subject Wangsicong = new ConcreteSubject("王思聪"); var jello = new ConcreteObserver("jello"); var fanbingbing = new ConcreteObserver("范冰冰"); var huangxiaoming = new ConcreteObserver("黄晓明"); Wangsicong.Add(jello); Wangsicong.Add(fanbingbing); Wangsicong.Add(huangxiaoming); Wangsicong.Notify("国足不给力啊!"); Console.WriteLine("---jello取消关注了王思聪---"); Wangsicong.Remove(jello); Console.WriteLine("---angelababy关注了王思聪---"); var angelababy = new ConcreteObserver("angelababy"); Wangsicong.Add(angelababy); Wangsicong.Notify("蜻蜓FF老板应该坐牢"); Console.ReadKey(); }
static void Main(string[] args) { Console.WriteLine("Hello World!"); Console.WriteLine("--------------------ObserverPattern--------------------"); Console.WriteLine(); Subject subject1 = new ConcreteSubject("我是订阅号1"); Subject subject2 = new ConcreteSubject("我是订阅号2"); Observer observer1 = new ConcreteObserver("我是观察者1"); Observer observer2 = new ConcreteObserver("我是观察者2"); Observer observer3 = new ConcreteObserver("我是观察者3"); subject1.AddObserver(observer1); subject2.AddObserver(observer1); subject2.AddObserver(observer2); observer3.AddSubject(subject2); subject1.PublishMessage("订阅号1发布了第1条消息"); subject2.PublishMessage("订阅号2发布了第1条消息"); subject1.PublishMessage("订阅号1发布了第2条消息"); subject2.PublishMessage("订阅号2发布了第2条消息"); Console.ReadKey(); }
static void Main(string[] args) { Spy spy = new Spy007("007"); CountryA countryA = new CountryA("A", spy); CountryB countryB = new CountryB("B", spy); spy.Attach(countryA); spy.Attach(countryB); spy.SetIntelligence("研制武器!"); spy.Notify(); Subject subject = new ConcreteSubject(); subject.Attach(new ConcreteObserver(subject, "1号观察者")); subject.Attach(new ConcreteObserver(subject, "2号观察者")); subject.Attach(new ConcreteObserver(subject, "3号观察者")); subject.SetState("Talking"); subject.Notify(); }
public ConcreteObserver(ConcreteSubject subject, string name) { this._subject = subject; this._name = name; }
public ConcreteObserver(string name, ConcreteSubject subject) { _name = name; _subject = subject; }
public ConcreteObserver(ConcreteSubject subject) { ConcreteSubject = subject; }
public ConcreteObserver(string name, ConcreteSubject conSubject) { this._name = name; this.ConSubject = conSubject; }
public ConcreteObserver(string name, ConcreteSubject subject) { this.name = name; this.subject = subject; }
public void Destroy() { _subject.DetachObserver(this); _subject = null; }
public ConcreteObserver(ConcreteSubject subject) { _subject = subject; _subject.AttachObserver(this); }
static void Main(string[] args) { // Configure Observer pattern ConcreteSubject s = new ConcreteSubject(); s.Attach(new ConcreteObserver(s, "X")); s.Attach(new ConcreteObserver(s, "Y")); s.Attach(new ConcreteObserver(s, "Z")); // Change subject and notify observers s.SubjectState = "ABC"; s.Notify(); // Wait for user Console.ReadKey(); }
// Constructor public ConcreteObserver( ConcreteSubject subject, string name) { this._subject = subject; this._name = name; }
public ConcreteObserver(ConcreteSubject subject, string name) { Subject = subject; _name = name; }
public ConcreteObserver(ConcreteSubject subject, string name) { Subject = subject; _name = name; }