Thread: [Dev-C++] Design decision in C++
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Michael S. <mic...@nu...> - 2004-03-09 20:16:58
|
Hi, =20 I=B4ve thought about a class-design for one of my projects. I=B4ve several big codeblocks like "compiler" which are seperated into classes. However, of this classes there is only one instance, like TheCompiler. So I thought of implementing this class using static methods rather than = a regular class (I don=B4t need constructors/destructors). So, is it better do use a regular class and create one instance of it or = to use a class which has only static methods (also easier) Where are the caveats? =20 thanks for your help =20 Michael Stather |
|
From: P.D. v. Z. <pd...@xs...> - 2004-03-09 20:53:46
|
There's an excellent pattern for this, it's called "Singleton"... look = it up (try google for "singleton design pattern" for instance). =20 Hope this helped, =20 =20 Paul ________________________________ From: dev...@li... [mailto:dev...@li...] On Behalf Of Michael Stather Sent: Tuesday, March 09, 2004 9:00 PM To: dev...@li... Subject: [Dev-C++] Design decision in C++ Hi, =20 I=B4ve thought about a class-design for one of my projects. I=B4ve several big codeblocks like "compiler" which are seperated into classes. However, of this classes there is only one instance, like TheCompiler. So I thought of implementing this class using static methods rather than = a regular class (I don=B4t need constructors/destructors). So, is it better do use a regular class and create one instance of it or = to use a class which has only static methods (also easier) Where are the caveats? =20 thanks for your help =20 Michael Stather |
|
From: Per W. <pw...@ia...> - 2004-03-09 23:50:50
|
Oops, missed your mail ;) /Per W On Tue, 9 Mar 2004, P.D. van Zelst wrote: > There's an excellent pattern for this, it's called "Singleton"... look it= up > (try google for "singleton design pattern" for instance). > > Hope this helped, > > > Paul > > ________________________________ > > From: dev...@li... > [mailto:dev...@li...] On Behalf Of Michael > Stather > Sent: Tuesday, March 09, 2004 9:00 PM > To: dev...@li... > Subject: [Dev-C++] Design decision in C++ > > > Hi, > > I=B4ve thought about a class-design for one of my projects. > I=B4ve several big codeblocks like "compiler" which are seperated into > classes. > However, of this classes there is only one instance, like TheCompiler. > So I thought of implementing this class using static methods rather than = a > regular class (I don=B4t need constructors/destructors). > So, is it better do use a regular class and create one instance of it or = to > use a class which has only static methods (also easier) > Where are the caveats? > > thanks for your help > > Michael Stather > > > > > ---------------------------- |
|
From: Per W. <pw...@ia...> - 2004-03-09 23:50:23
|
I would instanciate a single object using the singleton design pattern.
Make the only constructor private, and add a public static method to
create and retrieve this single instance:
class MyClass {
private:
static My>Class *myself;
MyClass(); // Note constructor private!
public:
static MyClass* getInstance() {
if (!myself) myself =3D new MyClass;
return myself;
}
};
/Per W
On Tue, 9 Mar 2004, Michael Stather wrote:
> Hi,
>
> I=B4ve thought about a class-design for one of my projects.
> I=B4ve several big codeblocks like "compiler" which are seperated into
> classes.
> However, of this classes there is only one instance, like TheCompiler.
> So I thought of implementing this class using static methods rather than =
a
> regular class (I don=B4t need constructors/destructors).
> So, is it better do use a regular class and create one instance of it or =
to
> use a class which has only static methods (also easier)
> Where are the caveats?
>
> thanks for your help
>
> Michael Stather
>
|
|
From: Doug M. <dou...@al...> - 2004-03-10 03:29:42
|
This is thread is especially interesting to me, as I'm working on a project that uses Singleton for a number of its classes, and I'm not sure it is warranted. No one has addressed Michael's original question: what are the tradeoffs between using Singleton and just making the class (or all of its members) static? I'd like to know about others' experiences with Singleton, in case it's advisable to move from Singleton-based classes to static classes in my project. Doug |
|
From: Per W. <pw...@ia...> - 2004-03-10 06:09:33
|
The only real advantage of a static object is that you don't allocate it which might be an advantage in a small embedded environment - you might write the application without support for dynamic memory. If the class on the other hand has only static members, you don't have to send the "this" pointer to the member functions. A very minor improvement that is mostly eaten up for a number of compilers by loss of a couple of compiler optimizations specially added for C++ object accesses. If the class is using static methods it has to use static member variables too, which is a bit bothersome, and you don't get to use a constructor. Some notable advantages of Singleton objects are: 1) Constructor is run after main, so it might be easier to debug. 2) Programmer control when constructor is run, which is advantageous if multiple classes relates to each other, and you want to control order of construction. 3) If you later realizes that you need more than one object of a class, it tends to be easier to modify the program if the class was originally using the Singleton pattern. A solution with static methods requires heavy rewrites. /Per W On Tue, 9 Mar 2004, Doug MacDonald wrote: > This is thread is especially interesting to me, as I'm working on a > project that uses Singleton for a number of its classes, and I'm not > sure it is warranted. No one has addressed Michael's original question: > what are the tradeoffs between using Singleton and just making the class > (or all of its members) static? I'd like to know about others' > experiences with Singleton, in case it's advisable to move from > Singleton-based classes to static classes in my project. > > Doug > > > > |
|
From: <de...@ci...> - 2004-03-11 18:01:41
|
If you want to use any objects within your class that just uses static methods you end up having to instantiate them as singletons in a non-standard construction method that you then have to document. Far simpler to design a conventional class that you then use as a singleton. In article <404...@al...>, dou...@al... (Doug MacDonald) wrote: > This is thread is especially interesting to me, as I'm working on a > project that uses Singleton for a number of its classes, and I'm not > sure it is warranted. No one has addressed Michael's original > question: what are the tradeoffs between using Singleton and just > making the class (or all of its members) static? I'd like to know > about others' experiences with Singleton, in case it's advisable to > move from Singleton-based classes to static classes in my project. > > Doug |