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

Wednesday, December 15, 2010

Counter-Queue and Counter-Wait Pool Patterns for SaaS Implementations

SaaS or Software as a Service is the upcoming paradigm shift in the IT industry. Everyone is shifting towards converting their offerings to Cloud platforms. Some of the key things in this model are the; 
  • Effective utilization of the resources available
  • Satisfy as much service requests as possible with the available resources
  • High Throughput

 The Counter-Queue Pattern is an attempt to increase the productivity of the SaaS offerings using the real life scenario of Ticket booking counters in the Railways Stations. Like in the booking counters, the service requests will be piled up in the queues available and executed in parallel. Say we plan to have 10 queues; the service requests available in the 10 queues will be executed in parallel. The diagram below pictorially explains the Counter-Queue pattern;

  1. Initially, the service requests will be put in the “Input Queue”
  2. The queue manager reads the service requests in the “Input Queue” and allocates them to the “Counter Queues” available. Each counter queue will have the max queue size, depending on that, the queue manager allocates the service requests in them
  3. Each counter will perform the similar functionality. They will pick up the service requests in their respective counter queues and perform the necessary functionality and put the result in the “Output Queue”

In case, any counter fails; the service requests will be moved back to the end of the “Input Queue” for re-allotment. As you can notice, the problem here is that when any counter fails; SLA of the service requests in those queues will not be met.

The “Counter-Wait Pool pattern” will solve this problem. The below diagram pictorially explains this pattern


  1. Initially, the service requests will be piled up in the input queue
  2. The queue manager picks the service requests from the “Input Queue” and assigns a sequential number to it and put in the wait pool. There will be a max size set to the wait pool. Depending on that, queue manager will put the service requests in the wait pool.
  3. Each counter will have “Next token” display in them. Depending on the number displayed, the service requests will be picked from the wait pool.
  4. Once the counter services the request, the result will be put in the “Output Queue”.

As you can notice, here we will not run into the problem of not meeting the SLA of the service requests on any scenario 

Tuesday, November 9, 2010

Abstract Factory pattern in simple terms

Just tried out abstract factory pattern... One of my colleague had issues coming up with the pattern.. Just helped him with the sample below;

Invocation point:
var obj = FactoryManager.GetFactory(1);

Implementation:

namespace ConsoleApplication2
{
    internal class RootDO
    {

    }

    internal sealed class SubDO1 : RootDO
    {

    }

    internal sealed class SubDO2 : RootDO
    {

    }

    internal abstract class RootClass<T> where T: RootDO ,new ()
    {

    }

    internal sealed class SubClass1<T> : RootClass<T> where T: RootDO,new ()
    {

    }

    internal sealed class SubClass2<T> : RootClass<T> where T: RootDO, new ()
    {

    }
    internal interface IFactory<S, T>
    {
         S GetObject();
    }

    internal class SubClass1Factory : IFactory<SubClass1<SubDO1>,SubDO1>
    {
        public SubClass1<SubDO1> GetObject()
        {
            return new SubClass1<SubDO1>();
        }

        internal static SubClass1<SubDO1> GetFactory()
        {
            SubClass1Factory s = new SubClass1Factory();
            return s.GetObject();
        }
    }

    public class FactoryManager
    {
        public static object GetFactory(int type)
        {
            return SubClass1Factory.GetFactory();
        }
    }
}
 



Tuesday, November 3, 2009

Business Action Model - Design Pattern

Recently, I had designed an Orchestration functionality using the "Business Action Model" design model. Thought of sharing this. Find below the UML class diagram for the same;





Tuesday, October 13, 2009

Design Patterns - Fire and Forget Pattern

Often in modeling for application development, you want a process to call another process and continue the process flow without waiting for a response from the called process. This pattern is called the "fire and forget" pattern.
This can be achieved in C# using the delegates.
Delegate has the following options which will help to achieve the fire and forget pattern.
Invoke (Synchronous)
BeginInvoke and EndInvoke (Asynchronous)
DynamicInvoke (Synchronous)

The difference between Invoke and DynamicInvoke is that, Invoke needs the instance of object to execute the method while DynamicInvoke doesn't need. We will be using BeginInvoke, EndInvoke and DynamicInvoke to achieve the "Fire and Forget" pattern.

public class ThreadUtils

