Junior — Middle
Can you tell about design patterns you have used in your projects?
sobes.tech AI
Answer from AI
In my projects using C/C++, I have employed several common design patterns that help make the code more structured and maintainable:
- Singleton — for creating a single instance of a class, for example, for a configuration manager.
- Factory Method — for creating objects without specifying the exact class, which facilitates extension.
- Observer — for implementing a subscription and notification system, for example, in GUIs or event handling.
- RAII (Resource Acquisition Is Initialization) — a C++-specific pattern for resource management through objects, helping to prevent leaks.
Example of Singleton in C++:
class Logger {
public:
static Logger& getInstance() {
static Logger instance;
return instance;
}
void log(const std::string& message) {
// logging
}
private:
Logger() {}
Logger(const Logger&) = delete;
void operator=(const Logger&) = delete;
};
Using patterns helps to write more flexible and extendable code.