Saturday, November 13, 2010

.....Behavioral Pattern

Behavioral patterns are those patterns that are most specifically concerned with communication between objects.

The interactions between the objects should be such that they are talking to each other and still are loosely coupled. The loose coupling is the key to n-tier architectures. In this, the implementation and the client should be loosely coupled in order to avoid hard-coding and dependencies.

There are eleven patterns under this category:-

Chain of Responsibility :- The chain of responsibility pattern is a design pattern that defines a linked list of handlers, each of which is able to process requests. When a request is submitted to the chain, it is passed to the first handler in the list that is able to process it. Ex. Exception Handling is an example of this pattern.

Command Pattern :- The Command design pattern encapsulates the concept of the command into an object.The issuer holds a reference to the command object rather than to the recipient.The issuer sends the command to the command object by executing a specific method on it.The command object is then responsible for dispatching the command to a specific recipient to get the job done. Delegate -Event an example of command pattern.

Interpreter Pattern :- The Interpreter Pattern defines a grammatical representation for a language and an interpreter to interpret the grammar. The best example you can get for this is Java itself which is an interpreted language. It converts the code written in English to a byte code format so as to make possible for all the operating systems to understand it. This quality of it makes it platform independent.

Iterator Pattern :- The Iterator pattern allows you to move through a list or collection of data using a standard interface without having to know the details of the internal representations of that data. Ex. Enumerator

Mediator Pattern :- Mediator pattern, used to handle complex communications between related objects, helping with decoupling of those objects.Ex. Just like all airplanes contact to airplane control Tower to confirm whether they should land or not, they do not communicate to each other.

Momento Pattern :- The memento design pattern is a pattern that helps to save the object's internal state in an external place enabling us to restore the state later when needed. The memento pattern doesn’t violate encapsulation of the internal state. The pattern is rarely used but it’s very helpful in scientific computing or in computer games (saving of check points in the game for example).

Observer :- Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.When we call AcceptChanges method of dataset all its row's status has been changed, this is the best example of Observer pattern. MVVM Pattern is an example of Observer pattern where when properties changes than the control bind with them are changes as well.

State :- Allow an object to alter its behavior when its internal state changes.

Strategy :- Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

Template Pattern :- It is used to set up the outline or skeleton of an algorithm, leaving the details to specific implementations later. This way, subclasses can override parts of the algorithm without changing its overall structure.Ex.. When we use interface or abstract class simply actually at that way we are using Template pattern.

Visitor Pattern :- The visitor design pattern is a way of separating an algorithm from an object structure it operates on. A practical result of this separation is the ability to add new operations to existing object structures without modifying those structures.

If a Collection contains different classes objects. And we have to iterate the collection than we have to typecast each object to its proper type means we have to use if-else ladder. The best solution for this problem is used Visitor Pattern. Implements all classes with a common interface and again when you will iterate the collection you have to typecast only once in interface type.

No comments: