Tuesday 3 January 2012

Design Patterns


What are design patterns?
Design patterns are recurring solution to recurring problems in software architecture.
What are types of Design Patterns?
There are three basic classification of patterns Creational, Structural and Behavioral patterns.
Creational Patterns
1. Abstract Factory - Creates an instance of several families of classes
2. Builder - Separates object construction from its representation
3. Factory Method - Creates an instance of several derived classes
4. Prototype - A fully initialized instance to be copied or cloned
5. 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).
Structural Patterns
1. Adapter - Match interfaces of different classes.
2. Bridge - Separates an object’s interface from its implementation.
3. Composite - A tree structure of simple and composite objects.
4. Decorator - Add responsibilities to objects dynamically.
5. Façade - A single class that represents an entire subsystem.
6. Flyweight - A fine-grained instance used for efficient sharing.
7. Proxy - An object representing another object.
To remember structural pattern best is (ABCDFFP)
Behavioral Patterns
1. Mediator - Defines simplified communication between classes.
2. Memento - Capture and restore an object's internal state.
3. Interpreter - A way to include language elements in a program.
4. Iterator - Sequentially access the elements of a collection.
5. Chain of Resp - A way of passing a request between a chain of objects.
6. Command - Encapsulate a command request as an object.
7. State - Alter an object's behavior when its state changes.
8. Strategy - Encapsulates an algorithm inside a class.
9. Observer - A way of notifying change to a number of classes.
10. Template Method:-Defer the exact steps of an algorithm to a subclass.
11. Visitor - Defines a new operation to a class without change.
Note :- Just remember (MMIICCSSOTV).
What is the difference between Factory and Abstract Factory Patterns?
The common thing they have is that they belong to creational patterns. In short they hide the complexity of creating objects. The main difference between factory and Abstract factory is factory method uses inheritance to decide which object has to be instantiated while abstract factory uses delegation to decide instantiation of object. We can say Abstract factory uses factory method to complete the architecture. Abstract Factory is one level higher in abstraction over Factory.
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:-
1. Model - This section is specially for maintaining data. It is actually where your business logic, querying database, database connection etc. is actually implemented.
2. Views - Displaying all or some portion of data, or probably different view of data. View is responsible for look and feel, Sorting, formatting etc.
3. 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. Ok now this was all in theory. Let us look at how in actually ASP.NET we can implement MVC pattern. Following are the various sections of ASP.NET which maps to MVC sections:-
1. 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.
2. View - ASPX, ASCX, or windows application UI like data grid etc. form the view part of it.
3. 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.
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 - Many 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:-
1. 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.
2. Second define a private constructor to your class.
Note - Defining a private constructor to class does not allow a client to create objects directly.
3. Finally provide a static method to get access to your singleton object.
What are the situations you will use a Web Service and Remoting in projects?
Well "Web services" uses "remoting" concepts internally. But the major difference between "web service" and "remoting" is that "web service" can be consumed by clients who are not .NET platform. While remoting you need the client to be .NET compliant. Regarding the speed issue "Remoting" is faster than "Web Services". So I think when deciding the architecture side of choosing between "Web services" and "Remoting" keep the cross platform issue and the speed issue in mind.
How can we implement observer pattern in .NET?
Observer patterns can be implemented using "Delegates" and "Events". I leave this to the readers to implement one sample code for observer patterns.
What is three tier architecture?
The three tier software architecture emerged in the 1990s to overcome the limitations of the two tier architecture. There are three layers when we talk about three tier architecture:-
1. User Interface (Client) - This is mostly the windows user interface or the Web interface but this has only the UI part.
2. Mid layer - Middle tier provides process management where business logic and rules are executed and can accommodate hundreds of users (as compared to only 100 users with the two tier architecture) by providing functions such as queuing, application execution, and database staging.
3. Data Access Layer - This is also called by the famous acronym "DAL" component. It has mainly the SQL statement which do the database operation part of the job. The three tier architecture is used when an effective distributed client/server design is needed that provides (when compared to the two tier) increased performance, flexibility, maintainability, reusability, and scalability, while hiding the complexity of distributed processing from the user.
What are Microsoft Application Blocks?
Application Blocks are C# and VB.NET classes distributed as Visual Studio projects that can be downloaded from Microsoft's Web site and used in any .NET application, including ASP.NET Web applications. They are useful and powerful tools that can make applications more maintainable, scalable and efficient Secondly which application blocks has been used depends on really what you have implemented. But there are two famous MAB which is making buzz around the industry:-
1. Data access block - The Data Access Block provides static methods located in the SqlHelper class that encapsulates the most common data access tasks performed with Microsoft SQL server. If the term "static method" is new to you, it means that the class methods can be called without instantiating an instance of the class. For example, the method ExecuteReader () within the SqlHelper class can be called by simply using the statement SqlHelper.ExecuteReader () no object instantiation of the SqlHelper class is required.
2. Exception management block - The Exception Management Application Block provides a simple yet extensible framework for handling exceptions. With a single line of application code you can easily log exception information to the Event Log or extend it by creating your own components that log exception details to other data sources or notify operators, without affecting your application code. The Exception Management Application Block can easily be used as a building block in your own .NET application.
What is Service Oriented architecture?
"Services" are components which expose well defined interfaces and these interfaces communicate through XML messages. Using SOA you can build workflow, which uses interfaces of these components. SOA is typically useful when you are crossing heterogeneous technical boundaries, organizations, domain etc. In .NET SOA technically uses Web services to communicate with each service which is crossing boundaries. You can look SOA which sits on top of web services and provides a workflow. SOA uses service components which operate in their own domain boundary. Let us note some points of service :-
1. They are independent components and operate in their own boundary and own technology.
2. They have well defined interfaces which use XML and WSDL to describe themselves.
3. Services have URL where anyone can find them and clients can bind to these URL to avail for the service.
4. Services have very loosely coupled architecture. In order to communicate to service you only have to know the WSDL. Your client can then generate proxy from the WSDL of the service.
What are different ways you can pass data between tiers?
There are many ways you can pass data between tiers :-
1. Dataset the most preferred one as they maintain data in XML format.
2. Datareader.
3. Custom classes.
4. XML.


No comments:

Post a Comment