Quick Start On Design Pattern In C#


I have read many articles about Design patterns and explored them with a lot of examples to understand them better. Here, I would like to present with you on Design pattern simply and easily with pictorial and C# codes. I am sure that, you will find the main use of design patterns and make use of them in your projects.

Why do we have to follow the Design Pattern?

In the Software Development Life Cycle (SDLC), there are many processes involved, starting from Planning to Deployment of the system. Here, the development process is vital and a longer process to execute, where developers start building an application with good and tidy designs using their knowledge and experience according to business requirements. Later on, the application is qualified and ready to deploy in the production environment. But over time, applications might need some more enhancement with the change request by the client and bug fixes. We can’t say no to the client to apply the updates, and that is a part of SDLC. The application design must be altered for every change request or new feature request. Once requirements are in place, sometimes, we might need to put in a lot of effort, even for simple tasks, and it might require full working knowledge of the entire system, and this will leads to more time consuming, more effort, more testing, and more cost.

The three best solutions,

  • Choosing the correct architecture like MVC, 3-tier, MVVM, and so on.
  • Following the Design Principles (SOLID).
  • Choosing the correct Design Patterns to build the software based on its specifications.

What is a Design Pattern?

It is a solution for common problems in software design. They are like a blueprint that helps to customize the recurring design problems in your code/application. Here we can’t just find a pattern and copy it into your application, but it is a general concept for solving a particular problem in your code.

The design pattern is not a simple and not an easy one, but it often confuses to decide to choose a correct design pattern and implement it. These design patterns give you a high-level description of a solution. The code for the same pattern may differ for different applications/programs based on their needs.

What is the scope of this article on Design Patterns?

  • Background: A brief idea of design pattern
  • Real-Time Analogy: Looking at the real-world environment on how design pattern is co-related on the object-oriented world.
  • Pictorial representation: The elaboration on a real-time example.
  • UML representation: Describing a sample code example with UML
  • Sample Code: Demonstration of a design pattern with sample code and observing the outputs.

Why should I learn Design Patterns?

We all might manage our day-to-day programming life for many years without knowing about a single design pattern and most developers do just for that by adding an IF condition or an IF ELSE condition to solve a problem 😊. In the end, all the logic and functionality go at the same place and violating SOLID principles. In the future, any small changes in the code, might break the existing functionality and end up spending lots of time to fix the issue. Refer to the article link – Deep Dive on SOLID Principle. Design patterns help to solve a common problem in software design to accommodate any changes with very minimal effort.

Sometimes, you might be implementing some design patterns without even knowing about them and we should know that how the implementation helps to accommodate upcoming change requests.

These Design patterns are a toolkit for a common problem in software design and those are tried and tested solutions with object-oriented principles.

For an instance, If your application needs a logging implementation, you can say confidently, use the Singleton design pattern for it. Here everyone will understand the idea behind your suggestion. No need to explain on Singleton design pattern.

Classification of Design Patterns

Design patterns are classified based on the level of details, Complexity, and applicability of the system to be designed.

Let me take some real-time analogy. Suppose if you want to build a Home, first we need to know the requirement of the House like 3 Bed Rooms, 1 Kitchen, 3 Bathrooms, 1 Garden, 1 Swimming pool, 3 Floor Storages, and Parking Area. An engineer will measure the site and provide a rough design of the layout. Once confirmed with the design, the engineer will prepare a blueprint of the layout that will have a standard measurement and details. In the future, if you are looking for some extension and alternation, the design should be more flexible to do the changes without hampering the base design like Load balancing, Strength of the pillars, and other major parts. With keeping this clause, the engineer will prepare a blueprint that will be easy to accommodate with upcoming alterations in the House.

There is 23 Gang of Four (GoF) patterns are categorized into three groups and all design patterns describe their own problem that occurs over and over again in our environment and then describes the solution to that problem.

Let’s understand each classification with real-world examples as below,

Creational Pattern

Let’s consider a car manufacture company. They manufacture lots of cars in a day with batch process. Here Car is a template/blueprint, which can be used by different car manufacturing vendors like Tata, Honda, Ford, Hyundai, Maruti Suzuki, and many more. They all build a new car with a Car template and forms a unique car with a Tata manufacture like Tata Nano, Tata Hexa, Tata Nexon, Tata Safari, and more. Similar to other Car bands. So, we can see the Car instance has been created with make use of the Car template and it shows a clear behavior as Creating/Building an object. Herewith this example illustration represents the Creational pattern.

Structural Pattern

Let’s consider the same Car manufacture company as the Tata brand. They have several cars like Tata Nano, Tata Hexa, Tata Nexon, Tata Safari, and more. Herewith all Cars have different variants, Tata Nexon has the following variants like XE, XE+, XS, XS+, XZ, XZ+, XZ(O), XZ(S), XZ(O)+, and Limited editions. All these variants have features customization like Power Window, Music system, Temperature sensor, Pressure sensor, Rear wiper, Alloy wheels, and more. So, we can see the Tata Nexon car has been formed multiple variants based on the customer’s choice. Herewith this example illustration represents the Structural pattern.

Behavioral Pattern

Let’s consider a Car manufacture company as a Tata brand. There are a few selective Car has remote key options, that allows to Lock and Un-lock the Car and some other features to control the Car. Here we see two different objects like Car and Remote key, both can communicate with each other with RF technology. Here we see that the two different independent objects can communicate with each other. So, we can see the Car and Remote key is communicating with an interface as RF technology. Herewith this example illustration represents the Behavioural Pattern.

It is not simple to remember all the design patterns, but we can remember few commonly used design patterns for any software application development.

I would encourage you to explore a few of the common design patterns below which will be illustrated with real-time analogy and sample code walkthrough. This will really helpful for your project development.

  • Creational Patterns
  • Structural Patterns
    • Adaptor Design Pattern- A pattern simplifies the incompatible interfaces and acts as a bridge between two incompatible interfaces to communicate independently
    • Decorator Design Pattern- A pattern allows extending the existing functionality without disturbing the existing one
    • Facade Design Pattern- A pattern simplifies the higher-level interface that makes the subsystem easier to use and hides the complexity of the system
  • Behavioral Patterns
    • Observer Design Pattern- A pattern is used when there is a one-to-many relationship between objects, if one object is modified, its dependent objects are to be notified automatically

I hope this article helps you to understand the need for Design patterns and implementation. Soon, we will add up few more design patterns to the list. Thanks for reading the article. Be safe and stay healthy.

Similar Articles