1 / 9

The Law of Demeter

Learn the Law of Demeter, a style rule for designing object-oriented systems that promotes low coupling and encapsulation. Understand how to apply this principle to create loosely coupled classes and achieve better software design. Explore examples and practical guidance for implementing the Law of Demeter in your projects.

tlarson
Download Presentation

The Law of Demeter

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The Law of Demeter By Rick Mercer with help from Object-Oriented Design Heuristics, Arthur Riel Addison-Wesley, 1996, ISBN 0-201-6338-X and Applying UML and Patterns: An Introduction to Object-Oriented Analysis and Design and Iterative Development, Craig LarmanThird Edition, Pearson PTR, October 2004

  2. Law of Demeter • A style rule for designing object-oriented systems • "Only talk to your immediate friends" • Name of the Greek Goddess of Agriculture • In other words, grow software in small steps • Each module should have only limited knowledge about other modules • Those "closely" related to the current unit • Each module only talks to friends • “Don't talk to strangers”

  3. Law of Demeter • This is low coupling in software engineering made more explicit • Try to avoid knowing about the structure of indirect objects • Use the direct object that you need to know about and let that object talk to indirect objects

  4. Who are closely related friends? • From a method, messages can be sent to • this object • a parameter of the method • an instance variable of this object • an object created within the method

  5. FRIENDS

  6. An Example • Widely used in big projects, for example, at JPL for the Mars exploration software, the quote • The Law of Demeter … has taken a firm hold in many areas of JPL. Major systems which have used LoD extensively include … Mars Pathfinder Software (begun in 1993). We are going to use LoD as a foundational software engineering principle for the X2000 Europa orbiter mission.

  7. Grady Booch Quote • The basic effect of applying this Law is the creation of loosely coupled classes, whose implementation secrets are encapsulated. Such classes are fairly unencumbered, meaning that to understand the meaning of one class, you need not understand the details of many other classes.

  8. Abstract example • class A { • private B b = new B(); • publicvoid m() { • this.b.c.foo(); // High coupling, bad • } • } • class B { • C c; // Package protected • } • class C { • publicvoidfoo() { • } • }

  9. Possible Test Question • Which code represents the better design, a or b?__ • // a. • dog.body.tail.wag(); • // b. • dog.expressHappiness(); • The bad example couples dog to two indirect classes DogBody, and DogTail • The good design couples dog only to the direct class DogAnimal

More Related