词条 | Law of Demeter |
释义 |
The Law of Demeter (LoD) or principle of least knowledge is a design guideline for developing software, particularly object-oriented programs. In its general form, the LoD is a specific case of loose coupling. The guideline was proposed by Ian Holland at Northeastern University towards the end of 1987, and can be succinctly summarized in each of the following ways:[1]
The fundamental notion is that a given object should assume as little as possible about the structure or properties of anything else (including its subcomponents), in accordance with the principle of "information hiding". It may be viewed as a corollary to the principle of least privilege, which dictates that a module possess only the information and resources necessary for its legitimate purpose. It is so named for its origin in the Demeter Project, an adaptive programming and aspect-oriented programming effort. The project was named in honor of Demeter, “distribution-mother” and the Greek goddess of agriculture, to signify a bottom-up philosophy of programming which is also embodied in the law itself.{{Citation needed|reason=No source given for naming the Law of Demeter after the Demeter Project, or for naming the project after the Greek goddess|date=May 2018}} In object-oriented programmingWhen applied to object-oriented programs, the Law of Demeter can be more precisely called the “Law of Demeter for Functions/Methods” (LoD-F). In this case, an object Instead, More formally, the Law of Demeter for functions requires that a method
In particular, an object should avoid invoking methods of a member object returned by another method. For many modern object oriented languages that use a dot as field identifier, the law can be stated simply as "use only one dot". That is, the code AdvantagesThe advantage of following the Law of Demeter is that the resulting software tends to be more maintainable and adaptable. Since objects are less dependent on the internal structure of other objects, object containers can be changed without reworking their callers. Basili et al.[3] published experimental results in 1996 suggesting that a lower Response For a Class (RFC, the number of methods potentially invoked in response to calling a method of that class) can reduce the probability of software bugs. Following the Law of Demeter can result in a lower RFC. However, the results also suggest that an increase in Weighted Methods per Class[4] (WMC, the number of methods defined in each class) can increase the probability of software bugs. Following the Law of Demeter can also result in a higher WMC; see Disadvantages. A multilayered architecture can be considered to be a systematic mechanism for implementing the Law of Demeter in a software system. In a layered architecture, code within each layer can only make calls to code within the layer and code within the next layer down. "Layer skipping" would violate the layered architecture. DisadvantagesAlthough the LoD increases the adaptiveness of a software system, it may (but not necessarily will) result in having to write many wrapper methods to propagate calls to components; in some cases, this can add noticeable time and space overhead.[3][5][6] At the method level, the LoD leads to narrow interfaces, giving access to only as much information as it needs to do its job, as each method needs to know about a small set of methods of closely related objects.[7] On the other hand, at the class level, the LoD leads to wide (i.e. enlarged) interfaces, because the LoD requires introducing many auxiliary methods instead of digging directly into the object structures [5][6]. One solution to the problem of enlarged class interfaces is the aspect-oriented approach[8], where the behavior of the method is specified as an aspect at a high level of abstraction. This is done by having an adaptive method that encapsulates the behaviour of an operation into a place, with which the scattering problem is solved. It also abstracts over the class structure that results in avoiding the tangling problem. The wide interfaces are managed through a language that specifies implementations. Both the traversal strategy and the adaptive visitor use only a minimal set of classes that participate in the operation, and the information about the connections between these classes is abstracted out. Since the LoD exemplifies a specific type of coupling, and does not specify a method of addressing this type of coupling, it is more suited as a metric for code smell as opposed to a methodology for building loosely coupled systems. See also
References1. ^{{cite web | url = https://github.com/emerleite/demeter | title = README.markdown: Demeter | last = Macedo | first = Emerson | publisher = GitHub | accessdate = 2012-07-05}} 2. ^{{cite web | title = The Paperboy, The Wallet, and The Law Of Demeter | url = http://www.ccs.neu.edu/research/demeter/demeter-method/LawOfDemeter/paper-boy/demeter.pdf | last = Bock | first = David | page = 5 | publisher = College of Computer and Information Science, Northeastern University | accessdate = 2012-07-05}} 3. ^1 {{cite journal | first1=Victor | last1=Basili | first2=L. | last2=Briand | first3=W. L. | last3=Melo | date=October 1996 | title=A Validation of Object-Oriented Design Metrics as Quality Indicators | journal=IEEE Transactions on Software Engineering | volume=22 | issue=10 | pages=751–761 | quote=As expected, the larger the WMC, the larger the probability of fault detection. | doi=10.1109/32.544352}} 4. ^{{Cite web|url=https://maisqual.squoring.com/wiki/index.php/Weighted_Methods_per_Class|title=Weighted Methods per Class - Maisqual Wiki|website=maisqual.squoring.com|language=en|access-date=2018-09-20}} 5. ^1 {{cite web |url=http://www.bradapp.com/docs/demeter-intro.html |title=Introducing Demeter and its Laws | last=Appleton | first=Brad |accessdate=6 July 2013 |quote=A side-effect of this is that if you conform to LoD, while it may quite increase the maintainability and "adaptiveness" of your software system, you also end up having to write lots of little wrapper methods to propagate methods calls to its components (which can add noticeable time and space overhead).}} 6. ^1 {{cite web |url=http://pragprog.com/articles/tell-dont-ask |title=Tell, Don't Ask |publisher=The Pragmatic Programmers, LLC |accessdate=6 July 2013 |quote=The disadvantage, of course, is that you end up writing many small wrapper methods that do very little but delegate container traversal and such. The cost tradeoff is between that inefficiency and higher class coupling.}} 7. ^{{cite book | chapter-url = http://www.ccs.neu.edu/research/demeter/papers/law-of-demeter/oopsla88-law-of-demeter.pdf | title = Conference proceedings on Object-oriented programming systems, languages and applications (OOPSLA '88) | chapter = Object-Oriented Programming: An Objective Sense of Style | last1 = Lieberherr | first1 = K. | last2 = Holland | first2 = I. | last3 = Riel | first3 = A. | date = 1988 | publisher = ACM |pages=323–334 |doi=10.1145/62083.62113 |editor-first=Norman |editor-last=Meyrowitz | archiveurl = http://www.ccs.neu.edu/research/demeter/papers/law-of-demeter/oopsla88-law-of-demeter.pdf | archivedate = 1988-09-25 | quote = Easier software maintenance, less coupling between your methods, better information hiding, narrower interfaces, methods which are easier to reuse, and easier correct.ness proofs using structural induction. | accessdate = 2012-07-05| isbn = 978-0897912846 }} 8. ^{{cite journal | last1 = Lieberherr | first1 = Karl | last2 = Orleans | first2 = Doug | last3 = Ovlinger | first3 = Johan |date=October 2001 | title = Aspect-oriented programming with adaptive methods | pages = 39–40 | journal = Commun. ACM |volume=44 |issue=10 |doi=10.1145/383845.383855 | accessdate = 2012-07-05 | quote = An adaptive method encapsulates the behavior of an operation into one place, thus avoiding the scattering problem, but also abstracts over the class structure, thus avoiding the tangling problem as well. | url = http://www.inf.puc-rio.br/~inf2007/docs/artigos/%5B2%5D/Adaptive%20Programming%20CACM%202001.pdf| citeseerx = 10.1.1.192.6403 }} Further reading{{refbegin}}
External links
2 : Object-oriented programming|Programming principles |
随便看 |
|
开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。