Re: Singleton Pattern
- From: Michael Feathers <mfeathers@xxxxxxxxxxxxxxxx>
- Date: Thu, 25 Aug 2005 13:48:25 GMT
Jon Harrop wrote:
theinvisibleGhost wrote:
I've just started working on a very large project that uses the singleton pattern to create just about everything. Argghh! Why did someone ever allow this pattern to come into existence. It just seems an incredibly posh way of using global data!
The overuse of singletons doesn't really have anything to do with OO, it is a consequence of most people programming in OO languages than lack better alternatives (e.g. modules, nesting).
I think it does have to do with OO. It's really taking the easy way out. When you are tempted to use a global variable and you decide to do it, one thing that you are doing is shutting off some feedback that can help your design. When you skip globals and start to pass variables around, you alter your design so that you don't have to do that as often. What you end up with is more layering and separation of responsibility.
Databases are a typical example. If your database access is global and you use it every place (easy to do) you essentially have no layering. If you pass around references to it's access, it isn't too long before you discover that not everything needs the database and your design stratifies as a result.
Now, there's a big caveat here. Some people, if asked not to use globals, would pass variables through argument lists over and over again and not do anything to the design to make things easier. But, that's another problem entirely. For people who care about the structure of their code and its ease of modification, avoiding globals gives you more feedback as you develop and it helps you arrive at a better design.
Michael Feathers author, Working Effectively with Legacy Code (Prentice Hall 2005) www.objectmentor.com .
- Follow-Ups:
- Re: Singleton Pattern
- From: Jon Harrop
- Re: Singleton Pattern
- References:
- Singleton Pattern
- From: theinvisibleGhost
- Re: Singleton Pattern
- From: Jon Harrop
- Singleton Pattern
- Prev by Date: Re: Singleton Pattern
- Next by Date: Re: Singleton Pattern
- Previous by thread: Re: Singleton Pattern
- Next by thread: Re: Singleton Pattern
- Index(es):
Relevant Pages
|