Implement Singleton Using or Not Using dynamic object.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steven Woody

    #1

    Implement Singleton Using or Not Using dynamic object.

    hi,

    my friend ask me the following question. even thogth i prefer the
    solution 3, but i owe an explanation.


    -------------------------------

    I create three versions for singletone pattern below, plz compare them
    with advantage and disadvantage:

    1.

    MyApp.hpp
    class MyApp
    {
    public:
    static MyApp& getInstance() { return appInstance; }

    private:
    MyApp() {;}

    private:
    static MyApp appInstance;
    };

    MyApp.cpp

    MyApp MyApp::appInsta nce;

    2.

    MyApp.hpp
    class MyApp
    {
    public:
    static MyApp* getInstance() { return &appInstance ; }

    private:
    MyApp() {;}

    private:
    static MyApp appInstance;
    };

    MyApp.cpp

    MyApp MyApp::appInsta nce;
    3.

    MyApp.hpp
    class MyApp
    {
    public:
    static MyApp* getInstance();

    private:
    MyApp() {;}

    private:
    static MyApp* appInstance;
    };

    MyApp.cpp

    MyApp MyApp::appInsta nce==NULL;

    MyApp::MyApp()
    {
    }

    MyApp* MyApp::getInsta nce()
    {
    if (appInstance == NULL)
    appInstance = new MyApp();

    return appInstance;
    }
    ---------------------------------------

  • Axter

    #2
    Re: Implement Singleton Using or Not Using dynamic object.

    Steven Woody wrote:[color=blue]
    > hi,
    >
    > my friend ask me the following question. even thogth i prefer the
    > solution 3, but i owe an explanation.
    >
    >
    > -------------------------------
    >
    > I create three versions for singletone pattern below, plz compare them
    > with advantage and disadvantage:
    >
    > 1.
    >
    > MyApp.hpp
    > class MyApp
    > {
    > public:
    > static MyApp& getInstance() { return appInstance; }
    >
    > private:
    > MyApp() {;}
    >
    > private:
    > static MyApp appInstance;
    > };
    >
    > MyApp.cpp
    >
    > MyApp MyApp::appInsta nce;
    >
    > 2.
    >
    > MyApp.hpp
    > class MyApp
    > {
    > public:
    > static MyApp* getInstance() { return &appInstance ; }
    >
    > private:
    > MyApp() {;}
    >
    > private:
    > static MyApp appInstance;
    > };
    >
    > MyApp.cpp
    >
    > MyApp MyApp::appInsta nce;
    > 3.
    >
    > MyApp.hpp
    > class MyApp
    > {
    > public:
    > static MyApp* getInstance();
    >
    > private:
    > MyApp() {;}
    >
    > private:
    > static MyApp* appInstance;
    > };
    >
    > MyApp.cpp
    >
    > MyApp MyApp::appInsta nce==NULL;
    >
    > MyApp::MyApp()
    > {
    > }
    >
    > MyApp* MyApp::getInsta nce()
    > {
    > if (appInstance == NULL)
    > appInstance = new MyApp();
    >
    > return appInstance;
    > }
    > ---------------------------------------[/color]

    If you're singleton needs to be access from multiple translation units
    before calling main(), then I recommend a reference return type of
    option 3.
    It's safer to return a reference then to return a pointer, because it's
    easier to accidently delete a return pointer.

    //MyApp.hpp
    class MyApp
    {
    public:
    static MyApp& getInstance();
    private:
    MyApp() {}
    static MyApp* appInstance;
    };

    //MyApp.cpp
    MyApp MyApp::appInsta nce==NULL;

    MyApp& MyApp::getInsta nce()
    {
    if (appInstance == NULL)
    appInstance = new MyApp();

    return *appInstance;
    }

    Comment

    • Jay Nabonne

      #3
      Re: Implement Singleton Using or Not Using dynamic object.

      On Wed, 28 Dec 2005 17:41:46 -0800, Steven Woody wrote:
      [color=blue]
      > hi,
      >
      > my friend ask me the following question. even thogth i prefer the
      > solution 3, but i owe an explanation.
      >
      >
      > -------------------------------
      >
      > I create three versions for singletone pattern below, plz compare them
      > with advantage and disadvantage:
      >[/color]
      <snip>

      4.

      MyApp.hpp

      class MyApp
      {
      public:
      static MyApp& getInstance()
      {
      static MyApp appInstance;
      return appInstance;
      }

      private:
      MyApp() {;}
      };

      Doesn't get constructed until you call it the first time (so you don't
      have to worry about initialization order), and it will be destructed
      automatically when the program exits.

      - Jay

      Comment

      Working...