Sunday, May 5, 2013

Published 11:24 PM by with 0 comment

Observer Pattern - [Behavioral Patterns]


Observer Pattern 

[Download cs c#]

Definition

Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.



    //Observer Class
    abstract class Observer
    {
        public abstract void Update();
    }


    //Subject Class
    abstract class Subject
    {
        private List<Observer> _observer = new List<Observer>();

        public void Register(Observer obs)
        {
            _observer.Add(obs);
        }

        public void Unregister(Observer obs)
        {
            _observer.Remove(obs);
        }

        public void Notify()
        {
            foreach (Observer obs in _observer)
            {
                obs.Update();
            }
        }
    }

    //StockMarket Class
    class StockMarket : Subject
    {
        private string _subjectState;

        public string SubjectState
 {
get
{
 return _subjectState;
}
set
{
 _subjectState = value;
}
}
    }


    //GoogleStockGadet Class
    class GoogleStockGadet  : Observer
    {
        private string _name;
        private string _observerState;
        private StockMarket _concreteSubject;

        public GoogleStockGadet(StockMarket conSubject, string name)
        {
            _concreteSubject = conSubject;
            _name = name;
        }

        public override void Update()
        {
            _observerState = _concreteSubject.SubjectState;
            Console.WriteLine("Observer {0}'s new state is {1}", _name, _observerState);
        }

        public StockMarket Subject
 {
 get
 {
return _concreteSubject;
 }
 set
 {
 _concreteSubject = value;
 }
 }
    }

    //MSNStockGadget Class
    class MSNStockGadget  : Observer
    {
        private string _name;
        private string _observerState;
        private StockMarket _concreteSubject;

        public MSNStockGadget(StockMarket conSubject, string name)
        {
            _concreteSubject = conSubject;
            _name = name;
        }

        public override void Update()
        {
            _observerState = _concreteSubject.SubjectState;
            Console.WriteLine("Observer {0}'s new state is {1}", _name, _observerState);
        }

        public StockMarket Subject
 {
get
{
return _concreteSubject;
}
Set
{
_concreteSubject = value;
}
 }
    }


    //YahooStockGadget Class
    class YahooStockGadget  : Observer
    {
        private string _name;
        private string _observerState;
        private StockMarket _concreteSubject;

        public YahooStockGadget(StockMarket conSubject, string name)
        {
            _concreteSubject = conSubject;
            _name = name;
        }

        public override void Update()
        {
            _observerState = _concreteSubject.SubjectState;
            Console.WriteLine("Observer {0}'s new state is {1}", _name, _observerState);
        }

        public StockMarket Subject
 {
 get
 {
 return _concreteSubject;
 }
 Set
 {
_concreteSubject = value;
 }
 }
    }

            //Calling...
            StockMarket s = new StockMarket();

            s.Register(new GoogleStockGadet(s, "X"));
            s.Register(new MSNStockGadget(s, "Y"));
            s.Register(new YahooStockGadget(s, "Y"));

            s.SubjectState = "ABC";
            s.Notify();


Read More
    email this       edit

Thursday, May 2, 2013

Published 1:50 AM by with 0 comment

Strategy Pattern - [Behavioral Patterns]



Strategy Pattern


Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

So to understand these concepts, let us work on a toy application for converting audio files into different formats (like MP3 to WMA). Here the user will have an option of selecting the source file and then deciding the target output type format. The operation that will be performed is to convert the source file to the target output format.

The multiple strategies that we will be using are to define the quality of the output. The user could choose to convert to a low quality output (perhaps because it is fast and is of small size). The user could choose to convert to a high quality output (perhaps because he wants to use it on high-end audio products from BOSE or B&O), or he could just choose an average quality output (perhaps to put it on his cell phone). We implement all these as separate strategies so that the client code can be independent of the implementation details of these different strategies and will work in the same fashion for any selected strategy.

interface IStrategy
{
  void Convert();
}


class FileConverter
{
int quality;
        public FileConverter(int qual)
        {
            quality = qual;
        }

        public void FileConvert()
        {
            if (quality < 100)
            {
                Console.WriteLine("Low Quality");
            }
            else if (quality > 100 && quality < 200)
            {
                Console.WriteLine("Medium Quality ");
            }
            else
            {
                Console.WriteLine("High Quality");
            }
        }
    }


    class LowQualityConversion:IStrategy
    {
        const int QUALITY = 50;
        public void Convert()
        {
            FileConverter fcon = new FileConverter(QUALITY);
            fcon.FileConvert();
        }
    }
    class MediumQualityConversion : IStrategy
    {
        const int QUALITY = 150;
        public void Convert()
        {
            FileConverter fcon = new FileConverter(QUALITY);
            fcon.FileConvert();
        }
    }
    class HighQualityConversion : IStrategy
    {
        const int QUALITY = 250;
        public void Convert()
        {
            FileConverter fcon = new FileConverter(QUALITY);
            fcon.FileConvert();
        }
    }

Calling…

IStrategy selectStratergy = null;
int choice = Console.Read();

if (choice == '1')
{
selectStratergy = new LowQualityConversion();
}
else if (choice == '2')
{
selectStratergy = new MediumQualityConversion();
}
else if (choice == '3')
{
selectStratergy = new HighQualityConversion();
}
if (selectStratergy != null)
{
selectStratergy.Convert();
}

Read More
    email this       edit

Wednesday, May 1, 2013

Published 11:14 PM by with 1 comment

Factory Pattern - [Creational Patterns]



Factory Pattern

Logical Model

As with other design patterns, there are countless variations of the Factory pattern, although most variants typically used the same set of primary actors, a client, a factory, and a product. The client is an object that requires an instance of another object (the product) for some purpose. Rather than creating the product instance directly, the client delegates this responsibility to the factory. Once invoked, the factory creates a new instance of the product, passing it back to the client. Put simply, the client uses the factory to create an instance of the product. Bellow shows this logical relationship between these elements of the pattern.





abstract class Page
{
}

class Education : Page
{
}

class SkillPage : Page
{
}

class Experiance : Page
{
}

//----
abstract class Document
{
private List<Page> _pages = new List<Page>();

        public abstract void CreatePages();

        public Document()
        {
            this.CreatePages();
        }

        public List<Page> Page
        {
            get { return _pages; }
        }
    }

class Resume : Document
{
   public override void CreatePages()
   {
Page.Add(new Education());
       Page.Add(new SkillPage());
       Page.Add(new Experiance());
   }
}

 

Read More
    email this       edit