Design patterns are one of the most expressive tools for programmers. They allow us to solve design problems in an elegant and well-tested way. When you are struggling to provide the best possible design of your classes and their relationship, a well-known design pattern may come to the rescue.
The simplest example of a design pattern is a Singleton. It provides us with a way to declare and use only one instance of the class. For example, suppose that the e-commerce platform has only one Warehouse. To access the Warehouse class, the project may require that we include and use it in many source files. To keep things in sync, we should make the Warehouse a Singleton:
class Warehouse {
public:
static create_instance() {
if (instance_ == nullptr) {
instance_ = new Warehouse();
}
return instance_;
}
static remove_instance() {
delete instance_;
...