{
delegate void DelegateWrapper(Delegate d, object[] args);
static DelegateWrapper wrapperInstance = new DelegateWrapper(InvokeWrappedDelegate);
static AsyncCallback callback = new AsyncCallback(EndWrapperInvoke);

public static void FireAndForget(Delegate d, params object[] args)
{
wrapperInstance.BeginInvoke(d, args, callback, null);
}

static void InvokeWrappedDelegate(Delegate d, object[] args)
{
d.DynamicInvoke(args);
}

static void EndWrapperInvoke(IAsyncResult ar)
{
wrapperInstance.EndInvoke(ar);
ar.AsyncWaitHandle.Close();
}


This "ThreadUtils" class is provided by Peter A Boomberg in his article on "Fire and Forget" pattern.

FireAndForget method takes delegate as input and invokes it's own delegate asynchronously and that delegate executes the one you have provided synchronosuly. At the same time, ThreadUtils class takes care of the dirty job of calling EndInvoke on the internal delegate using the AsynCallback mechanism.

Invoking code

class Program

{
delegate void InsertDelegate(string first, string second, int num);

static void Main(string[] args)
{
Delegate dWrite = new InsertDelegate(WriteIt);
for (int i = 0; i < 1000; i++)
{
ThreadUtils.FireAndForget(dWrite, new object[] { "hoohoo", "hahaa", i });
}
}
static void WriteIt(string first, string second, int num)
{
//do some intesive IO operations
}

Monday, October 12, 2009

Extension Methods in C#

Recently, I come across with the blog of Troelsen. He was mentioning that "Extension" methods in .NET 3.5 are good example of "Decorator Pattern". Here is the sample for extension method which satisfies the "Decorator Pattern" definition.

Decorator Pattern - Definition:

The decorator pattern provides a formal way to add new functionality to an existing type, without sub-classing. This will be useful when the class is sealed and classic inheritance will be not feasible to use.

Sample code:


public class TextMessage
{

string _message = string.Empty;
public string Message
{

get { return _message; }

set { _message = value; }

}

public void Display()
{

Console.WriteLine("Here we go: " + _message);

}

}

public static class TextMessageExtensions
{
public static void DisplayWithHello(this TextMessage tm)
{

Console.WriteLine("Hello");

tm.Display();
}

public static void DisplayWithHi(this TextMessage tm)
{

Console.WriteLine("Hi");

tm.Display();

}

}

Wednesday, October 7, 2009

Behavioral Pattern - Mediator Pattern

Definition:


Mediator defines an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.


UML Diagram:

















Sample code:


Mediator:


public enum BillerType
    {
        Vodafone = 1,
        BESCOM = 2,
        TataIndicom = 3,
        Airtel = 4
    }
    public interface IBillPayAgent : IDisposable
    {
       
        bool PayBill(double amount);
    }
    public class BillPayAgent : IBillPayAgent
    {
        private BillerType _billerType;
        public BillPayAgent(BillerType billerType)
        {
            this._billerType = billerType;
        }
        private IBiller Biller
        {
            get { return BillerFactory.GetFactory(_billerType); }
        }
        public bool PayBill(double amount)
        {
            using (Biller)
            {
                Biller.ReceiveAmount(amount);
            }
            return true;
        }

        public void Dispose()
        {
            Dispose(true);
        }

        ~BillPayAgent()
        {
            Dispose(false);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
                GC.SuppressFinalize(this);
        }
    }



Supplier:


    public class IBiller : IDisposable
    {
        public virtual void ReceiveAmount(double amount)
        {
            Console.WriteLine("Received Amount: " + amount.ToString());
        }

        public void Dispose()
        {
            Dispose(true);
        }

        ~IBiller()
        {
            Dispose(false);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
                GC.SuppressFinalize(this);
        }
    }

    public sealed class Vodafone : IBiller
    {
        public override void ReceiveAmount(double amount)
        {
            Console.WriteLine("Greetings from Vodafone");
            base.ReceiveAmount(amount);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
                GC.SuppressFinalize(this);
            base.Dispose(disposing);
        }
    }

    public sealed class TataIndicom : IBiller
    {
        public override void ReceiveAmount(double amount)
        {
            Console.WriteLine("Greetings from TataIndicom");
            base.ReceiveAmount(amount);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
                GC.SuppressFinalize(this);
            base.Dispose(disposing);
        }
    }

    public sealed class BESCOM : IBiller
    {
        public override void ReceiveAmount(double amount)
        {
            Console.WriteLine("Greetings from BESCOM");
            base.ReceiveAmount(amount);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
                GC.SuppressFinalize(this);
            base.Dispose(disposing);
        }
    }

    public sealed class Airtel : IBiller
    {
        public override void ReceiveAmount(double amount)
        {
            Console.WriteLine("Greetings from Airtel");
            base.ReceiveAmount(amount);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
                GC.SuppressFinalize(this);
            base.Dispose(disposing);
        }
    }



Consumer:


public class Consumer : IDisposable
    {
        private IBillPayAgent billPayAgent = default(IBillPayAgent);
       
        public void DoShopping()
        {
            Console.WriteLine("I am doing shopping");
        }

        public void PayEBBill(double amount)
        {
            using (billPayAgent = new BillPayAgent(BillerType.BESCOM))
            {
                billPayAgent.PayBill(amount);
            }
        }

        public void PayMobileBill(double amount,BillerType billerType)
        {
            using (billPayAgent = new BillPayAgent(billerType))
            {
                billPayAgent.PayBill(amount);
            }
        }

