.NET Design Patterns: What they are and why we need them?

dpHi! In this article, I would like to talk about design patterns, what they are and why we need them. Do not want to show you whole list of patters because, I think, it does not make any sense from my point, I think better understating will be in case if I will show you a problem in the code and after, solution of resolving this problem by using pattern. I know, that until current time were wrote many articles about patterns and we can find hundreds of pages which describe them. But anyway, my aim in this article is to describe the concept of patterns in my vision, as simple as possible, what patterns are and why we need them, without using difficult explanation and I hope your understanding of this will be more clearly. And please, DO NOT WORRY, if you do not understand patterns, only you need is to trying to learn it, read articles in web, try to apply it on practice, and finally you will get full vision and any girl fall in love with you :)

So, let’s get started!

Patternit is just common approach for solving common issues or common problems, not more!

Patterns is just like a tool that will helps you to write very powerful and extensible systems and makes your life easy in this world :) This approach was created and tested many years ago by many developers around the world. So you do not need to reinvent the wheel, just use it!

Sometimes, you can even a little bit change this approach and you WILL NOT BREAKE signature of pattern, because, all patterns represent concepts but NOT formulas. This means that when you are trying to apply some of them, you can use it as you wish; it depends only on your requirements. I saw many times developers ask on the forums or in the blogs something like:

„if I will do like this, will it break pattern rules?”

The answer is: “Pattern is not a formula, it’s just a concept that you can use and change under your requirement at the current moment”.

For instance, pattern Strategy contains three main parts:

  • Strategy (interface)
  • ConcreteStrategy
  • Context

So, if you do not need Context in the application, just do not use it! To make your understanding a little bit clearly, I prepared one very simple comparison, let’s imagine that:

One line of programming code = one brick

One pattern = style of how you put these bricks


And let’s compare this in the real world example:

Development of Application Building a house

When you would like to develop an application, you have to write code in a proper style (way).


Only in this case, you can guarantee that your application will be stable, extensible, will not crash and easy understandable by any other developers.


However, this is also not a problem, if you wish to apply Strategy pattern (this pattern I will describe a little bit later) and due to requirements, you do not want to create Context class like it described in all books. Because Context class we need in case to store additional data for strategy and if you just do not need this, you do not have to create this one, that is it.


From the other hand, if you want to write code and do not think about next things:


1)    Can I use pattern here?

2)    Is this not a duplicate?

3)    My method does only one thing?

4)    Is my code supportable?

5)    Is my code extensible?

6)    It will be understandable for another programmer?

7)    Is my code b** sh**?




Your application will be difficult for developing, not extensible, SOLID principles will be broken and finally it will turn to a big madness which very difficult to support.

When you would like to build a house, you have to put bricks in a proper way.


Only in this case, you can guarantee that your house will be strong and will not break.



However, this is also not a problem, if you wish to build an anti-rocket wall and put bricks in two or three rows to make it more solid. Putting bricks in two rows is not a common rule, it is your desire, if you wish; you can put bricks in four rows, it is up to you.




From the other hand, if you want to put bricks randomly, house will be not so solid, with holes in the walls or even just collapse like a cards house.


I think that after comparison you understood my point. I would like to remind you once again: Pattern is not a formula, it is just a concept, which you can use to develop your applications. So, this is all that I wanted to say you about patterns and in the next article I will show you how to resolve one of the most famous problems in code by using Strategy pattern. Under famous problem, I mean „if-else code style”, it is more typical for beginner programmers.

Example of „if-else”:

        public static void Pay(PaymentTypeEnum paymentType, decimal amount)
            if (paymentType == PaymentTypeEnum.Cash)
                //Do Cash payment
                if (amount < 100)
                    //Do crazy logic
                else if (amount > 100)
                    //Do crazy stuff

            if (paymentType == PaymentTypeEnum.CreditCard)
                //Do CreditCard payment

            if (paymentType == PaymentTypeEnum.DirectDebit)
                //Do DirectDebit payment


So, In the next article we will discuss why this code is terrible and how we can replace this by using most famous Strategy pattern.

Thanks for reading!