Gabriel
Software Surgeon
Originally posted by Pat Farrell:
Many testing folks discourage use of the GoF singleton pattern. For the usual reasons (implicit coupling, hard to test, etc.).
The usual suggestion is to use a factory.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Originally posted by Gabriel Claramunt:
Singleton and Factory Method are different patterns to solve different problems.
Originally posted by Gabriel Claramunt:
The problem the pattern solves is:
"How do we ensure that a class has only one instance and that the instance is easily accessible? A global variable makes an object accessible, but it doesn't keep you from instantiating multiple objects."
Gabriel
Software Surgeon
Originally posted by Frank Carver:
The factory can be supplied using any form of "dependency injection" (such as passing in as a constructor parameter, or into a "setter"), or fetched from some sort of context supplied by similar methods.
Originally posted by Frank Carver:
Is there anything actually preventing you from just "passing in" either objects pre-fetched from a factory, a factory, or a context from which the factory may be fetched?
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Originally posted by Ilja Preuss:
In my experience, designing software mostly is a skill of balancing different forces. So certainly dependency injection can be overdone, although personally I have a much stronger dislike for global dependencies.
Originally posted by Ilja Preuss:
Also note that using dependency injection doesn't necessarily imply the use of a dependency injection framework. But it certainly means more passing around, and less pulling from known access points in the code. Again, to me that is mostly a good thing.
Originally posted by Frank Carver:
[qb]I've made the leap that having a global parameter hiding in a Context or XML is just syntactic sugar over having a Singleton in the Java code.
Possibly. I think we may be discussing different meanings of "context", though. In my mind, a "context" is just an object from which other stuff may be fetched. It might be a Map, or a domain object with specific "getters", or a factory of some sort, or whatever. The point is that it is a single object which can provide an environment and is simple to pass around during the execution of some scenario.
[/QB]
{QB]
In my experience, though, every Java application has some starting point, be it a "main" method, a servlet, or whatever. This starting point is by definition special, so I usually set up any initial values, factories etc. in this class. That way there is little or no object creation in classes and methods used in the processing of the application, and this in turn allows such classes and methods to be tested independently, with any test-specific setup that is required.
[/QB]
{QB]
That's an interesting viewpoint. I have seen this sort of problem in some applications where setter-based dependency injection has resulted in most classes in the system consisting mostly of methods to support the supply of such collaborators. This seems a common approach for teams which have bought into Spring, and proceeded to use it for everything.
[/QB]
{QB]
I guess it depends how bad you see the "disease".
[/QB]
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Originally posted by Scott Vachalek:
However, there is a workaround for testing. First, make the field package visible or add a package visible setter. Then, add a class to the package in the harness, i.e. the TEST SOURCE directory, with a public method to set the singleton. This gives tests a foothold for controlling the Singleton without having a public setter in production code.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Gabriel
Software Surgeon
Originally posted by Gabriel Claramunt:
I'm intrigued now: what should be the recommended solution if one needs to control the number of instances of an object?
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Gabriel
Software Surgeon
Originally posted by Gabriel Claramunt:
Worse, all your collaboration classes will depend on the "singleton" even without using it.
As most singletons are from cross-cutting concerns, maybe using AOP could be a solution to avoid using them...
Originally posted by Peer Reynders:
Dependency injection only makes sense if the dependent objects implement against interfaces rather than implementations.
Originally posted by Peer Reynders:
AOP sounds good on paper but the auto-magically appearing code at the point cuts can cause its own sets of problems.
Originally posted by Gabriel Claramunt:
I read Martin's proposal, and I don't see much improvement. Either is a public attribute of the main class and you have the same global access problems that you had with singleton
or you pass the "singleton" object all along the collaboration chain because one class at the end needs it. Worse, all your collaboration classes will depend on the "singleton" even without using it.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
or you pass the "singleton" object all along the collaboration chain because one class at the end needs it. Worse, all your collaboration classes will depend on the "singleton" even without using it.
Gabriel
Software Surgeon
a fool thinks himself to be wise, but a wise man knows himself to be a fool - shakespeare. foolish tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|