Re: static or not?




David Vanderschel wrote:
> There are many situations when one has a Class for
> which it only makes sense that a single instance be
> created in a given session. It seems to me that, in
> this situation, it does not really make much
> difference whether the Class's variables are declared
> as static or not. There is still only going to be one
> instance of each anyway. I have determined by
> experiment that such a program compiles and works the
> same either way. (I have encountered a couple
> situations where the compiler was not happy until I
> made a certain variable static because I had
> referenced it from a static context. But such
> situations are rare for me.)
>
> My question: When you have the choice, which choice
> should you make? My guess is that static declarations
> may lead to more efficient code; but I have been
> unable to find any discussion of this issue. (Part of
> the problem here is that Sun's search on "static" hits
> just about everything. Its concept of relevance seems
> pretty weak.) If it makes no difference, then I see
> no point in cluttering the code with unnecessary
> "static" key words.
>
> I could use some guidance on this issue.
>
> Any pointers appreciated,
> David V.


Hi, David,

When I need a single instance, I use a singleton (with a non-private
constructor, admittedly), with a static getInstance() method and all
other methods and data non-static.

I have no idea whether static data/methods lead to more efficient code,
but I think if your application depends upon such efficiency gains then
perhaps a non-interpreted language is better suited to your needs.

The minor reason I use as many non-static data/methods as possible is
because it makes it slightly easier to subclass the class if I find
that I was wrong in my assumption of the class's singularness, and
that perhaps two specific variants of the class are more suitable. If
it were a significant effort to use a singleton over using all static
data/methods, then I wouldn't bother; but singletons are, I find, easy
to design and use.

The major reason I use a singleton applies only when the singleton is
declared as public, i.e., is accessible outside its package.
Dependencies on concrete classes outside their home packages are more
dangerous than dependencies on concrete classes within their home
packages, as packages should be as encapsulated at possible/convenient.
So I prefer to cite the first principle of OO ("Program to an interface
not an implementation") when dealing with public classes, and try to
access the services they offer via an interface (or an abstract class)
rather than by the concrete class itself. Thus, I can alter the
implementation of the concrete class without affecting its clients.

I generally do this by having the singleton register itself in a public
Registry class. Yes, this is a concrete class visible to all packages,
but I keep this class as simple as possible: it only stores and returns
interface, and particluarly has no dependencies on the methods
contained in those interfaces, so I can change the the entire set of
methods within any given interface without changing the Registry code.

If I use a singleton, then I can strip away an interface from this
singleton, and have it register that interface in the Registry.

If I use static methods, I cannot do this: static methods are not
allowed to hide the instance methods of the interface.

This is the major reason I use a singleton, instead of a class of
static methods.

..ed

www.EdmundKirwan.com - Home of The Fractal Class Composition.

.



Relevant Pages

  • Re: Singleton
    ... hide the singleton class wholesale in the implementation section of the ... The interface is of course easily replaced with an abstract class: ... property MyFirstProperty: Integer read GetMyFirstProperty write ... procedure MyFirstProcedure; virtual; abstract; ...
    (borland.public.delphi.non-technical)
  • Re: Windows Service in VB
    ... Your service needs to expose an interface via .Net remoting. ... and register the public class as a well-known singleton. ... You also need to create a WinForms app that can be run by logged-in users. ... if your client app just wants to call methods on the service's remoted ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Singleton & Self := in constructor
    ... > I want to enforce singleton behavior in my TObject derived ... > The basic idea is that clients can call the Constructor any ... But it is much simpler to just use an interface ...
    (borland.public.delphi.language.objectpascal)
  • Re: Singleton
    ... I don't usually go to a string factory or a list factory to ... If you have an abstract DOM interface, for example, and the implementation ... To me it's very clear to go to the DomFactory or singleton ... As mentioned in my posting you unit test a class separate from the ...
    (comp.object)
  • Re: Singleton & Interfaces
    ... Basically, since both singletons expose the same interface, you ... public static class A: IMyInterface ... If you were truly implementing the singleton pattern, ...
    (microsoft.public.dotnet.languages.csharp)