Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Optimizing Performance with Denormalization, Observer Design Pattern and Asynchronous Technique

, 24 Aug 2009 CPOL
Making use of Denormalization, Observer Design Pattern and Asynchronous technique can enhance performance for any system with massive database.
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Pham Dinh Truong
Technical Lead
Vietnam Vietnam
I have been working in ASP.NET for more than 7 years. I've also been working on Java and Windows-based apps for more than 4 years. My core competences include ASP.NET, Design Patterns, SubSonic, Ajax, Asynchronous and Multi-threading. I'm particularly interested in building smart software with great UI/UX and rich User Experience.
 
Recently, I'm interested on developing online advertisements. It's so complicated just like studying a new programming language!
 
Feel free to discuss with me at: phdtruong@yahoo.com.
 
Visit my home:
http://smartwebdevelopment.blogspot.com
https://www.facebook.com/SmartWebDevelopment
Follow on   Twitter   Google+

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 24 Aug 2009
Article Copyright 2009 by Pham Dinh Truong
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid