[fpc-pascal] Implementing a true Singleton - Can we decrease the visibility of a method?
Dean Zobec
dean.zobec at gmail.com
Fri Dec 8 22:23:22 CET 2006
> >> Try this.
> >> http://bdn.borland.com/article/22576
>
> NewInstance/FreeInstance is what I would have recommended as well.
>
> > Maybe we should include an implementation in FPC by default ?
>
> How ? The article works as-is AFAIK.
>
> Btw, I think singletons are nonsense too. Why is a global variable evil,
> and a singleton class not ?
You are right, beware of singletons. Being a simple pattern to implement it's
often misused and sometimes creates more problems than it solves,
it's a global variable after all, and you should put an
extra effort to try to avoid it as you would avoid a global variable.
It was one of the things I've learnt through unit testing, using a
singleton breaks the independency between tests, because the singleton
carries state with it that lasts as long as the program runs.
In this case thinking about how to make tests independent of each
other and trying to
introduce a mock object for the singleton, you can get rid of it in a
lot of cases.
The best thing is usually to think about how we can substitute a
singleton in the client class with a parameter in the constructor of
the client class.
See this article for further explanation:
http://www-128.ibm.com/developerworks/library/co-single.html
Regards,
Dean
More information about the fpc-pascal
mailing list