Global objects

Discussion in 'C++' started by Oystein Haare, May 3, 2004.

  1. I'm thinking about sort of a factory-system where the factories get
    instanciated on program start (through a global object in the .cpp file),
    and registers themselves with a singelton.

    Is it a good idea to create global objects like that?
    Maybe it won't work at all?

    code:

    class AbstractFactory { ...};
    class AbstractProduct { ...};

    class Manager {
    public:
    static *Manager instance();
    registerFactory(AbstractFactory *factory);
    };
    ....

    // Header:
    class ConcreteProduct : public AbstractProduct { ... };

    class ConcreteFactory {
    public:
    ConcreteFactory()
    {
    Manager::instance()->registerFactory(this);
    }
    };

    // Cpp:

    ConcreteFactory factory; // instanciate a factory object
    // (is this a bad idea??)

    // implementation
    Oystein Haare, May 3, 2004
    #1
    1. Advertising

  2. Oystein Haare

    Siemel Naran Guest

    "Oystein Haare" <> wrote in message

    > I'm thinking about sort of a factory-system where the factories get
    > instanciated on program start (through a global object in the .cpp file),
    > and registers themselves with a singelton.
    >
    > Is it a good idea to create global objects like that?
    > Maybe it won't work at all?


    It is a good idea. There are minor variations, like storing pointers to a
    function which creates the objects. But the hardest part is to determine if
    the creation of the singleton manager must be thread safe, and to make it so
    if necessary (as when two objects try to register themselves in the
    manager).


    > class AbstractFactory { ...};
    > class AbstractProduct { ...};
    >
    > class Manager {
    > public:
    > static *Manager instance();
    > registerFactory(AbstractFactory *factory);
    > };


    Why not make the input argument to registerFactory an AbstractProduct (or
    smart pointer to one)? There seems to be no need for the AbstractFactory
    class.

    // Manager.h
    class Manager {
    public:
    class RegisterFactory {
    explicit RegisterFactory(AbstractProduct *factory);
    };
    };

    Then in each of the cpp files you will just say as follows, as const objects
    have internal linkage,

    // cpp file 1
    #include h file
    const Manager::RegisterFactory doregister(new ConcreteProduct1);
    ....

    Personally I like templates so the user doesn't have to enter constructor
    arguments

    // Manager.h
    class Manager {
    public:
    template <class Product>
    class RegisterFactory {
    RegisterFactory();
    };
    private:
    template <class Product> friend class RegisterFactory<Product>;
    void registerFactory(const AbstractProduct *);
    };

    // cpp file 1
    #include h file
    const Manager::template RegisterFactory<ConcreteProduct1> doregister;
    Siemel Naran, May 3, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VSK
    Replies:
    1
    Views:
    770
    Alvin Bruney
    Jul 29, 2003
  2. Wayne
    Replies:
    2
    Views:
    459
    Wayne
    Nov 11, 2003
  3. Daniel Bass
    Replies:
    2
    Views:
    6,864
    Daniel Bass
    Dec 31, 2003
  4. 2obvious

    no global constants in global.asax?

    2obvious, Sep 4, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    1,800
  5. 7stud
    Replies:
    11
    Views:
    675
    Dennis Lee Bieber
    Mar 20, 2007
Loading...

Share This Page