Thursday, September 17, 2009

Design Patterns


What are design patterns ?
Design patterns are recurring solution to recurring problems in software architecture.

Design Patterns are basically divided into 3 classifactions.
Creational patterns.
Structural patterns.
Behavioral patterns.

1) Creational Patterns
√ Abstract Factory:- Creates an instance of several families of classes
√ Builder :- Separates object construction from its representation
√ Factory Method:- Creates an instance of several derived classes
√ Prototype:- A fully initialized instance to be copied or cloned
√ Singleton:- A class in which only a single instance can exist
Note :- The best way to remember Creational pattern is by ABFPS (Abraham Became First President of States).


2) Structural Patterns
√ Adapter:-Match interfaces of different classes.
√ Bridge:-Separates an object’s interface from its implementation.
√ Composite:-A tree structure of simple and composite objects.
√ Decorator :-Add responsibilities to objects dynamically.
√ Façade:-A single class that represents an entire subsystem.
√ Flyweight:-A fine-grained instance used for efficient sharing.
√ Proxy:-An object representing another object. 8..NET Architecture229
Note : To remember structural pattern best is (ABCDFFP)


3) Behavioral Patterns
√ Mediator:-Defines simplified communication between classes.
√ Memento:-Capture and restore an object's internal state.
√ Interpreter:-A way to include language elements in a program.
√ Iterator:-Sequentially access the elements of a collection.
√ Chain of Resp:-A way of passing a request between a chain of objects.
√ Command:-Encapsulate a command request as an object.
√ State:-Alter an object's behavior when its state changes.
√ Strategy:-Encapsulates an algorithm inside a class.
√ Observer:-A way of notifying change to a number of classes.
√ Template Method:-Defer the exact steps of an algorithm to a subclass.
√ Visitor:-Defines a new operation to a class without change.
Note :- Just remember Music....... 2 MICS On TV (MMIICCSSOTV). Note:- No source code is provided for architecture section as much of the things can be clear from good UML diagrams.


What is MVC pattern?
The main purpose using MVC pattern is to decouple the GUI from the Data. It also gives the ability to provide multiple views for the same Data. MVC pattern separates objects in to three important sections:-
Model: - This section is specially for maintaining data. It is actually where your business logic, querying database, database connection etc. is actually implemented.
Views: - Displaying all or some portion of data, or probably different view of data. View is responsible for look and feel, Sorting, formatting etc.
Controller: - They are event handling section which affects either the model or the view. Controller responds to the mouse or keyboard input to command model and view to change. Controllers are associated with views. User interaction triggers the events to change the model, which in turn calls some methods of model to update its state to notify other registered views to refresh their display.

MVC sections:-
Model: - This section is represented by Data view, Dataset, Typed Dataset, Business components, business entity models etc. Now this section can then be tied up to either windows application or web UI.
View: - ASPX, ASCX, or windows application UI like data grid etc. form the view part of it.
Controller: - In ASP.NET the behind code is the controller as the events are handled by that part. Controller communicates both with Model as well as view. I hope I was able to map you imagination of ASP.NET with the diagram given below.


How can we implement singleton pattern in .NET?
Singleton pattern mainly focuses on having one and only one instance of the object running. Example a windows directory service which has multiple entries but you can only have single instance of it through out the network.
Note:- May of developers would jump to a conclusion saying using the "STATIC" keyword we can have a single instance of object. But that’s not the real case there is something more that has to be done. But please note we can not define a class as STATIC, so this will not serve our actual purpose of implementing singleton pattern.

Following are the three steps needed to implement singleton pattern in .NET:-
√ First create your class with static members.
Public class ClsStaticClass
Private shared objCustomer as clsCustomer
End class
This ensures that there is actually only one Customer object throughout the project.
√ Second define a private constructor to your class. Note: - defining a private constructor to class does not allow a client to create objects directly.
√ Finally provide a static method to get access to your singleton object.

No comments:

Post a Comment