Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Global objects

Reply
Thread Tools

Global objects

 
 
Oystein Haare
Guest
Posts: n/a
 
      05-03-2004

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


 
Reply With Quote
 
 
 
 
Siemel Naran
Guest
Posts: n/a
 
      05-03-2004
"Oystein Haare" <(E-Mail Removed)> 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;




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
class objects, method objects, function objects 7stud Python 11 03-20-2007 06:05 PM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 1 10-25-2006 06:50 PM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 0 10-25-2006 01:04 PM
Global variables on par with ASP's global.asa Wayne ASP .Net 2 11-11-2003 10:58 PM
Global Error handling in Applicatio_Error() of Global.asax VSK ASP .Net 1 07-29-2003 03:12 AM



Advertisments