You are here: Re: Singletons in PHP « PHP Programming Language « IT news, forums, messages
Re: Singletons in PHP

Posted by Willem Bogaerts on 12/03/07 08:22

>> The main problem is that they do not abide the Law of Demeter: they do
>> not come in through the interface of your objects.

> Willem - could you explain that last sentence, please?

The Law of Demeter is a set of rules to reduce tight coupling in
object-oriented programs.

Passing the necessary related objects through the interface (methods,
constructor) ensures that your object stays modular. This has a few
advantages: they can be used in more general applications and (even
better) they can be tested with unit tests.

Suppose you have an object that references the Singleton "Database". You
would have a hard time to write a unit test that provides a mock
database or a test database object, because the object does not recieve
a database; it will locate that itself. And there is the problem. The
object is _hardwired_ to the Database object.

Or suppose you want the same object, but now for a different database.
You would have the same problem.

By the way, because of the amount of legacy code that I encountered that
were completely hardwired all around, I wrote a pattern to migrate the
use of such a Singleton to something more testable: the Half-a-Singleton
pattern (see http://www.w-p.dds.nl/pathalfs.php?STYLE=4)

I often draw an analogy of objects to electronic components: if you see
a wire welded from your CD drive to the speaker, for instance, they have
effectively become one component. If it is plugged, they are still
separate, but connected. The interface of an object is in a lot of ways
like a plug in hardware.

Best regards,
--
Willem Bogaerts

Application smith
Kratz B.V.
http://www.kratz.nl/

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация