When to use which design pattern?

C#Design Patterns

C# Problem Overview


I like design patterns very much, but I find it difficult to see when I can apply one. I have read a lot of websites where design patterns are explained. I do understand the most of them, but I find it difficult to recognize a pattern in my own situations.

So, that is why I ask this question. Are there any guidelines / alarm bells when to use which design pattern.

For example, if you are doing a switch statement to determine which object you need to create, you probably want to use the factory design pattern. So the switch statement in this case is a 'alarm bell' to use the Factory pattern.

So, do you know more 'alarm bells' to determine a design pattern?

C# Solutions


Solution 1 - C#

Usually the process is the other way around. Do not go looking for situations where to use design patterns, look for code that can be optimized. When you have code that you think is not structured correctly. try to find a design pattern that will solve the problem.

Design patterns are meant to help you solve structural problems, do not go design your application just to be able to use design patterns.

Solution 2 - C#

Learn them and slowly you'll be able to reconize and figure out when to use them. Start with something simple as the singleton pattern :)

if you want to create one instance of an object and just ONE. You use the singleton pattern. Let's say you're making a program with an options object. You don't want several of those, that would be silly. Singleton makes sure that there will never be more than one. Singleton pattern is simple, used a lot, and really effective.

Solution 3 - C#

I completely agree with @Peter Rasmussen.

Design patterns provide general solution to commonly occurring design problem.

I would like you to follow below approach.

  1. Understand intent of each pattern
  2. Understand checklist or use case of each pattern
  3. Think of solution to your problem and check if your solution falls into checklist of particular pattern
  4. If not, simply ignore the design-patterns and write your own solution.

Useful links:

sourcemaking : Explains intent, structure and checklist beautifully in multiple languages including C++ and Java

wikipedia : Explains structure, UML diagram and working examples in multiple languages including C# and Java .

Check list and Rules of thumb in each sourcemakding design-pattern provides alram bell you are looking for.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMartijnView Question on Stackoverflow
Solution 1 - C#Joep KillaarsView Answer on Stackoverflow
Solution 2 - C#Peter RasmussenView Answer on Stackoverflow
Solution 3 - C#Ravindra babuView Answer on Stackoverflow