Showing posts with label .NET Design Patterns. Show all posts
Showing posts with label .NET Design Patterns. Show all posts

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

Monday, April 29, 2013

Published 10:21 PM by with 0 comment

Abstract methods



Abstract methods

When an instance method declaration includes an abstract modifier, that method is said to be an abstract method. Although an abstract method is implicitly also a virtual method, it cannot have the modifier virtual.

An abstract method declaration introduces a new virtual method but does not provide an implementation of that method. Instead, non-abstract derived classes are required to provide their own implementation by overriding that method. Because an abstract method provides no actual implementation, the method-body of an abstract method simply consists of a semicolon.

Abstract method declarations are only permitted in abstract classes

public abstract class AbstractClass
{
public abstract void PrintName();
}

Implementation

public sealed class AbstractImp1:AbstractClass
{
public override void PrintName()
       {
           Console.WriteLine("Im Aruna");
}
}

Calling

AbstractClass abs = new AbstractImp1();
abs.PrintName();

Read More
    email this       edit