接口实现观察者模式
using System.Text; using System.Collections.Generic; using System.Linq; using System; namespace 观察者 { public interface IServer { void Notice( IClient C); } public interface IClient { void Show(); } public class Person : IClient { public Person( IServer S ) { S.Notice(this); } public void Show() { Console.WriteLine("主人被惊醒了"); } } public class Mouses : IClient { public Nouses( IServer C ) { C.Notice( this ); } public void Show() { Console.WriteLine("老鼠被吓跑了"); } } public class Tom : IServer { ArrayList al =new ArrayList(); public void Notice(IClient C) { al.Add( C ); } public void Runs() { Console.WriteLine("猫叫了"); foreach (IClient item in al) { item.Show(); } } } class Test { static void Main(string[] args) { Tom t=new Tom(); Person p=new Person(t); Mouses m =new Mouses(t); t.Runs(); } } }