Showing posts with label .net. Show all posts
Showing posts with label .net. Show all posts

Wednesday, May 8, 2013

Published 9:50 PM by with 0 comment

Template Method Pattern



Template Method Pattern

Definition
Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.

abstract class AbstractClass
    {
        public abstract void PrimitiveOperation1();
        public abstract void PrimitiveOperation2();

        public void TemplateMethod()
        {
            PrimitiveOperation1();
            PrimitiveOperation2();
            Console.WriteLine("Call Template Method");
        }
    }

    //Concrete Class A
    class ConcreteClassA : AbstractClass
    {
        public override void PrimitiveOperation1()
        {
            Console.WriteLine("concreate A primi 1");
        }

        public override void PrimitiveOperation2()
        {
            Console.WriteLine("concreate A primi 2");
        }
    }

    //Concrete Class B
    class ConcreteClassB : AbstractClass
    {
        public override void PrimitiveOperation1()
        {
            Console.WriteLine("concreate B primi 1");
        }

        public override void PrimitiveOperation2()
        {
            Console.WriteLine("concreate B primi 2");
        }
    }

//calling…
            //Template Method
            AbstractClass a = new ConcreteClassA();
            a.TemplateMethod();

            AbstractClass b = new ConcreteClassB();
            b.TemplateMethod();

Read More
    email this       edit
Published 1:44 AM by with 0 comment

Composite Pattern - [Structural Patterns]


Definition
[Download cs c#]
Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.

    //Component class
    abstract class Component
    {
        protected string name;

        public Component(string name)
        {
            this.name = name;
        }

        public abstract void Add(Component c);
        public abstract void Remove(Component c);
        public abstract void Display(int Dept);
    }

    //Composite Class
    class Composite : Component
    {
        private List<Component> _com = new List<Component>();

        public Composite(string name):base(name)
        {

        }
        public override void Add(Component c)
        {
            _com.Add(c);
        }
        public override void Remove(Component c)
        {
            _com.Remove(c);
        }
        public override void Display(int Dept)
        {
            Console.WriteLine(new String('-',Dept)+name);

            foreach (Component component in _com)
            {
                component.Display(Dept + 2);
            }
        }
    }

        //Leaf Class
        class Leaf : Component
        {
            public Leaf(string name):base(name)
            {

            }
            public override void Add(Component c)
            {
                Console.WriteLine("Can not add to a leaf");
            }
            public override void Remove(Component c)
            {
                Console.WriteLine("Can not remove from a leaf");
            }
            public override void Display(int Dept)
            {
                Console.WriteLine(new String('-', Dept) + name);
            }
        }


            //---Composite Calling...
            Composite root = new Composite("root");
            root.Add(new Leaf("Leaf A"));
            root.Add(new Leaf("Leaf B"));

            Composite comp = new Composite("Composite X");
            comp.Add(new Leaf("Leaf XA"));
            comp.Add(new Leaf("Leaf XAB"));

            root.Add(comp);
            root.Add(new Leaf("Leaf C"));

            // Add and remove a leaf
            Leaf leaf = new Leaf("Leaf D");
            root.Add(leaf);
            root.Remove(leaf);

            // Recursively display tree
            root.Display(1);



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