Robert said:
Is there any reason to make an instance of the class, when there wil be just
exactly one instance of that class? Assume please that I`m not talking about
the case of classes implementing Runnable interface. Just as theory. Why not
just to use static methods and variables of class instead of using one
instance of that class or singleton class.
There are a few cases where you might want to use an instance of a class
as opposed to static methods and fields. These include:
1. You want to take advantage of inheritance and/or polymorphism. Even
if there's only one instance, it may still implement interfaces declared
elsewhere and extend classes declared elsewhere. These superclasses and
superinterfaces could allow the class to be used in ways that it
couldn't with only static methods.
2. You want to be able to manipulate data in a way intrinsic to an
object. Again, having only one object of a specific class doesn't mean
that, for example, you don't want to add that object into a list
somewhere or otherwise treat it as an independent piece of data from the
perspective of another part of the application.
3. You anticipate future expansion making the object less unique. For a
large group of problem-domain objects, shifting from one unique instance
to managing multiple instances is actually a fairly common way for the
scope of the application to expand in future versions.
There are probably more reasons as well, but those are the reasons that
come to mind.
--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation