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?
For example, the Decorator pattern offers us to follow the Open Close principle. Furthermore, we may use the Factory Method, Strategy pattern and the Observer pattern to design an application with minimum changes in the existing code. That's all about 'SOLID Principles : The Open Closed Principle'.
If you know the design patterns, then when you are working through a design, and particular part of a system requires something that fits a design pattern you have, then use it. Don't try to fit a system round a design pattern, fit design patterns in to your system (where they fit).
It means that you should put new code in new classes/modules. Existing code should be modified only for bug fixing. New classes can reuse existing code via inheritance. Open/closed principle is intended to mitigate risk when introducing new functionality.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With