Re: Design Patterns
Stefan Ram wrote:
Eric Sosman writes:
A singleton class can be transformed into an uninstantiable
class having only static methods.
Static methods cannot be passed via reference or used to
Do you mean "via a reference to their owning instance"?
implement interfaces. Therefore, there is a need for
non-static methods sometimes. But this does not mean a
singleton, I am thinking of a POJO first and foremost
until someone shows me where a POJO does not suffices,
but a singleton is needed.
Most of the time, at least, it suffices to have only one instance of a class where
otherwise one might be tempted to implement a Singleton.
I have a strong bias toward using instance methods over static methods, in part because
they can implement an interface method. It does not suffice that the method merely
doesn't access instance state. It also needs to be "global" by design.
Instance methods can be easier to control in multi-threaded contexts. It's often good to
have a manager instance that controls the methods so that different units can use them
independently.
If you later decide that "singleton" was a mistake, instance methods don't need to be
refactored.
There's also a circular analysis where type state is maintained in mutable static variables, so one
creates static methods on the theory that they don't access instance state.
The risk of mistake with instance methods is lower than with static methods.
So if I have a compelling argument for making a method static, fine, but tie or a close second
goes to the instance implementation.
--
Lew