Singleton

Description

  • Provide single entry point to global variable

  • It is a global variable

  • Only more engineered

Drawbacks

  • Hidden dependencies

  • Instantiation should be automatic

  • … and this is where the problems arise

Exercises

And class Person?

Imagine a system with huge numbers of instances of

class Person
{
public:
    // ...
    void be_sick();   // <--- charges social insurance. which one?
    // ...
};
  • Do all Person instance share the same insurance institution? Which one?

  • How to test such a system? Provide a test case fixture to setup and teardown the singleton instance?