Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you know when to use design patterns? [closed]

Anyone can read the GoF book to learn what design patterns are and how to use them, but what is the process for figuring out when a design pattern solves a problem? Does the knowledge of the pattern drive the design, or is there a way to figure out how a pattern can be used to change a design?

In other words, are there patterns for Patterns?

like image 910
Robert S. Avatar asked Sep 17 '08 16:09

Robert S.


People also ask

How do you know when to use design patterns?

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).

When should design patterns not be used?

If a problem has two solutions, one that fits in ten lines of code, and another one with hundreds of lines of code along with a pattern, please consider not using the pattern. Their presence isn't a quality measurement.

Which design patterns use Open Closed Principle?

The Open Closed Principle: Software entities should be open to extension but closed to modification. The Liskov Substitution Principle: Derived classes must be substitutable for their base classes. The Interface Segregation Principle: Make fine-grained interfaces that are client specific.

Why should the open closed principle be used when designing a class?

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.


1 Answers

Design patterns are supposed to provide a structure in which problems can be solved. When solving a real problem, you have to consider many tiny variations of a solution to that problem to see whether any fits a design pattern. In particular, you will probably need to generalise your problem, or its solution, in order to make a design pattern fit.

The answer is, it's an art. Knowing the design patterns is certainly an important step. One way to get used to this sort of thing is to study applications of design patterns, not just the patterns. Seeing many different applications of one pattern can help you over time to get better at mapping a task onto a pattern.

like image 194
EfForEffort Avatar answered Sep 19 '22 09:09

EfForEffort