Monday, December 6, 2010

Dot Net Patterns

As per my understanding design is a blue print and pattern means idea or guideline for something in mind that occurs regularly, which is used to solve the common problem in software design. Design patterns can speed up the development process by providing tested, proven development paradigm. For me the definition of design patterns is creating something that repeats as a guideline.

They are very useful to solve a complex problem if they used properly.

It provides core solution to the problems so that you can implement the exact solution to any problems. They are about objects and interaction of objects. For an example Normalization is a pattern (Core solution) however level of normalization depends on the requirement (exact solution). They are used throughout the asp.net framework.

Broadly they are divided into three Main categories

Creational Patterns

- Abstract Factory

- Builder

- Factory

- Prototype

- Singleton

Behavior Patterns

- Observer

- Visitor

- State Chain of responsibility

- Interpreter

- Command Mediator

- Strategy Template Method

- IteratorMemento

Structural Patterns

- Adaptor

- Bridge

- Facade

- Decorator

- Composite

- Flyweight Proxy

Patterns are not easy to understand it requires extra effort but remember before moving ahead you would need to understand and remember the features of patterns so that you can implement the correct pattern. Here I am not going to discuss all above mentioned patterns because I am not aware of them. I will cover only Adaptor Pattern which I have used recently in my project.

Adaptor Pattern

Example: Let's assume we have used some library where we have One Function which takes 2 integers and return the multiplication of them.

Example: Let's assume we have used some library where we have One Function which takes 2 integers and return the multiplication of them. Now the library has changed and the same function takes two decimal/floating numbers, here adaptor patterns comes into pictures otherwise we have to change the client code. Adaptor patterns use those interfaces which client's understand or in other words it convert the interface of a class into another interface as client expect. This enables systems to use classes whose interface don't quite match's.

It is useful when we are having very old code that was written long back and we don't have access.

E.g.

class LegacyCode // Old Code

{

public bool IsEmailID (string email)

{

return System.Text.RegularExpressions.Regex.IsMatch

(email, @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");

}

}

// Standard Implementation

interface Iemail

{

bool ValidateEmail(string email);

}

// Implementing the required standard via Old code

class Adapter: LegacyCode, Iemail

{

public bool ValidateEmail(string email)

{

return IsEmailID (email);

}

}

class Client

{static void Main()

{

Iemail objEmail = new Adapter();

Console.WriteLine("\Adaptor Pattern Used.. New Standard");

Console.WriteLine(objEmail.ValidateEmail(myemail@hotmail.com));

}}

!!!Happy coding!!!

No comments:

Post a Comment