The Singleton Pattern: To be or not to be [used]?

Started by
79 comments, last by 21st Century Moose 11 years, 5 months ago
the streering wheel example shows that some people don't understand what a class is for, and what an object is for.


class SteeringWheel {
}

class Car {
SteeringWheel steeringWheel;
}


classes are not what defines how many exists of something. that's the objects job.

if a singleton would fit my needs, i would use it. but it NEVER ever did. it's just a) limiting and b) making code look ugly. and, most importantly, c), it NEVER makes sense.
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia

My Page davepermen.net | My Music on Bandcamp and on Soundcloud

Advertisement

the streering wheel example shows that some people don't understand what a class is for, and what an object is for.


class SteeringWheel {
}

class Car {
SteeringWheel steeringWheel;
}


classes are not what defines how many exists of something. that's the objects job.

if a singleton would fit my needs, i would use it. but it NEVER ever did. it's just a) limiting and b) making code look ugly. and, most importantly, c), it NEVER makes sense.


this.. I am amazed what length people can go to justify their laziness. Why limit yourself to 1? I have never found a single example of something that REALLY needs to be instantiated once or kittens die... never.

That steering wheel in the car example is, seriously, pathetic.

Stefano Casillo
TWITTER: [twitter]KunosStefano[/twitter]
AssettoCorsa - netKar PRO - Kunos Simulazioni

METADATA:

There are many times when a program needs to store and reference data that is a result of processing and that can be used again. How will you store metadata? I like to use singletons for structural/constant data/flags/metadata(temporary) instances, when the complexity of creating new instances and having it loading its data from something/somewhere doesn't worth as simply acessing it straight like cache of processors is used and Windows Registry (always in memory).

METADATA:

There are many times when a program needs to store and reference data that is a result of processing and that can be used again. How will you store metadata? I like to use singletons for structural/constant data/flags/metadata(temporary) instances, when the complexity of creating new instances and having it loading its data from something/somewhere doesn't worth as simply acessing it straight like cache of processors is used and Windows Registry (always in memory).


sorry it doesn't float at all.
It doesn't have to be a ONE! You create it ONCE, and pass it around or publish it to objects interested in it.
You are talking about trading memory for computation costs.. it doesn't have anything to do with SINGLENESS and globality of scope.

Stefano Casillo
TWITTER: [twitter]KunosStefano[/twitter]
AssettoCorsa - netKar PRO - Kunos Simulazioni


[quote name='Caburé' timestamp='1352472139' post='4999297']
METADATA:

There are many times when a program needs to store and reference data that is a result of processing and that can be used again. How will you store metadata? I like to use singletons for structural/constant data/flags/metadata(temporary) instances, when the complexity of creating new instances and having it loading its data from something/somewhere doesn't worth as simply acessing it straight like cache of processors is used and Windows Registry (always in memory).


sorry it doesn't float at all.
It doesn't have to be a ONE! You create it ONCE, and pass it around or publish it to objects interested in it.
You are talking about trading memory for computation costs.. it doesn't have anything to do with SINGLENESS and globality of scope.
[/quote]


Singleton is single instance. It does not necessarily mean global scope (you can create a singleton in the scope of a package, for example).
Singleton is static - that is why there is a static word in Java (and not, again, necessarily, a global pointer/variable).
If you could reraise the data of a singleton just by making a new instance without costs, what would be the reason to use static classes (singleton) - that is what I am explaining?


How would you store, for example, trigonometry tables? If you can't see that, every time you declare a public static final int, the class where you declared it becomes a singleton, conceptually: is accessible in global scope and has only one static instance - the class itself. In Java, you can even set/change static variables at runtime (but not final variables).

I just said how I like to use SingleTons : to store metadata. I didn't argue that it is the only way to do that. But.. ppl seem to be stuck in just one technique.


After all, I am posting in the wrong place, I forgot to tell that I was meaning Java as programming language.

How would you store, for example, trigonometry tables?


class TrigonometryTables
{
...
}

rolleyes.gif
It DOES NOT HAVE to be one. If your application only needs one, you application will instance it once and make it available to its subsystems.
In other words: why making assumptions on an application behavior if you dont have to?

Stefano Casillo
TWITTER: [twitter]KunosStefano[/twitter]
AssettoCorsa - netKar PRO - Kunos Simulazioni


[quote name='Caburé' timestamp='1352473348' post='4999305']
How would you store, for example, trigonometry tables?


class TrigonometryTables
{
...
}

rolleyes.gif
It DOES NOT HAVE to be one. If your application only needs one, you application will instance it once and make it available to its subsystems.
In other words: why making assumptions on an application behavior if you dont have to?
[/quote]

That was a rethorical question, I already knew how to create a static class in Java. You mean why don't copy/paste Sun's/Oracle's formal definitions ? Because I thought it is a forum to interchange experience (this is rethoric again), not conceptual aspects of formal oop inspired by how philosofically the real world binds into the structure of a program and how beautiful it may become, in the aspect of fitting in the common-use of objects, the reference to singleton classes within the code.

I just said how I like to use singletons: reutilization of already created metadata valid in global level,everytime, with no specific meaning (and metadata doesn't have a context specific to an object but is the result of interactions of them, having no fit within them without extra-redesign). If the topic is how pretty it fits into the aspects of OOP... the name of the topic should be changed to "why do we still need to use singletons when it is conceptually outside the formal view of the floating round world of objects, resembling old days of procedural programming?".

I just answered the topic. If it fits or not the formal definitions of a design pattern instead of what the term means by itself.. that is another issue.

Singleton is single instance. It does not necessarily mean global scope (you can create a singleton in the scope of a package, for example).

Actually global access is part of the Singleton definition. From the GoF: "Ensure a class has only one instance and provide a global point of access to it."
Global variable is already bounded in a scope.. do you mean that, when someone uses an API, all its singletons used internally must be visible for the programmer? I don't believe.

Ok. You refer a design pattern.


I was dumb and did not payed enough attention to it in the first post, in the beginning of the topic, sorry.
The fact that effort has to be made to bring up reasons (or excuses!) for using a singleton should be telling you something roundabout now.

There is no valid technical constraint enforcing "there can only be one" on anything (aside from Highlander ;) ) and plenty of valid reasons why having more than one is or can be useful. So why would you enforce such a restriction on your code? And at the expense of having to do extra work in order to do so? You're basically spending extra effort in order to impose an artificial restriction on yourself. Seems a wee bit nuts to me.

That's aside from the design considerations of using globals.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

This topic is closed to new replies.

Advertisement