System; System.Collections.Generic; Demo { 抽象層,定義了觀察者模式 { SubScribe( s); UnSubScribe( s); Publish( newsTitle); } { Read( newsTitle); } 實現層,是對抽象層的具體實現 : { <> readers = <>(); SubScribe( s) { readers.Add(s); } UnSubScribe( s) { (readers.Count >0) { readers.Remove(s); } } Publish( newsTitle) { ( i readers) { i.Read(newsTitle); } } } : { subscriber = .Empty; Subscriber( subscriber) { .subscriber = subscriber; } Read( newsTitle) { .WriteLine(.Format(, subscriber, newsTitle)); } } { Main([] args) { news = (); zs = (), ls = (), ww = (); news.SubScribe(zs); news.SubScribe(ls); news.SubScribe(ww); news.UnSubScribe(ls); news.Publish(); news.Publish(); .ReadKey(); } } }