Tuesday, December 11, 2007

Generic Decorator Pattern


using System;
using 
System.Collections.Generic;

//Decorator pattern is very powerful pattern which can be used for supplementing the inheritance relationship with 
//composition based relationship. The basic idea is Attaching additional responsibilities to an object dynamically. 
//Decorators provide a flexible alternative to subclassing for extending functionality and it can be implemented 
//on several different ways. 
//This sample program presented here show that it can be done through Generic and Interface. 

//The classes and/or objects participating in this pattern are:
//1. Generic Abstract Decorator
//2. Generic Concrete Decorator
//3. Abstract Component
//4. Concrete Component


namespace Generic.DecoratorPattern
{
    
    // Generic Abstract Decorator 
    
abstract class Decorator<T> : ComputerItem  where T : ComputerItem, new() 
    {
        
protected readonly T computerItem = new T();

        
// Constructor 
        
public Decorator(T computerItem)
        {
            
this.computerItem computerItem;
        
}

        
public override void DisplayReport()
        {
            computerItem.DisplayReport()
;
        
}

    }

    
// Generic Concrete Decorator 
    
class Saleable<T> :  Decorator<T> where T : ComputerItem, new()
    {
        
protected List<string> Buyers = new List<string>();

        
// Constructor 
        
public Saleable(T computerItem) : base(computerItem)
        {
        }

        
public void SaleItem(string name)
        {
            Buyers.Add(name)
;
            
computerItem.NumStocks--;
        
}

        
public void ReturnItem(string name)
        {
            Buyers.Remove(name)
;
            
computerItem.NumStocks++;
        
}

        
public override void DisplayReport()
        {
            
base.DisplayReport();

            foreach 
(string buyer in Buyers)
            {
                Console.WriteLine(
" Buyer: " + buyer);
            
}
        }
    }

    
interface IComputerItem
    {
        
void DisplayReport();
    
}

    
// Abstract Component 
    
abstract class ComputerItem : IComputerItem
    {
        
private string brand;

        public string 
Brand
        {
            
get return brand}
            
set { brand = value; }
        }

        
private string type;

        public string 
Type
        {
            
get return type}
            
set { type = value; }
        }

        
private int numStocks;

        public int 
NumStocks
        {
            
get return numStocks}
            
set { numStocks = value; }
        }

        
public abstract void DisplayReport();
    
}

    
// Concrete Component 
    
class Desktop : ComputerItem
    {
        
public Desktop()
        { }

        
public Desktop(string brand, string type, int numStocks)
        {
            
this.Brand brand;
            this
.Type type;
            this
.NumStocks numStocks;
        
}

        
public override void DisplayReport()
        {
            Console.WriteLine(
"\n----- Desktop ----- ");
            
Console.WriteLine(" Brand: {0}", Brand);
            
Console.WriteLine(" Type: {0}", Type);
            
Console.WriteLine(" # Stocks: {0}", NumStocks);
        
}
    }

    
// Concrete Component 
    
class Laptop : ComputerItem
    {
        
public Laptop()
        { }

        
public Laptop(string brand, string type, int numStocks)
        {
            
this.Brand brand;
            this
.Type type;
            this
.NumStocks numStocks;
        
}

        
public override void DisplayReport()
        {
            Console.WriteLine(
"\n----- Laptop ----- ");
            
Console.WriteLine(" Brand: {0}", Brand);
            
Console.WriteLine(" Type: {0}", Type);
            
Console.WriteLine(" # Stocks: {0}", NumStocks);
        
}
    }

    
class Program
    {
        
static void Main()
        {
            
// Create desktop 
            
Desktop desktop = new Desktop("HPP""XM600"32);
            
desktop.DisplayReport();

            
// Create laptop 
            
Laptop laptop = new Laptop("ASUSS""F3MAP0"9);
            
laptop.DisplayReport();

            
Console.WriteLine("\nMaking laptop saleable:");

            
Saleable<Laptop> saleLaptop = new Saleable<Laptop>(laptop);
            
saleLaptop.SaleItem("Customer #1");
            
saleLaptop.SaleItem("Customer #2");
            
saleLaptop.SaleItem("Customer #3");

            
saleLaptop.DisplayReport();

            
Console.Read();
        
}
    }
}

