Thread overview | ||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
January 13, 2004 singleton classes | ||||
---|---|---|---|---|
| ||||
Often programmers make classes that are only intended to have one instance for the duration of the whole program. These are called singleton classes. D should have built in support for them so that a singleton object will have its own methods generated for it, without the use of a this pointer. This would save on a this parameter being pushed on stack to a singleton class method. Walter, what da ya say? |
January 13, 2004 Re: singleton classes | ||||
---|---|---|---|---|
| ||||
Posted in reply to imr1984 | singletons are one of the most stupid trend-programming-idioms. most people just use them because they don't know how to design else.. in D, i'd suggest you bether write a module. this _is_ what a singleton _should_be_. a.k.a. mySingleton.d -------------- module mySingleton; private int data; void set(int value) { data = value; } int get() { return data; } -------------- main.d -------------- import mySingleton; // private import? :D int main(char[][] args) { mySingleton.set(10); printf("%i" \n,mySingleton.get()); } -------------- like this In article <bu0da8$1qic$1@digitaldaemon.com>, imr1984 says... > >Often programmers make classes that are only intended to have one instance for the duration of the whole program. These are called singleton classes. D should have built in support for them so that a singleton object will have its own methods generated for it, without the use of a this pointer. This would save on a this parameter being pushed on stack to a singleton class method. > >Walter, what da ya say? > > |
January 13, 2004 Re: singleton classes | ||||
---|---|---|---|---|
| ||||
Posted in reply to davepermen | ar cheers for that. So D already has support for it then, you just use global private variables. cunning. In article <bu0ef1$1sds$1@digitaldaemon.com>, davepermen says... > >singletons are one of the most stupid trend-programming-idioms. > >most people just use them because they don't know how to design else.. > >in D, i'd suggest you bether write a module. this _is_ what a singleton _should_be_. > >a.k.a. > >mySingleton.d >-------------- >module mySingleton; > >private int data; > >void set(int value) { >data = value; >} > >int get() { >return data; >} >-------------- > >main.d >-------------- >import mySingleton; // private import? :D > >int main(char[][] args) { >mySingleton.set(10); >printf("%i" \n,mySingleton.get()); >} >-------------- > >like this > >In article <bu0da8$1qic$1@digitaldaemon.com>, imr1984 says... >> >>Often programmers make classes that are only intended to have one instance for the duration of the whole program. These are called singleton classes. D should have built in support for them so that a singleton object will have its own methods generated for it, without the use of a this pointer. This would save on a this parameter being pushed on stack to a singleton class method. >> >>Walter, what da ya say? >> >> > > |
January 13, 2004 Re: singleton classes | ||||
---|---|---|---|---|
| ||||
Posted in reply to imr1984 | as singletons are nothing more than that (globals), yes.. the real module support of D makes it possible to have them more easy than in c++ but always remember. singletons ARE just globals. if you don't need them, bether don't make them. In article <bu0eol$1su2$1@digitaldaemon.com>, imr1984 says... > >ar cheers for that. So D already has support for it then, you just use global private variables. cunning. > >In article <bu0ef1$1sds$1@digitaldaemon.com>, davepermen says... >> >>singletons are one of the most stupid trend-programming-idioms. >> >>most people just use them because they don't know how to design else.. >> >>in D, i'd suggest you bether write a module. this _is_ what a singleton _should_be_. >> >>a.k.a. >> >>mySingleton.d >>-------------- >>module mySingleton; >> >>private int data; >> >>void set(int value) { >>data = value; >>} >> >>int get() { >>return data; >>} >>-------------- >> >>main.d >>-------------- >>import mySingleton; // private import? :D >> >>int main(char[][] args) { >>mySingleton.set(10); >>printf("%i" \n,mySingleton.get()); >>} >>-------------- >> >>like this >> >>In article <bu0da8$1qic$1@digitaldaemon.com>, imr1984 says... >>> >>>Often programmers make classes that are only intended to have one instance for the duration of the whole program. These are called singleton classes. D should have built in support for them so that a singleton object will have its own methods generated for it, without the use of a this pointer. This would save on a this parameter being pushed on stack to a singleton class method. >>> >>>Walter, what da ya say? >>> >>> >> >> > > |
January 13, 2004 Re: singleton classes | ||||
---|---|---|---|---|
| ||||
Posted in reply to imr1984 | imr1984 wrote:
>Often programmers make classes that are only intended to have one instance for
>the duration of the whole program. These are called singleton classes. D should
>have built in support for them so that a singleton object will have its own
>methods generated for it, without the use of a this pointer. This would save on
>a this parameter being pushed on stack to a singleton class method.
>
>Walter, what da ya say?
>
>
>
This should probably be an optimisation issue.
|
January 13, 2004 Re: singleton classes | ||||
---|---|---|---|---|
| ||||
Posted in reply to davepermen | davepermen wrote:
>singletons are one of the most stupid trend-programming-idioms.
>
>most people just use them because they don't know how to design else..
>
>in D, i'd suggest you bether write a module. this _is_ what a singleton
>_should_be_.
>
>a.k.a.
>
>mySingleton.d
>--------------
>module mySingleton;
>
>private int data;
>
>void set(int value) {
>data = value;
>}
>
>int get() {
>return data;
>}
>--------------
>
>main.d
>--------------
>import mySingleton; // private import? :D
>
>int main(char[][] args) {
>mySingleton.set(10);
>printf("%i" \n,mySingleton.get());
>}
>--------------
>
>like this
>
>
But you can't extend that singleton.
|
January 13, 2004 Re: singleton classes | ||||
---|---|---|---|---|
| ||||
Posted in reply to imr1984 | "imr1984" <imr1984_member@pathlink.com> wrote in message news:bu0da8$1qic$1@digitaldaemon.com... > Often programmers make classes that are only intended to have one instance for > the duration of the whole program. These are called singleton classes. D should > have built in support for them so that a singleton object will have its own > methods generated for it, without the use of a this pointer. This would save on > a this parameter being pushed on stack to a singleton class method. > > Walter, what da ya say? Since the only hard thing about singletons - ensuring that the creation and destruction happens deterministically - is handled in D, by virtue of the module initialisation, where's the need? |
January 13, 2004 Re: singleton classes | ||||
---|---|---|---|---|
| ||||
Posted in reply to J Anderson | In article <bu0i1p$21m1$2@digitaldaemon.com>, J Anderson says... >But you can't extend that singleton. > hm? show me how to extend a normal one? anyways, i don't use singletons myself at all. they are 100% useless imho |
January 13, 2004 Re: singleton classes | ||||
---|---|---|---|---|
| ||||
Posted in reply to davepermen | davepermen wrote: >In article <bu0i1p$21m1$2@digitaldaemon.com>, J Anderson says... > > >>But you can't extend that singleton. >> >> >> >hm? show me how to extend a normal one? > > I don't know, parhaps by using static members from a class. But then you don't get virtual functions ;(. It'd be nice if you could extend namespaces (and templates), which included static virtual functions -> it might help solve the free operators problem. >anyways, i don't use singletons myself at all. they are 100% useless imho > > > But your right, most of the time I change singletons into classes as I realism their more useful that way. What's an extra memory access on the stack at that level anyway? the compiler probably wouldn't beat an eyebrow. It's the inner code you've gota worry about. |
January 13, 2004 Re: singleton classes | ||||
---|---|---|---|---|
| ||||
Posted in reply to davepermen | davepermen wrote:
> In article <bu0i1p$21m1$2@digitaldaemon.com>, J Anderson says...
>
>>But you can't extend that singleton.
>>
>
> hm? show me how to extend a normal one?
>
> anyways, i don't use singletons myself at all. they are 100% useless imho
interface FileSystem {
File openFile(char[] fileName);
char[][] getFileList();
}
class ZipFileSystem : FileSystem {
...
}
class FtpFileSystem : FileSystem {
...
}
class PhysicalFileSystem : FileSystem {
// This could reasonably be a singleton.
}
A singleton implemented as a module can't be extended, sure, but more importantly, they can't implement an interface.
-- andy
|
Copyright © 1999-2021 by the D Language Foundation