4

Consider I have a singleton class Foo. The question here is not about the way to implement the singleton idiom, then I don't explicit it. I have somthing like that:

class Foo
{
  private:
          Foo();
          ~Foo();
  public:
    void  doSomething();
    int   getSomething() const;
  public:
    static Foo&  getInstance();
};

Then, in my mind, the classical way to call it is the following:

Foo::getInstance().doSomething();
int i = Foo::getInstance().getSomething();

It's quite annoying to always have to write Foo::getInstance() to access the instance and execute the expected task. Then my question is the following: what about duplicate functions as static functions, keeping the singleton mechanic, but making access shorter ?

class Foo
{
  private:
          Foo();
          ~Foo();
  public:
    void  doSomething();
    int   getSomething() const;
  public:
    static Foo&  getInstance();
    static void  _doSomething() { Foo::getInstance().doSomething(); }
    static int   _getSomething() { return Foo::getInstance().getSomething(); }
};

And then, I can call it with this shorter version:

Foo::_doSomething();
int i = Foo::_getSomething();

Is it common ? Is there good reasons to not do that (and why) ? Is there other (better) ways to simplify calls to singleton classes ?

Note: I don't use C++11 for compatibility reason.

Caduchon
  • 4,574
  • 4
  • 26
  • 67
  • 2
    Have you considered something like `auto & instance = Foo::getInstance(); instance.doSomething(); instance.getSomething();` ? – François Andrieux Aug 24 '17 at 14:04
  • @FrançoisAndrieux : Yes, of course. It's valid if the singleton is called multiple times in the same scope. I think about calls in several scopes. But it's a good point. – Caduchon Aug 24 '17 at 14:08
  • @FrançoisAndrieux `auto` is a C++11 keyword what the OP said he can't use. – muXXmit2X Aug 24 '17 at 14:09
  • @muXXmit2X: just replace it by `Foo` is the same. – Caduchon Aug 24 '17 at 14:10
  • If you want to be even shorter you can use free functions instead of static members. Or [not use a singleton at all](https://stackoverflow.com/questions/1020312/are-singletons-really-that-bad?noredirect=1&lq=1) :-) – Bo Persson Aug 24 '17 at 14:17
  • 2
    @BoPersson: I also can develop in Fortran... ;-) – Caduchon Aug 24 '17 at 14:24
  • 1
    You can write `Single().doSomething()`. Otherwise I don't think it makes a big difference, singleton is a global object and any disguise is counter-productive. – Peter K Aug 24 '17 at 14:55

1 Answers1

1

There is nothing wrong with the other static methods that make it shorter to call the other methods of your singleton.

If another class is frequently calling the singleton's methods, then consider a private member reference to the singleton in the other class that will get set upon construction of that class (assuming your singleton has been constructed before the other object's construction).

class Bar{

private:
    Foo& fooRef;
    void doA();


public:
    Bar();
    ~Bar();
};

And then in the constructor:

Bar() :
    fooRef(Foo::getInstance())
{}

And now for ease of use in Bar::doA() you can call:

void Bar::doA(){
    fooRef.doSomething(); 
}

And I know you said your question was not about singleton implementation, but I thought I would throw this in and say don't forget to prevent copy construction and assignment for the singleton.

9Breaker
  • 724
  • 6
  • 16