Tuesday, December 4, 2007

Design Event Using Generic Delegate And Generic Interface

//When you design an event you have to do 3 steps

//1. Declare a private member for the event
//private event EventHandler<MyEventType> _EventName;

//2. Declare public accessors for adding and removing handlers
//public event EventHandler<MyEventType> EventName
//{
//      add    { _EventName += value; }
//      remove { _EventName -= value; }
//}

//3. Provide a virtual OnEventName method
//virtual void OnEventName(MyEventType e)
//{
//      if(_EventName != null)
//      {
//            try
//            {
//                  _EventName(this,e);
//            }
//            catch(Exception e)
//            {
//                  //your code here
//            }
//      }
//}

using System;

namespace 
Lab
{
    
    
interface IEventNum
    {
        
int EventNum
        {
            
get; set;
        
}
    }

    
//Define an interface that can perform operations on a generic type parameter
    
interface ITEventNum<T>
    {
        T EventNum
        {
            
get;
            set;
        
}
    }

    
//Create a type that inherits from EventArgs if the event is going to propagate event specific data.
    
public class MyEventArgs1 : EventArgs, IEventNum
    {
        
public int _EventNum;

        public int 
EventNum
        {
            
get return  _EventNum}
            
set { _EventNum = value; }
        }

    }

    
//Create a delegate signature for the event. 
    
public delegate void MyEventHandler(object source, MyEventArgs1 arg);

    class 
MyEvent1
    {
        
private static int count 0;

        
// Declare an event of delegate type MyEventHandler of MyEventArgs1.
        // Implicit Registration
        
public event MyEventHandler SomeEvent;

        
//Create a method on the type that will raise the event.
        
public virtual void OnSomeEvent()
        {
            MyEventArgs1 arg 
= new MyEventArgs1();

            
// Copy to a temporary variable to be thread-safe.
            
MyEventHandler temp SomeEvent;
            if 
(SomeEvent != null)
            {
                
try
                
{
                    arg.EventNum 
count++;
                    
temp(this, arg);
                
}
                
catch (Exception e)
                {
                    Console.WriteLine(e.Message)
;
                
}
            }
        }
    }

    
//Create a type that inherits from EventArgs if the event is going to propagate event specific data.
    
public class MyEventArgs2 : EventArgs, IEventNum
    {
        
public int _EventNum;

        public int 
EventNum
        {
            
get return _EventNum}
            
set { _EventNum = value; }
        }
    }

    
class MyEvent2
    {
        
private static int count 0;

        
//Generic EventHandler<T> delegate to declare an event bound to the specific EventArgs derivative 
        //Implicit Registration
        
public event EventHandler<MyEventArgs2> SomeEvent;

        
//Create a method on the type that will raise the event.
        
public virtual void OnSomeEvent()
        {
            MyEventArgs2 arg 
= new MyEventArgs2();

            
// Copy to a temporary variable to be thread-safe.
            
EventHandler<MyEventArgs2> temp SomeEvent;
            if 
(temp != null)
            {
                
try
                
{
                    arg.EventNum 
count++;
                    
temp(this, arg);
                
}
                
catch (Exception e)
                {
                    Console.WriteLine(e.Message)
;
                
}
            }
        }
    }


    
//Create a type that inherits from EventArgs if the event is going to propagate event specific data.
    //When you implement a generic interface, the supporting type specifies the placeholder type
    
public class MyEventArgs3<T> : EventArgs, ITEventNum<T>
    {
        
public T _EventNum;
        
        public T 
EventNum
        {
            
get return  _EventNum}
            
set { _EventNum = value; }
        }

    }

    
    
