|
using System;
namespace ObserverPattern_Test
{
class Program
{
static void Main(string[] args)
{
OrderController objOrder = new OrderController();
ProductObserver observer1 = new ProductObserver(objOrder, "Product");
CustomerObserver observer2 = new CustomerObserver(objOrder, "Customer");
SalesPersonObserver observer3 = new SalesPersonObserver(objOrder, "Sales Person");
objOrder.Attach(observer1);
objOrder.Attach(observer2);
objOrder.Attach(observer3);
objOrder.OrderID = "10029";
Console.WriteLine("=================================================");
Console.WriteLine("CASE #1: UPDATE ORDER INFORMATION (such as change");
Console.WriteLine("customer, change sales person...)");
Console.WriteLine("=================================================");
if (objOrder.Update() == true)
{
Console.WriteLine("General information for Order {0} has been updated successfully.", objOrder.OrderID);
Console.WriteLine();
Console.WriteLine("For this change, the following Observers have been updated too:");
Console.WriteLine();
objOrder.State = SubjectState.UPDATE_ORDER;
objOrder.Notify();
}
Console.WriteLine("=================================================");
Console.WriteLine("CASE #2: UPDATE ORDER DETAIL INFORMATION (such as");
Console.WriteLine("product price, quantity..)");
Console.WriteLine("=================================================");
int productID = 10;
objOrder.AddProductToCart(productID);
if (objOrder.Update() == true)
{
Console.WriteLine("Order detail information for Order {0} has been updated successfully.", objOrder.OrderID);
Console.WriteLine();
Console.WriteLine("For this change, the following Observers have been updated too:");
Console.WriteLine();
objOrder.State = SubjectState.UPDATE_ORDER_DETAIL;
objOrder.Notify();
}
Console.WriteLine("=================================================");
Console.WriteLine("CASE #3: DETACH OBSERVER IN RUN-TIME");
Console.WriteLine("=================================================");
Console.WriteLine("Detaching SalesPersonObserver...");
Console.WriteLine();
objOrder.Detach(observer3); //Detach SalesPersonObserver
productID = 20;
objOrder.AddProductToCart(productID);
if (objOrder.Update() == true)
{
Console.WriteLine("Order detail information for Order {0} has been updated successfully.", objOrder.OrderID);
Console.WriteLine();
Console.WriteLine("For this change, the following Observers have been updated too:");
Console.WriteLine();
objOrder.State = SubjectState.UPDATE_ORDER_DETAIL;
objOrder.Notify();
}
Console.ReadKey();
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I love working with Web-based workflow automation systems, ERP systems for SME, Data Visualization and Augmented Intelligence.
I have been working in ASP.NET for more than 12 years. I've also been working on Java and Windows-based apps for more than 5 years. My core competencies include ASP.NET (+net core), MVC, Restful API, Advance JavaScript, JQuery, Bootstrap, SubSonic, Dapper, Entity Framework, Lucne.net, ElasticSearch Ajax... I'm particularly interested in building smart apps with great UI/UX and high earned value.
I love to write elegant code. I am a type of pragmatic personality.
Recently, I've been interested in developing SPA apps using Angular with NodeJS backend. I also love to write the progressive web apps which could be the next big thing for the mobile web.
Feel free to discuss with me at: phamdinhtruong@gmail.com