On Thu, Jul 23, 2009 at 11:22 PM, Ed
Lazor<[email protected]> wrote:
> I realize some people are saying that Singletons should never be used.
>  If that's the case, why are they used in Zend Framework?   For
> example, Zend_Auth, Zend_Registry, Zend_Loader_Autoloader, and
> Zend_Controller_Front.  Aren't these good examples of where Singletons
> are beneficial?

It's a trade off between two types of complexity; coupling on one hand
and abstraction on the other hand. You don't want coupling, because it
makes it hard to reason about your program, but you don't want too
much abstraction either. What is "too much" depends a whole lot on the
individual programmer, so it can be tricky to pick an appropriate
level.

There's a certain class of problems where a global symbol doesn't
hurt. This is in places where the language imposes global effects in
any case. For example, PHP doesn't allow redefining classes, so
therefore the autoloader is inherently global in any case. It's not
that a global doesn't add coupling here - it's just that the language
designers have already made the choice for you.

Everything else is a judgement call.

> How do you know when it's ok to use Singletons?  It seems like
> Singletons are beneficial, when used to share a single resource,
> because they have the potential for improving performance, and
> reducing application complexity.
> ...
> That way, instead of reinstantiating an instance of Users all over the
> application, you call getInstance and re-use the same object as often
> as you'd like - giving the 7X performance boost Michael was
> mentioning.

Now you're mixing apples and oranges again. Global symbols do not
improve performance. Shared resources improve performance. Globals are
just one way to share resources, but it's not the only one.

--
troels

Reply via email to