Design patterns Monostate Monostate-based hierarchies


Example

In contrasto to the Singleton, the Monostate is suitable to be inherited to extend its functionalities, as long as member methods are not static.
It follows a minimal example in C++:

struct Settings {
    virtual std::size_t width() const noexcept { return width_; }
    virtual std::size_t height() const noexcept { return height_; }

private:
    static std::size_t width_;
    static std::size_t height_;
};

std::size_t Settings::width_{0};
std::size_t Settings::height_{0};

struct EnlargedSettings: Settings {
    std::size_t width() const noexcept override { return Settings::height() + 1; }
    std::size_t height() const noexcept override { return Settings::width()  + 1; }
};