How to choose a design pattern
As we already saw, there is a huge number of design patterns. In many cases, they are suitable to be used in combinations as well. Unfortunately, there is no definite answer about how to choose the concept of designing our code. There are many factors that could affect the final decision, and you should ask yourselves the following questions:
- Is this piece of code going to be fairly static or will it change in the future?
- Do we have to dynamically decide what algorithms to use?
- Is our code going to be used by others?
- Do we have an agreed interface?
- What libraries are we planning to use, if any?
- Are there any special performance requirements or limitations?
This is by no means an exhaustive list of questions. There are a lot of amount of factors that could dictate our decision in how we build our systems. It is, however, really important to have a clear specification, and if something seems missing, it should always be checked first.
In the rest of the chapters, we will try to give specific recommendations about when a design pattern should and should not be used. They should help you ask the right questions and take the right decision before going on and writing code.