Pages

Saturday, 11 January 2014

In this article we are going to see how to use the Async and Await and what is the difference the Async -await and old Asynchronus format.In Generally when we using the Async amd await keyword the method should have a Async along with Task Return type. then we can mention the await keyword wherever the async method is call. Why we have to mention the Await instead of just mention the Async as in the return type, it is because in the Async process the execution of method is invoked and then the object is get disposed by the next line of execution, to avoid this we have to make await.What is the Difference between the Async-Await and Old format async programing ? Generally if we are writing a asynchronous programming developers thought is , the thread is running in the background and the UI responsive will be in good state. Then this is the same for Async-Await , If we really see deeply there is a big difference is present, Actually when we use the thread to run in background it will run ,but when the I/O bound operation comes the thread execution is sleep or pause and the I/O operation starts the process when the I/O operation ends a signal is sent to the thread to start the thread from interrupt. So there is a Wait and sleep is maintain once the I/O operation starts. but in the Async-Await the program is running in background and when the I/O operation reaches then the I/O operation is execution starts but the Thread starts executing the next line of code instead of wait and it will receive a signal from the I/O once operation completes. So I/O operation bound is a major difference between the Thread in Asynchronous and Async-Await.Now let we see in programming Asynchronous programming in C# 4

Friday, 10 January 2014

In this article we are going to see the Adapter design pattern and there implementation, Adapter design pattern is convert a interface of a class in to a client required interface, it is acts as a intermediate layer between the client and the target class.

In this article we are going to see the how to implement the Abstract Factory design pattern in code, Abstract Factory design is creating a abstract class and a abstract method which will return the required instance based on the input parameter type and create a Factory by the static method.

Now in this sample we take a Interface IAnimal which have a method speak, now this is imlpement for many animals cow, dog, lion, shark, octopus.

publicinterfaceIAnimal

{

string Speak();

}

classDog:IAnimal

{

publicstring Speak()

{

return"Bark";

}

}

classLion:IAnimal

{

publicstring Speak()

{

return"Roar";

}

}

classCat:IAnimal

{

publicstring Speak()

{

return"Meow";

}

}

classOctopus:IAnimal

{

publicstring Speak()

{

return"Whack";

}

}

classWhale:IAnimal

{

publicstring Speak()

{

return"No Voice";

}

}Declare the Enum Types

publicenumAnimalType

{

Lion,

Dog,

Cat,

Whale,

Octopus,

}

publicenumFactoryType

{

LandAnimalFactory,

SeaAnimalFactory

}

Now Let we create a Abstract Factory class with static method return the child instance type ,based on the input. Create a Two derived class from the abstract factory as LandAnimalFactory and SeaAnimalFactory

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace AbstractFactoryDesign

{

publicabstractclassAnimalFactory

{

publicabstractIAnimal GetAnimal(AnimalType animalType);

publicstaticAnimalFactory GetAnimalFactory(FactoryType factoryType)

{

switch(factoryType)

{

caseFactoryType.LandAnimalFactory:

returnnewLandAnimalFactory();

caseFactoryType.SeaAnimalFactory:

returnnewSeaAnimalFactory();

default :

returnnull;

}

}

}

}

classLandAnimalFactory:AnimalFactory

{

publicoverrideIAnimal GetAnimal(AnimalType animalType)

{

switch(animalType)

{

caseAnimalType.Cat:

returnnewCat();

caseAnimalType.Dog:

returnnewDog();

caseAnimalType.Lion:

returnnewLion();

default :

returnnull;

}

}

}

classSeaAnimalFactory:AnimalFactory

{

publicoverrideIAnimal GetAnimal(AnimalType animalType)

{

switch(animalType)

{

caseAnimalType.Octopus:

returnnewOctopus();

caseAnimalType.Whale:

returnnewWhale();

default :

returnnull;

}

}

}

I hope from this article you can learn how to implement the abstract factory design pattern in code.