473,473 Members | 2,134 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Special type of singleton needed

I need to design a class that meets the following requirements:

-the class may not have more that one instance;
-the instance needs arguments for initialization;
-accessing the instance must not require any arguments;

Any ideas and comments are welcome.

Jul 23 '05 #1
11 1166

BigMan wrote:
I need to design a class that meets the following requirements:

-the class may not have more that one instance;
-the instance needs arguments for initialization;
-accessing the instance must not require any arguments;


How do you plan to provide the arguments? It's of course easy
to provide a static setArguments(...) method for your Singleton
class which you must call before calling the canonical
static Singleton& getInstance().

The private ctor/public static getInstance pattern guarantees
your first and third conditions, but there's no way to enforce
at compile time that setArguments is called. At best you can
throw from getInstance().

HTH,
Michiel Salters

Jul 23 '05 #2
BigMan wrote:
I need to design a class that meets the following requirements:

-the class may not have more that one instance;
-the instance needs arguments for initialization;
-accessing the instance must not require any arguments;

Any ideas and comments are welcome.


class myClass
{

private:
static myClass *_instance;
int pID;
protected:
public:
myClass(int processID) :pid(processID){
}
static myClass* Instance (int processID) {
try {
_instance = new myClass(processID)
return _instance;
}catch (...)
{
}
}

static myClass* Instance () {
return _instance;
}
};

int main()
{
myClass *OBJ = myClass::Instance(10003);
//
//
//
}

hope this is what you are looking for.

Jul 23 '05 #3
BigMan wrote:
I need to design a class that meets the following requirements:

-the class may not have more that one instance;
-the instance needs arguments for initialization;
-accessing the instance must not require any arguments;

Any ideas and comments are welcome.


class myClass
{

private:
static myClass *_instance;
int pID;
protected:
public:
myClass(int processID) :pid(processID){
}
static myClass* Instance (int processID) {
try {
_instance = new myClass(processID)
return _instance;
}catch (...)
{
}
}

static myClass* Instance () {
return _instance;
}
};

int main()
{
myClass *OBJ = myClass::Instance(10003);
//
//
//
}

hope this is what you are looking for.

Jul 23 '05 #4
BigMan wrote:
I need to design a class that meets the following requirements:

-the class may not have more that one instance;
-the instance needs arguments for initialization;
-accessing the instance must not require any arguments;

Any ideas and comments are welcome.

That's very easy, you will just use a static data member.


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #5
Could you please provide us with some code to demonstrate your idea?

Jul 23 '05 #6
BigMan wrote:
Could you please provide us with some code to demonstrate your idea?


class SomeClass
{
static SomeClass obj;

// The rest of your code

public:
SomeClass(int) { /* ... */}
};

SomeClass SomeClass::obj(4);
or
class SomeClass
{
static SomeClass *p;

// The rest of your code

public:
SomeClass(int) { /* ... */}
};

SomeClass *SomeClass::p= new SomeClass(4);

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #7
OK, I can make at least 2 instances of SomeClass. Here's how:

SomeClass Instance1( 1 );
SomeClass Instance2( 2 );

And hence this is not a singleton.

Jul 23 '05 #8
BigMan wrote:
OK, I can make at least 2 instances of SomeClass. Here's how:

SomeClass Instance1( 1 );
SomeClass Instance2( 2 );

And hence this is not a singleton.



Make the constructor private. I did not provide a complete solution but
only a way to start. Because it looks like a homework, that's why.
class SomeClass
{
static SomeClass *p;

private:
SomeClass(int x)
{
// Process x
}

// The rest of your code

};
SomeClass *SomeClass::p= new SomeClass(4);

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #9

BigMan wrote:
OK, I can make at least 2 instances of SomeClass. Here's how:

SomeClass Instance1( 1 );
SomeClass Instance2( 2 );

And hence this is not a singleton.


Please include quotes with your post to provide context for your
replies.

Brian

Jul 23 '05 #10
OK, this is all fine. There is, however, one minor peculiarity: I must
initialize the object in the main function.

Jul 23 '05 #11
BigMan wrote:
OK, this is all fine. There is, however, one minor peculiarity: I must
initialize the object in the main function.

class SomeClass
{
static SomeClass *p;

static bool activated;

int x;

SomeClass(int newValue):x(newValue) {}
public:

static void initialise(int newValue)
{
if(!activated)
{
p= new SomeClass(newValue);

activated=true;
}
}

// The rest of your code

};
SomeClass * SomeClass::p= 0;
bool SomeClass::activated= false;
int main()
{
SomeClass::initialise(4);
}


That's the last piece of code that I provide. If you do not try
yourself, how will you learn?


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #12

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

Similar topics

14
by: lawrence | last post by:
To call I would do something like: $headline = McSelectJustOneField::callDatastore("cbHeadline"); Is this the correct use of the static keyword, to implement a Singleton design?
26
by: Uwe Mayer | last post by:
Hi, I've been looking into ways of creating singleton objects. With Python2.3 I usually used a module-level variable and a factory function to implement singleton objects. With Python2.4 I...
13
by: William Stacey | last post by:
FYI. /// <summary> /// Author: William Stacey /// Fast and simple way to implement a singleton pattern without resorting /// to nested classes or other static vodo. Can also be easily converted...
4
by: Sgt. Sausage | last post by:
This may not be the best place to post this, but I'm coding in c# and, well this group is a "csharp" group! Background -- We encapsulate data access for an application through a home-grown...
2
by: Dave Rudolf | last post by:
Hey all, So I have a template class whose only template parameter is a type that the class is to manage. It's actually an implementation of the common Singleton design pattern. The class looks...
3
weaknessforcats
by: weaknessforcats | last post by:
Design Pattern: The Singleton Overview Use the Singleton Design Pattern when you want to have only one instance of a class. This single instance must have a single global point of access. That...
1
by: gzeng | last post by:
I am creating a singleton class in C++. Everything is fine except with the object pointers. I cannot instaniate a regular object. But I can define many pointers *aaaa, *bbbb, etc. to the class and...
2
by: Bob Johnson | last post by:
Just wondering the extent to which some of you are implementing classes as Singletons. I'm working on a brand new project and, early on, identified some obvious candidates. By "obvoius candidates"...
3
by: stevewilliams2004 | last post by:
I am attempting to create a singleton, and was wondering if someone could give me a sanity check on the design - does it accomplish my constraints, and/or am I over complicating things. My design...
0
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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
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
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...
0
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,...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.