2011年8月16日星期二

DevShed: PHP and the Law of Demeter


On DevShed today there's a new tutorial looking at how to use dependency injection to help prevent you from breaking the "Law of Demeter" in your application's structure.



When [responsibilities aren't well defined for classes], it's a clear symptom of a common issue known as the "Law of Demeter" breakage. In case the name doesn't ring any bells, the "Law of Demeter" (http://en.wikipedia.org/wiki/Law_of_Demeter) - or the Principle of Least Knowledge - is a paradigm that allows to create loosely-coupled classes, based on a simple concept: each class should be designed to work properly using only the dependencies that it really needs.


He talks about how violation of this law (whether you knew you were or not) can lead to some bad coupling practices. He includes a few classes under a SampleApp that handles the interface between a database and the domain model. The violation of the law comes in when the database and service layers are introduced - a fetch the code does to get an adapter from the service locator rather than directly from the database functionality as it should.



There's code for everything included in the post

没有评论:

发表评论