Techious http://www.techious.com/forums/ |
|
Singleton Vs Single Object? http://www.techious.com/forums/viewtopic.php?f=16&t=7484 |
Page 1 of 1 |
Author: | Harry [ Wed Jun 27, 2012 3:43 am ] |
Post subject: | Singleton Vs Single Object? |
Since a lot of you on here actually program, I want your thoughts/opinions or cold hard facts telling me where I am wrong. This may not specifically apply to the C family, but I don't see why it shouldn't. So I recently learned what a Singleton class is. Lets say you have an object, that you only ever have one of. Be it a Player class for a game, or a JsonHandler for a program. So lets say that your ONLY ever going to use 1 of these objects that you make, should you indefinitely treat it as a singleton, rather than passing it through many functions and to child objects? I'm probably sprouting <HERP TO MY DERP> right now, but I just find that it's a lot nicer to use ClassName.getProperty, instead of having a static decleration in main and using, Main.myClassNameObject.getProperty. It's also faster than doing "new JsonManager.someFunction();" every time you want something. Especially, when it comes to multi-threaded programs. Since a certain variable could get locked up, while you're trying to access a member of it. I don't know, this could most probably be prevented by thinking about your code before doing it but it's not exactly difficult to turn a regular class into a singleton, and vice versa. Also, just figured out that you can use the forums BB codes or whatever they are called, in a poll. |
Author: | azcn2503 [ Wed Oct 31, 2012 5:01 pm ] |
Post subject: | Re: Singleton Vs Single Object? |
It's often better to just keep things simple. It's always worth asking yourself, when making a program (of any sort, be it web or compiled or whatever) - do I really need an advanced class based class.property/object.property style function, or should I just resort to doing something that works in the most simple way. If it doesn't need to be placed within a larger organisational object of some sort, and if it is not hurting to keep it standalone/global/whatever, you should just do that. Although I am sure there are many arguments for and against it. Preach practise all you want, etc. but at the end of the day, does it work? |
Page 1 of 1 | All times are UTC |
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group http://www.phpbb.com/ |