        public void Dispose()
        {
            Dispose(true);
        }

        ~Consumer()
        {
            Dispose(false);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
                GC.SuppressFinalize(this);
        }
    }

Wednesday, September 30, 2009

Structural Pattern - Facade


Definition:
Provide a unified interface to a set of interfaces in a subsystem. Façade defines a higher-level interface that makes the subsystem easier to use. 

UML Diagram:  











Sample Code:
public sealed class Facade
    {
        SubSystem1 _subSystem1 = default(SubSystem1);
        SubSystem2 _subSystem2 = default(SubSystem2);
        SubSystem3 _subSystem3 = default(SubSystem3);

        public Facade()
        {
            _subSystem1 = new SubSystem1();
            _subSystem2 = new SubSystem2();
            _subSystem3 = new SubSystem3();
        }

        public void Method1()
        {
            _subSystem3.Method1();
            _subSystem1.Action1();
            _subSystem2.Action1();
        }

        public void Method2()
        {
            _subSystem1.Action2();
            _subSystem2.Action1();
            _subSystem2.Action2();
        }
    }

    public class SubSystem1
    {
        public void Action1()
        {
            Console.WriteLine("SubSystem1 -> Action1 called");
        }

        public void Action2()
        {
            Console.WriteLine("SubSystem1 -> Action2 called");
        }
    }
    public class SubSystem2
    {
        public void Action1()
        {
            Console.WriteLine("SubSystem2 -> Action1 called");
        }

        public void Action2()
        {
            Console.WriteLine("SubSystem2 -> Action2 called");
        }
    }

    public class SubSystem3
    {
        public void Method1()
        {
            Console.WriteLine("SubSystem3 -> Action1 called");
        }
    }

Tuesday, September 29, 2009

Creational Pattern - Abstract Factory

Definition:
An abstract factory provides an interface for creating families of related objects without specifying their concrete classes. Sometimes one wants to construct an instance of one of a suite of classes, deciding between the classes at the time of instantiation. In order to avoid duplicating the decision making everywhere an instance is created, we need a mechanism for creating instances of related classes without necessarily knowing which will be instantiated.

UML Diagram:










Sample Code:


Enums & Factories:


    public enum DeviceQuality
    {
        LowQuality = 1,
        AverageQuality = 2,
        GoodQuality = 3
    }
    public enum Device
    {
        CDPlayer = 1,
        DVDPlayer = 2,
        CassettePlayer = 3
    }
    public static class AVDeviceFactory
    {
        static IAVDevice _device = default(IAVDevice);
        public static IAVDevice GetFactory(Device device)
        {
           
            switch (device)
            {
                case Device.CDPlayer:
                    _device = new CDPlayer();
                    break;
            }
            return _device;
        }
    }

    public static class AudioDeviceFactory
    {
        static IAudioDevice _audioDevice = default(IAudioDevice);
        public static IAudioDevice GetFactory(DeviceQuality quality)
        {
           
            switch (quality)
            {
                case DeviceQuality.GoodQuality:
                    _audioDevice = new GoodQualityAudioDevice();
                    break;
            }
            return _audioDevice;
        }
    }

    public static class VideoDeviceFactory
    {
        static IVideoDevice _videoDevice = default(IVideoDevice);
        public static IVideoDevice GetFactory(DeviceQuality quality)
        {
           
            switch (quality)
            {
                case DeviceQuality.GoodQuality:
                    _videoDevice =  new GoodQualityVideoDevice();
                    break;
            }
            return _videoDevice;
        }
    }



Interfaces and Classes:


    public abstract class IAVDevice
    {
        protected IAudioDevice _audioDevice = default(IAudioDevice);
        protected IVideoDevice _videoDevice = default(IVideoDevice);

        public abstract IAudioDevice AudioDevice
        {
            get;
          
        }

        public abstract IVideoDevice VideoDevice
        {
            get;
           
        }
    }

    public interface IAudioDevice
    {
        int GetSoundQuality();
    }
    public interface IVideoDevice
    {
        int GetVideoQuality();
    }

    public sealed class GoodQualityAudioDevice : IAudioDevice
    {
        public int GetSoundQuality()
        {
            return 1;
        }
    }

    public sealed class GoodQualityVideoDevice : IVideoDevice
    {
        public int GetVideoQuality()
        {
            return 1;
        }
    }

    public sealed class CDPlayer : IAVDevice
    {
        public override IAudioDevice AudioDevice
        {
            get
            {
                _audioDevice = AudioDeviceFactory.GetFactory(DeviceQuality.GoodQuality);
                return _audioDevice;
            }
        }
        public override IVideoDevice VideoDevice
        {
            get
            {
                _videoDevice = VideoDeviceFactory.GetFactory(DeviceQuality.GoodQuality);
                return _videoDevice;
            }
        }
    }
}