473,503 Members | 1,929 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Global objects


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
Jul 22 '05 #1
1 3022
"Oystein Haare" <oy*****@online.no> 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;


Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1535
by: Gianguz | last post by:
I'd like to discuss about the opportunity to have a global objects creator that introduces into a general framework (suited for multithreading) a controlled semantic to manage globals variables...
2
8800
by: Thomas Matthews | last post by:
Hi, I'm getting linking errors when I declare a variable in the global scope, but not inside a function. The declarations are the same (only the names have been changed...). class Book {...
8
8389
by: john townsley | last post by:
whats the best way to declare Global objects (holding say 10 member variables and functions) of an unknown size in c++, memory efficient as possible thanks
59
3874
by: seberino | last post by:
I've heard 2 people complain that word 'global' is confusing. Perhaps 'modulescope' or 'module' would be better? Am I the first peope to have thought of this and suggested it? Is this a...
10
6741
by: David P. Donahue | last post by:
When I wrote websites in VB .NET, I would often put functions in Global for all the pages to call. Now, in C#, doing so results in "references to non-static objects" and whatnot. I realize what...
15
2451
by: randyr | last post by:
I am developing an asp.net app based on a previous asp application. in the asp applications global.asa file I had several <object id="id" runat="server" scope="scope" class="comclass"> tags for...
8
4845
by: Vishwanathan Raman | last post by:
Hi I have a declared a static DataSet object SOBJ in Global.asax.I also have a localy defined DataSet LSOBJ in Global.asax which I am storing in Application State.Is there any technical...
23
2221
by: David Colliver | last post by:
Hi, using c#, 1.1 I know that we are not supposed to use global variables etc. in c# I am having a problem, but not sure how to resolve. I did have another post here, but may have over...
24
3465
by: onnodb | last post by:
Hi all, Currently, I'm working on an Access XP database app that uses two global objects in its code. These two objects are instances of "controller classes". For example, one of them manages...
112
5346
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
0
7192
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7315
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6974
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5559
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4991
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4665
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3158
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3147
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
369
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.