Archive

Archive for April, 2014

Singleton

April 27, 2014 Leave a comment

Frequency of Use
High

Intent
Ensure a class only has one instance, and provide a global point of access to it.

Structure
Singeton

Applicability
Use the Singleton pattern when

  • there must be exactly one instance of a class, and it must be accessible to clients from a well-known access point.
  • when the sole instance should be extensible by subclassing, and clients should be able to use an extended instance without modifying their code.

Code Implemetation (C#)

using System;

namespace Singleton
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            Singleton s1 = Singleton.Instance();
            Singleton s2 = Singleton.Instance();

            if(s1 == s2)
            {
                Console.WriteLine("Objects are the same Instance");
            }

            Console.ReadKey();
        }
    }

    class Singleton
    {
        private static Singleton instance;

        protected Singleton()
        {

        }

        public static Singleton Instance()
        {
            if(instance == null)
            {
                instance = new Singleton();
            }

            return instance;
        }
    }
}

Output Code

Objects are the same Instance

 

 

Categories: C#, Design Pattern, Programming

Iterator

April 27, 2014 Leave a comment

Frequency of Use
High

Intent
Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.

Structure

Iterator

Iterator

Aplicability
Use the Iterator pattern

  • to access an aggregate object’s contents without exposing its internal representation.
  • to support multiple traversals of aggregate objects.
  • to provide a uniform interface for traversing different aggregate structures

Code Implementation (C#)

using System;
using System.Collections;

namespace Iterator
{
    class MainClass
    {

        public static void Main(string[] args)
        {
            ConcreteAggregate a = new ConcreteAggregate();
            a[0] = "Item A";
            a[1] = "Item B";
            a[2] = "Item C";
            a[3] = "Item D";
            
            ConcreteIterator i = new ConcreteIterator(a);
            object item = i.First();
            while(item != null)
            {
                Console.WriteLine(item);
                item = i.Next();
            }

            Console.ReadKey();

        }

    }

    abstract class Aggregate
    {
        public abstract Iterator CreateIterator();
    }

    class ConcreteAggregate : Aggregate
    {
        ArrayList _items = new ArrayList();

        public override Iterator CreateIterator()
        {
            return new ConcreteIterator(this);
        }

        public int Count
        {
            get{return _items.Count;}
        }

        public object this [int index]
        {
            get{return _items[index];}
            set{_items.Insert(index,value);}
        }

    }

    abstract class Iterator 
    {
        public abstract object First();
        public abstract object Next();
        public abstract object CurrentItem();
        public abstract bool IsDone();
    }

    class ConcreteIterator : Iterator
    {
        private ConcreteAggregate _aggregate;
        private int _currentIndex = 0;

        public ConcreteIterator(ConcreteAggregate a)
        {
            this._aggregate = a;
        }

        public override object First()
        {
            return _aggregate[0];
        }

        public override object Next()
        {
            object _ret = null;
            if(_currentIndex < (_aggregate.Count - 1))
            {
                _currentIndex = _currentIndex + 1;
                _ret = _aggregate[_currentIndex];
            }

            return _ret;
        }

        public override object CurrentItem()
        {
            return _aggregate[_currentIndex];
        }

        public override bool IsDone()
        {
            return _currentIndex >= _aggregate.Count;
        }
    }
}

Output Code

Item A
Item B
Item C
Item D

 

Categories: C#, Design Pattern, Programming

Factory Method

April 19, 2014 Leave a comment

Frequency of Use
High

Intent
Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

Structure

Factory Method

Factory Method

Applicability
Use the Factory Method pattern when:

  • a class can’t anticipate the class of objects it must create.
  • a class wants its subclasses to specify the objects it creates.
  • classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclass is the delegate.

Code Implementation (C#)

using System;

namespace FactoryMethod
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            Creator[] creators = new Creator[2];
            creators[0] = new ConcreteCreatorA();
            creators[1] = new ConcreteCreatorB();

            foreach(Creator creator in creators)
            {
                Product product = creator.FactoryMethod();
                Console.WriteLine("Created: " + product.GetType().Name);
            }

            Console.ReadKey();
        }
    }

    abstract class Creator
    {
        public abstract Product FactoryMethod();
    }

    class ConcreteCreatorA : Creator
    {
        public override Product FactoryMethod()
        {
            return new ProductA();
        }
    }

    class ConcreteCreatorB : Creator
    {
        public override Product FactoryMethod()
        {
            return new ProductB();
        }
    }

    abstract class Product
    {

    }

    class ProductA : Product
    {

    }

    class ProductB : Product
    {

    }

}

Output Code

Created: ProductA
Created: ProductB

 

Categories: C#, Design Pattern, Programming

Abstract Factory

April 13, 2014 Leave a comment

Frequency of Use
High

Intent
Provide an interface for creating families of related or dependent objects without specifying their concrete classes.

Structure

Abstract Factory Pattern

Applicability
Use the Abstract Factory pattern when:

  • a system should be independent of how its products are created, composed, and represented.
  • a system should be configured with one of multiple families of products.
  • a family of related product objects is designed to be used together, and you need to enforce this constraint.
  • you want to provide a class library of products, and you want to reveal just their interfaces, not their implementations.

Code Implementation (C#) :

using System;

namespace AbstractFactoryExample
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            var factory1 = new ConcreteFactory1();
            var client1 = new Client(factory1);
            client1.run();
            
            var factory2 = new ConcreteFactory2();
            var client2 = new Client(factory2);
            client2.run();
        }
    }

    abstract class AbstractFactory
    {
        abstract public AbstractProductA CreateProductA();
        abstract public AbstractProductB CreateProductB();
    }
    
    class ConcreteFactory1 : AbstractFactory
    {
        public override AbstractProductA CreateProductA()
        {
            return new ProductA1();
        }
        
        public override AbstractProductB CreateProductB()
        {
            return new ProductB1();        
        }
    }
    
    class ConcreteFactory2 : AbstractFactory
    {
        public override AbstractProductA CreateProductA()
        {
            return new ProductA2();
        }
        
        public override AbstractProductB CreateProductB()
        {
            return new ProductB2();
        }
    }
    
    abstract class AbstractProductA
    {
        
    }
    
    class ProductA1 : AbstractProductA
    {
        
    }
    
    class ProductA2 : AbstractProductA
    {
        
    }
    
    abstract class AbstractProductB
    {
        abstract public void Interact(AbstractProductA productA);
    }
    
    class ProductB1 : AbstractProductB
    {
        public override void Interact(AbstractProductA productA)
        {
            Console.WriteLine(this.GetType().Name + 
                              " Interact With " + 
                              productA.GetType().Name);
        }
    }
    
    class ProductB2 : AbstractProductB
    {
        public override void Interact(AbstractProductA productA)
        {
            Console.WriteLine(this.GetType().Name + 
                              " Interact With " + 
                              productA.GetType().Name);
        }
    }

    class Client
    {
        private AbstractProductA _productA;
        private AbstractProductB _productB;
        
        public Client(AbstractFactory factory)
        {
            _productA = factory.CreateProductA();
            _productB = factory.CreateProductB();
        }

        public void run()
        {
            _productB.Interact(_productA);
        }
    }
}

Output Code

ProductB1 Interact With ProductA1
ProductB2 Interact With ProductA2
Categories: C#, Design Pattern, Programming