Using classes within other classes
Moderator: General Moderators
- allspiritseve
- DevNet Resident
- Posts: 1174
- Joined: Thu Mar 06, 2008 8:23 am
- Location: Ann Arbor, MI (USA)
Re: Using classes within other classes
Sorry, must have misunderstood you.
Re: Using classes within other classes
My response was aimed more at the singleton advocates 
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Using classes within other classes
Ok ... I continue to be bewildered. It is my understanding that Singletons are to provide one and only on instance of an object. It is my understanding that Registries are to provide a place to find common objects. And it is my understanding that Dependency Injection is where external code know how to instantiate and set objects that an object needs internally. Those seem to be three pretty different things.jshpro2 wrote:Look, i'm arguing singletons.. not registries.. granted 99% of people probably associate registries with singletons. The most common argument for singletons is "without a singleton, every object needs to have a reference to the commonly used objects".. simple un-true. With dependency injection the code expresses it's meaning, and will not execute without dependencies supplied. With a registry object you have to write code inside your class that make sure the dependencies are present in the registry. None of this has anything to do whether or not to use singletons though
(#10850)
- allspiritseve
- DevNet Resident
- Posts: 1174
- Joined: Thu Mar 06, 2008 8:23 am
- Location: Ann Arbor, MI (USA)
Re: Using classes within other classes
I can see the similarities. If we're speaking in terms of finding common objects, a Singleton would have global scope, and no impact on the interface; a registry would have limited global scope, depending on how many classes used it, with minimal impact on the interface; a DI container would have very limited scope, and would impact the interface the most, but abstracts object construction so you don't need to worry about fulfilling dependencies.arborint wrote:Ok ... I continue to be bewildered. It is my understanding that Singletons are to provide one and only on instance of an object. It is my understanding that Registries are to provide a place to find common objects. And it is my understanding that Dependency Injection is where external code know how to instantiate and set objects that an object needs internally. Those seem to be three pretty different things.
Re: Using classes within other classes
Maybe I can interject a different perspective here.
Perhaps there are two reasons for singletons:
1. Because the application demands that only once instance be created for logic purposes.
2. To save resources.
So that being said, with reason 1, it's an open and shut case for the most part. How you get an instance of the class is your preference.
With reason 2, there are other things to consider and things can get a bit tricky. This is something that only experienced developers are able to really do without ending up with bugs of some sort. I use reason 2 in my framework because things get incredibly fast. Again knowing when this can or can't be used boils down to experience or knowledge of the codebase.
I think I'm qualified to say this given that I can increase the speed of osCommerce by over 3000% with a little less than 5 hours of development time by making codebase changes.
Perhaps there are two reasons for singletons:
1. Because the application demands that only once instance be created for logic purposes.
2. To save resources.
So that being said, with reason 1, it's an open and shut case for the most part. How you get an instance of the class is your preference.
With reason 2, there are other things to consider and things can get a bit tricky. This is something that only experienced developers are able to really do without ending up with bugs of some sort. I use reason 2 in my framework because things get incredibly fast. Again knowing when this can or can't be used boils down to experience or knowledge of the codebase.
I think I'm qualified to say this given that I can increase the speed of osCommerce by over 3000% with a little less than 5 hours of development time by making codebase changes.
Re: Using classes within other classes
They are, that's what I was trying to convey.arborint wrote: Those seem to be three pretty different things.
Not buying it. Then the code is not re-usable.astions wrote:1. Because the application demands that only once instance be created for logic purposes.
Re: Using classes within other classes
I'm referring to the class, not the application. Sorry for the misunderstanding.jshpro2 wrote:Not buying it. Then the code is not re-usable.astions wrote:1. Because the application demands that only once instance be created for logic purposes.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Using classes within other classes
You know, most of the time you only want one instance and the way you achieve that is by simply creating only one instance. Singleton is for those time when you, as usual, only want one instance and you are actually having a problem of getting multiple instances. Saying that you only want one instance is not saying that you want a Singleton. Conversely there are situations where the problem occurs that Singleton solves.jshpro2 wrote:Not buying it. Then the code is not re-usable.astions wrote:1. Because the application demands that only once instance be created for logic purposes.
(#10850)
Re: Using classes within other classes
Indeed, OO programmers think of objects as their own "mini applications" that work together to solve big problems. Since you can create multiple objects that each have different state, the code is re-usable. Think of your application as a "singleton" your code and suddenly your code becomes not so re-usable.astions wrote:I'm referring to the class, not the application. Sorry for the misunderstanding.