Singleton is a very well known and used design pattern.
A singleton is a class that can be instantiated only once: this can be very useful when you want a single component to be in charge of all the interaction with a specific piece of hardware.
In boardcore, you can achieve this by extending the Singleton template which you can find in src/shared/
.
Interface
The Singleton interface is a template class which exposes a single method:
inline static T* getInstance()
What this method does is:
- If an instance of this class already exists, return that instance.
- If an instance of this class does not exist yet, create and return a new one.
Example
From the Singleton.h
comment:
/**
* WARNING: deriving from this class is not enough to make a singleton,
* you also have to declare the constructor of your class private!
*/
class Foo : public Singleton<Foo>
{
private:
Foo() {} // private constructor
};