3、者,或者是发送到移动设备,还有电子邮件等。一开始我们先不考虑Observer模式,通过一步步地重构,最终重构为Observer模式。现在有这样两个类:Microsoft和Investor,如下图所示:图3UML静态图示例它们的实现如下:word版整理范文范例指导参考public class Microsoft{ private Investor _investor; private String _symbol; private double _price; public void Upda
4、te() { _investor.SendData(this); } public Investor Investor { get { return _investor; } set { _investor = value; }word版整理范文范例指导参考 } public String Symbol { get { return _symbol; } set { _symbol = value; }
5、 } public double Price { get { return _price; } set { _price = value; } }}public class Investor{word版整理范文范例指导参考 private string _name; public Investor(string name) { this._name = name; } public void SendData(Micros
6、oft ms) { Console.WriteLine("Notified {0} of {1}'s " + "change to {2:C}", _name, ms.Symbol,ms.Price); }}简单的客户端实现:class Program{word版整理范文范例指导参考 static void Main(string[] args) { Investor investor = new Investor("Jom"); Microsoft
7、 ms = new Microsoft(); ms.Investor = investor; ms.Symbol = "Microsoft"; ms.Price = 120.00; ms.Update(); Console.ReadLine(); }word版整理范文范例指导参考}运行后结果如下:NotifiedJomofMicrosoft'schangeto¥120可以看到,这段代码运行并没有问题,也确实实现了我们最初的设想的功能,把Mi