static void Main() { IBM ibm = new IBM("IBM", 120.00); TP TP = new TP("TP", 10.00); Investor Jonhy = new Investor("João"); Investor Rui = new Investor("Rui"); ibm.Attach(Rui); ibm.Attach(Jonhy); ibm.Attach(new Investor("Miguel")); ibm.Attach(new Investor("Aga")); TP.Attach(Rui); ibm.Price = 120.10; ibm.Price = 121.00; ibm.Price = 120.50; TP.Price *= 2; ibm.Detach(Jonhy); ibm.Price = 120.75; TP.Price *= 2; Console.ReadKey(); }
static void Main(string[] args) { // create investors Investor ivrInvestor1 = new Investor("ivrInvestor1"); Investor ivrInvestor2 = new Investor("ivrInvestor2"); Investor ivrInvestor3 = new Investor("ivrInvestor3"); Investor ivrInvestor4 = new Investor("ivrInvestor4"); Investor ptraInvestor1 = new Investor("ptraInvestor1"); Investor ptraInvestor2 = new Investor("ptraInvestor2"); //create stocks Stock ivr = new IVR("IVR", 2.1); Stock ptra = new PTRA("PTRA", 7.7); //attach investors to stock ivr.Attach(ivrInvestor1); ivr.Attach(ivrInvestor2); ivr.Attach(ivrInvestor3); ivr.Attach(ivrInvestor4); ptra.Attach(ptraInvestor1); ptra.Attach(ptraInvestor2); //change the price of stock ivr.Price = 1.9; ivr.Price = 1.9; ivr.Price = 9.9; ptra.Price = 7.9; }
static void Main(string[] args) { Console.WriteLine("Observer Pattern through Go4"); IInvestor User1 = new Investor("User1"); IInvestor User2 = new Investor("User2"); IBM iBM = new IBM(10, "IBM"); iBM.Register(User1); iBM.Register(User2); iBM.Price = 20; }
static void Main(string[] args) { IBM ibm = new IBM("IBM", 120.0); Investor investor1 = new Investor("John"); Investor investor2 = new Investor("Jack"); Investor investor3 = new Investor("Emily"); Investor investor4 = new Investor("Scarlett"); ibm.AddAllListeners(investor1, investor2, investor3, investor4); ibm.CalculatePriceChange(130.0); Console.ReadKey(); }
public static void Main(string[] args) { // 创建投资者对象 Investor m = new Investor("Mike"); Investor b = new Investor("Bob"); // 创建 IBM stock对象并添加投资者 IBM ibm = new IBM("IBM", 120.00); ibm.Attach(m); ibm.Attach(b); // 改变股票价格,将会自动通知投资者 ibm.Price = 120.10; ibm.Price = 121.00; ibm.Price = 120.50; ibm.Price = 120.75; }
static void Main(string[] args) { // Create subject Stock stock = new IBMStock("IBM", 21.76); // Create observers IInvestor investor = new Investor("Investor 1"); IInvestor investor2 = new Investor("Investor 2"); // Attach observers to subject stock.Attach(investor); stock.Attach(investor2); // Update prices (will notify investors) stock.Price = 21.80; stock.Price = 21.86; stock.Price = 21.90; stock.Price = 21.99; }
// 删除投资者 public void Detach(Investor investor) { investors.Remove(investor); }
// 添加投资者 public void Attach(Investor investor) { investors.Add(investor); }