What is a Design Pattern?
Design Pattern is a re-usable, high quality solution to a given requirement, task or recurring problem. Further, it does not comprise of a complete solution that may be instantly converted to a code component, rather it provides a framework for how to solve a problem.
In 1994, the release of the book Design Patterns, Elements of Reusable Object Oriented Software made design patterns popular.
Because design patterns consist of proven reusable architectural concepts, they are reliable and they speed up software development process.
Design Patterns are in a continious phase of evolution, which means that they keep on getting better & better as they are tested against time, reliability and subjected to continious improvements. Further, design patterns have evolved towards targeting specific domains. For example, windows-based banking applications are usually based on singleton patterns, e-commerce web applications are based on the MVC (Model-View-Controller) pattern.
Design Patterns are categorized into 3 types:
What are Creational Design Patterns?
The Creational Design Patterns focus on how objects are created and utilized in an application. They tackle the aspects of when and how objects are created, keeping in mind whats the best way these objects should be created.
Listed below are some of the commonly known Creational Design Patterns:
>>> Abstract Factory Pattern
>>> Factory Pattern
>>> Builder Pattern
>>> Lazy Pattern
>>> Prototype Pattern
>>> Singleton Pattern
Whats the difference between Abstract Factory Pattern and Factory Pattern?
In an abstract factory design, a framework is provided for creating sub-components that inherit from a common component. In .NET, this is achieved by creating classes that implement a common interface or a set of interfaces, where the interface comprises of the generic method declarations that are passed on to the sub-components. TNote that not just interfaces, but even abstract classes can provide the platform of creating an application based on the abstract factory pattern.
Example, say a class called CentralGovernmentRules is the abstract factory class, comprised of methods like ShouldHavePolice() and ShouldHaveCourts(). There may be several sub-classes like State1Rules, State2Rules etc. created that inheriting the class CentralGovernmentRules, and thus deriving its methods as well.
Note that the term “Factory” refers to the location in the code where the code is created.
A Factory Pattern is again an Object creation pattern. Here objects are created without knowing the class of the object. Sounds strange? Well, actually this means that the object is created by a method of the class, and not by the class’s constructor. So basically the Factory Pattern is used wherever sub classes are given the priviledge of instantiating a method that can create an object.
Describe the Builder Design Pattern
In a builder design pattern, an object creation process is separated from the object design construct. This is useful becuase the same method that deals with construction of the object, can be used to construct different design constructs.
What is the Lazy Design Pattern?
The approach of the Lazy Design Pattern is not to create objects until a specific requirement matches, and when it matches, object creation is triggered. A simple example of this pattern is a Job Portal application. Say you register yourself in that site thus filling up the registration table, only when the registration table is filled, the other objects are created and invoked, that prompt you to fill in other details too, which will be saved in other tables.
What is the Prototype Design Pattern?
A prototype design pattern relies on creation of clones rather than objects. Here, we avoid using the keyword ‘new’ to prevent overheads.
What is the Singleton Design Pattern?
The Singleton design pattern is based on the concept of restricting the instantiation of a class to one object. Say one object needs to perform the role of a coordinator between various instances of the application that depend on a common object, we may design an application using a Singleton. Usage of Singleton patterns is common in Banking, Financial and Travel based applications where the singleton object consists of the network related information.
A singleton class may be used to instantiate an object of it, only if that object does not already exist. In case the object exists, a reference to the existing object is given. A singleton object has one global point of access to it.
An ASP.NET Web Farm is also based on the Singleton pattern. In a Web Farm, the web application resides on several web servers. The session state is handled by a Singleton object in the form of the aspnet_state.exe, that interacts with the ASP.NET worker process running on each web server. Note that the worker process is the aspnet_wp.exe process. Imagine one of the web servers shutting down, the singleton object aspnet_state.exe still maintains the session state information across all web servers in the web farm.
In .NET, in order to create a singleton, a class is created with a private constructor, and a “static readonly” variable as the member that behaves as the instance.
What are Structural Design Patterns?
A structural design pattern establishes a relationship between entities. Thus making it easier for different components of an application to interact with each other. Following are some of the commonly known structural patterns:
>>> Adapter Pattern – Interfaces of classes vary depending on the requirement.
>>> Bridge Pattern – Class level abstraction is separated from its implementation.
>>> Composite Pattern – Individual objects & a group of objects are treated similarly in this approach.
>>> Decorator Pattern – Functionality is assigned to an object.
>>> Facade Pattern – A common interface is created for a group of interfaces sharing a similarity.
>>> Flyweight Pattern – The concept of sharing a group of small sized objects.
>>> Proxy Pattern – When an object is complex and needs to be shared, its copies are made. These copies are called the proxy objects.
What are the different types of Proxy Patterns?
1 – Remote Proxy – A reference is given to a different object in a different memory location. This may be on a different or a same machine.
2 – Virtual Proxy – This kind of object is created only & only when really required because of its memory usage.
3 – Cache Proxy – An object that behaves as a temporary storage so that multiple applications may use it. For example, in ASP.NET when a page or a user control contains the OutputCache directive, that page/control is cached for some time on the ASP.NET web server.
What is a behavioral design pattern?
Behaviorial design patterns focus on improving the communication between different objects. Following are different types of behavioral patterns:
>>> Chain Or Responsibilities Pattern – In this pattern, objects communicate with each other depending on logical decisions made by a class.
>>> Command Pattern – In this pattern, objects encapsulate methods and the parameters passed to them.
>>> Observer Pattern – Objects are created depending on an events results, for which there are event handlers created.
What is the MVC Pattern (Model View Controller Pattern)?
The MVC Pattern (Model View Controller Pattern) is based on the concept of designing an application by dividing its functionalities into 3 layers. Its like a triad of components. The Model component contains the business logic, or the other set of re-usable classes like classes pertaining to data access, custom control classes, application configuration classes etc. The Controller component interacts with the Model whenever required. The control contains events and methods inside it, which are raised from the UI which is the View component.
Consider an ASP.NET web application. Here, all aspx, ascx, master pages represent the View.
The code behind files (like aspx.cs, master.cs, ascx.cs) represent the Controller.
The classes contained in the App_Code folder, or rather any other class project being referenced from this application represent the Model component.
Advantages: * Business logic can be easily modified, without affecting or any need to make changes in the UI.
* Any cosmetic change in the UI does not affect any other component.
What is the Gang of Four Design Pattern?
The history of all design patterns used in modern day applications derive from the Gang of Four (GoF) Pattern. Gang of Four patterns are categorized into 3 types:
1 – Creational
2 – Structural
3 – Behavioral
The term “Gang of Four” (or “GoF” in acronym) is used to refer to the four authors of the book Design Patterns: Elements of Reusable Object-Oriented Software. The authors are Erich Gamma, Ralph Johnson, Richard Helm and John Vlissides.
When should design patterns be used?
While developing software applications, sound knowledge of industry proven design patterns make the development journey easy and successful. Whenever a requirement is recurring, a suitable design pattern should be identified. Usage of optimal design patterns enhance performance of the application. Though there are some caveats. Make sure that there are no overheads imposed on a simple requirement, which means that design patterns should not be unnecessarily be used.
How many design patterns can be created in .NET?
As many as one can think. Design patterns are not technology specific, rather their foundation relies on the concept of reusability, object creation and communication. Design patterns can be created in any language.