473,569 Members | 2,729 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 (AbstractFactor y *factory);
};
....

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

class ConcreteFactory {
public:
ConcreteFactory ()
{
Manager::instan ce()->registerFactor y(this);
}
};

// Cpp:

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

// implementation
Jul 22 '05 #1
1 3028
"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 (AbstractFactor y *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 (AbstractProduc t *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::Regist erFactory doregister(new ConcreteProduct 1);
....

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::templa te RegisterFactory <ConcreteProduc t1> doregister;


Jul 22 '05 #2

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

Similar topics

2
1547
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 (objects and scalar-types). In the following example Global is able to create objects of any kind with and index value attached to. So a Global<0,...
2
8810
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 { public: Book()
8
8391
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
3889
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 candidate for Python 3000 yet? Chris
10
6752
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 that means and all, but what I'm wondering is what's the best way around it? Say, for example, I want a function that takes a username and a...
15
2456
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 objects that the app used to speed up some global level data access and functionality. I have recoded the class libraries in .net and would like...
8
4853
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 differences in the way both the objects are handled by IIS. Are both objects stored in different memory spaces? I can access both the objects in my web...
23
2235
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 confused things, so I will start afresh. An example of what I want to do...
24
3477
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 the multiple open instances of a form (some sort of 'window manager'). These global objects are created in a module "General", by defining them...
112
5380
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 that may print some messages. foo(...) { if (!silent)
0
7694
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6278
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5504
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5217
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1208
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
936
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.