class MyEvent3
    {
        
private static int count 0;

        
// Declare an event of delegate type EventHandler of MyEventArgs3.
        // Implicit Registration
        // public event EventHandler<MyEventArgs3<int>> SomeEvent;

        // Explicit registration
        
private EventHandler<MyEventArgs3<int>> _SomeEvent;
        public event 
EventHandler<MyEventArgs3<int>> SomeEvent
        {
            
add    { _SomeEvent += value; }
            
remove { _SomeEvent -= value; }
        }
        
        
//Create a method on the type that will raise the event.
        
public virtual void OnSomeEvent()
        {
            MyEventArgs3<
int> arg = new MyEventArgs3<int>();

            
// Copy to a temporary variable to be thread-safe.
            
EventHandler<MyEventArgs3<int>> temp _SomeEvent;
            if 
(temp != null)
            {
                
try
                
{
                    arg.EventNum 
count++;
                    
temp(this, arg);   
                
}
                
catch(Exception e)
                {
                    Console.WriteLine(e.Message)
;
                
}
                
            }
        }
    }

    
class EventDemo1 : MyEvent1
    {
        
public void TestMain()
        {
            Test1 test1 
= new Test1();

            
MyEvent1 evt;

            
evt = new EventDemo1();
            
MyEventArgs1 arg = new MyEventArgs1();

            
// Add handler() to the event list.
            
evt.SomeEvent += new MyEventHandler(test1.handler);

            
// Fire the event.
            
evt.OnSomeEvent();
            
evt.OnSomeEvent();

        
}

        
public override void OnSomeEvent()
        {
            
base.OnSomeEvent();
            
Console.WriteLine("Override");
        
}
    }

    
class EventDemo2 : MyEvent2
    {
        
public void TestMain()
        {
            Test2 test2 
= new Test2();
            
MyEvent2 evt;

            
evt = new EventDemo2();
            
MyEventArgs2 arg = new MyEventArgs2();

            
// Add Handler() to the event list.
            
evt.SomeEvent += new EventHandler<MyEventArgs2>(test2.Handler);

            
// Fire the event.
            
evt.OnSomeEvent();
            
evt.OnSomeEvent();

        
}

        
public override void OnSomeEvent()
        {
            
base.OnSomeEvent();
            
Console.WriteLine("Override");
        
}

    }


    
class EventDemo3 : MyEvent3
    {
        
public void TestMain()
        {
            Test3 test3 
= new Test3();
            
            
MyEvent3 evt;
            
            
evt = new EventDemo3();
            
MyEventArgs3<int> arg = new MyEventArgs3<int>();

            
// Add Handler() to the event list.
            
evt.SomeEvent += new EventHandler<MyEventArgs3<int>>(test3.Handler);
           
            
// Fire the event.
            
evt.OnSomeEvent();
            
evt.OnSomeEvent();

        
}

        
public override void OnSomeEvent()
        {
            
base.OnSomeEvent();
            
Console.WriteLine("Override");
        
}

    }


    
class Test1
    {
        
public void handler(object source, MyEventArgs1 arg)
        {
            
if (source == nullthrow new ArgumentNullException("source");
            
Result.Write(source, arg);
        
}
    }

    
class Test2
    {
        
public void Handler(object source, MyEventArgs2 arg)
        {
            
if (source == nullthrow new ArgumentNullException("source");
            
Result.Write(source, arg);
        
}
    }

    
class Test3
    {
        
public void Handler(object source, MyEventArgs3<int> arg)
        {
            
if (source == nullthrow new ArgumentNullException("source");
            
Result.Write(source, arg);
        
}

    }

    
class Result
    {
        
public static void Write(object source, IEventNum arg)
        {
            Console.WriteLine(
"Event {0} received by an Test object.", arg.EventNum);
            
Console.WriteLine("Source is " + source);
        
}

        
public static void Write(object source, ITEventNum<int> arg)
        {
            Console.WriteLine(
"Event {0} received by an Test object.", arg.EventNum);
            
Console.WriteLine("Source is " + source);
        
}
    }
   
    
class program
    {
        
public static void Main()
        {
            EventDemo1 evt1 
= new EventDemo1();
            
evt1.TestMain();

            
EventDemo2 evt2 = new EventDemo2();
            
evt2.TestMain();

            
EventDemo3 evt3 = new EventDemo3();
            
evt3.TestMain();

            
Console.Read();
        
}
    }
}
Indonesia To Blog -Top Site
SEO - search engine submission and optimisation