
After working for a few years, I often see some friends asking whether design patterns need to be learned. It seems that those who work in embedded systems have never encountered the need to use design patterns, so they have never systematically studied them. However, I know that design patterns are important, yet over time, I still haven’t learned them.
Here, I would like to share my views and thoughts to explore some questions.
What Are Design Patterns?
Design patterns represent the best practices accumulated by developers over time. They are optimal solutions to common problems faced during software development.
In other words, through continuous development, different problems or solutions have corresponding methods, and these methods have been summarized into a few dozen general patterns that we can apply when we encounter similar situations.
Of course, the major development principles and over twenty design patterns can be easily found in various resources, so I won’t elaborate on them here (I have only used a few myself); it’s best for everyone to study them independently.
Why Are Design Patterns Rarely Used in Development?
In regular project development, we rarely use design patterns. I feel this phenomenon is quite normal. It’s not that design patterns are not used at work; rather, in most cases, we don’t think that much about them. We focus more on making some requirement changes and overlook the essence.
The purpose of design patterns is to provide scalability and maintainability. However, most of the projects we develop are fixed and straightforward, with single logic. The modules we develop are not reused in other places or projects; the goal is clear: to address current business needs.
While design patterns are rarely used in everyday development, frameworks are different. Frameworks must adapt to different projects and possess high flexibility and scalability. They need to accommodate various environments, so design patterns are ubiquitous in framework design.
If you start at a large company or take over a nearly mature project, you will likely be responsible for developing small modules or specialized areas. In contrast, when working at a small company or starting a project from scratch, you might not have enough experience to design an architecture that aligns with the project’s long-term planning, leading to messy code and poor maintainability.
Therefore, in embedded development, once we have a certain foundation and project experience, we will realize how important a good architecture is, or we might think, ‘I need to learn design patterns well.’
Is It Necessary to Learn Design Patterns in Embedded Development?
It can be said that design patterns are born for scalability.
In regular project business logic code, most functions are fixed and designed for specific scenarios, meaning they won’t appear in other contexts. This kind of business development does not require design patterns.
However, if requirements change, we might directly modify the source code, which incurs a certain modification cost. Designing a highly extensible architecture for potentially unclear future changes also has its evident costs, so this is a trade-off.
Of course, from a long-term perspective, a good design is worthwhile. After all, it is better to avoid constantly modifying for new requirements, allowing developers to work efficiently from various aspects.
In embedded software development, when the software system reaches a certain level of complexity, design patterns become particularly important. Although embedded developers often work with 16-bit/32-bit/64-bit microcontrollers and may face some hardware limitations, microcontroller software can still follow the basic principles of software engineering for architecture.
From the perspective of code organization, such as componentization, layering, and decoupling, or from a design perspective, such as message queues and event-driven designs, there are logical reasons behind them.
Therefore, regarding this issue, it is best to learn design patterns in embedded development. I encourage everyone to read more about refactoring, design patterns, and object-oriented C.
How to Learn Design Patterns?
Having discussed what design patterns are, where they are used in embedded systems, and whether they need to be learned, you might wonder how to actually learn them!
I would like to discuss this based on different situations regarding how to learn design patterns, depending on your level, the problems you mainly solve, and the parts you are responsible for.
If your programming level or learning stage has not yet reached that point (still learning how to code), you will naturally be confused after reading this. At this time, it is better to focus on solidifying your foundational knowledge, as you won’t need design patterns yet.
If, during the project, you mostly solve some business logic, the applicable design patterns are few because what you need is a business model, which means referencing design pattern principles to design your own business model and modify requirements, which is unnecessary.
If you are responsible for or participate in application frameworks or related areas, it will be very beneficial. At this point, the choice of design patterns and how to design will test your skills.
Of course, regardless of the situation, as long as you have time, even if your current level is not sufficient, you can still learn design patterns to improve your skills, but it should be done step by step, progressing steadily.
This learning process is not about learning everything at once; that would be quite difficult to master and may only lead to superficial understanding. It is generally better to start learning from a specific pattern relevant to the problems you are dealing with or commonly encounter, learning one at a time and in depth.
Most people often mention singleton, factory, and strategy patterns. For example, a feature that requires support for both serial communication and TCP communication, while the caller should ideally not know the difference between them, is a typical scenario for the strategy pattern.
This is a relatively common situation that many projects may encounter, making it impossible not to learn. There are also many experience shares and tutorials available online, so it is unlikely that you won’t learn it.
Conclusion
At this point, do you think design patterns are useful?
Everyone’s situation is different, and the learning process may not be the same. The work content will definitely vary. The key point is that as long as you are in this field and writing code, you will generally need to use several patterns. Once you learn a few and use them regularly, over time, you will naturally understand.
Author: Li Xiaoyao, Source: Technology Makes Dreams Greater
Disclaimer: This article is reprinted with permission from the WeChat public account ‘Technology Makes Dreams Greater’. The reprint is for learning reference only and does not represent the views of this account. This account is not responsible for any infringement of its content, text, or images.
1. Another mistake! Over a thousand boards need to be reworked, it’s a painful story!
2. A Bluetooth chip with a circuit so simple it’s shocking, let’s take a look together
3. How I converted 51 code to STM32 driver code