Law of Demeter. What is it: Style Rule for building systems. Proposed by my research group: The Demeter Research Group in 1987, published in 1988. Covered.

Slides:



Advertisements
Similar presentations
Ch:8 Design Concepts S.W Design should have following quality attribute: Functionality Usability Reliability Performance Supportability (extensibility,
Advertisements

Observer Method 1. References Gamma Erich, Helm Richard, “Design Patterns: Elements of Reusable Object- Oriented Software” 2.
Reviews for Exam 1 Chapter 1-4 CSc 212 Data Structures, Sec FG CCNY, Fall 2010.
What is the Chain? It’s a behavioral design pattern. It deals with how objects make requests and how they are handled.
Reviews for Exam 1 Chapter 1-4 CS 211 Data Structures MHC, 2007.
March R. McFadyen1 Principle of Least Knowledge – page 265 Principle: talk only to your immediate friends Also called Law of Demeter (LoD)
Oct. 2, 2003CS WPI1 CS 509 Design of Software Systems Lecture #5 Thursday, Oct. 2, 2003.
Fall 2009ACS-3913 R. McFadyen1 Protected Variations Principle: How do you design so that variations in the future do not have an undesirable affect on.
PRESENTED BY SANGEETA MEHTA EECS810 UNIVERSITY OF KANSAS OCTOBER 2008 Design Patterns.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
Chapter 25 More Design Patterns.
CSc 335: Three More OO Design Principles
Starting Chapter 4 Starting. 1 Course Outline* Covered in first half until Dr. Li takes over. JAVA and OO: Review what is Object Oriented Programming.
CIS 321—IS Analysis & Design
©Ian Sommerville 2000 Software Engineering, 6th edition. Chapter 7 Slide 1 System models l Abstract descriptions of systems whose requirements are being.
Design Dan Fleck CS 421 George Mason University. What is the design phase? Analysis phase describes what the system should do Analysis has provided a.
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser Objectives Lecture 11 : Frameworks SWE 316: Software Design and Architecture  To understand.
1 OO Design Novosoft, 2001 by V. Mukhortov. 2 OO Design Goals  Flexibility Changes must be localized  Maintainability Modules requiring changes can.
Object-Oriented Modeling and Design
Chapter 1 Object-Oriented Analysis and Design. Disclaimer Slides come from a variety of sources: –Craig Larman-developed slides; author of this classic.
A GENERIC PROCESS FOR REQUIREMENTS ENGINEERING Chapter 2 1 These slides are prepared by Enas Naffar to be used in Software requirements course - Philadelphia.
SOEN 6011 Software Engineering Processes Section SS Fall 2007 Dr Greg Butler
1 4/97 Object-Oriented Design Workshop Week 6: Some Patterns for Implementing Associations, Part 1 David Van Camp Object Technology Consultant Software.
Technology in the classroom. UM weather Great way to begin a day or class –Links to 300 weather sites –Links to 700 web cams to view weather –Radar and.
Chapter 17. Initial Object Design Inputs: requirements meetings various Use Cases – 10% complete Key risks addressed with preliminary programming System.
AP/DJ AP: a generic technology DJ: a low-learning-curve implementation of AP.
A Validation of Object-Oriented Design Metrics As Quality Indicators Basili et al. IEEE TSE Vol. 22, No. 10, Oct. 96.
Chapter 1 Object-Oriented Analysis and Design. Disclaimer Slides come from a variety of sources: –Craig Larman-developed slides; author of this classic.
1 The Modular Structure of Complex Systems Presented by: SeyedMasoud Sadjadi and Wei Zhu David L. Parnas, Paul C. Clement, and David M. Weiss ICSE 1984.
SWE © Solomon Seifu ELABORATION. SWE © Solomon Seifu Lesson 12-5 Software Engineering Design Goals.
GRASP: Designing Objects with Responsibilities
Not only mark-up languages! There are other many other grammar formalisms and tools than XML. Some of them standardized (ASN). Even XML does not always.
1 Single Responsibility Principle Open Closed Principle Liskov Substitution Principle Law of Demeter CSC 335: Object-Oriented Programming and Design.
Object-oriented Design and Programming CS 2210: SW Development Methods Reading: Chapter 2 of MSD text – Section on UML: look at class diagrams but.
Adaptive Software Kevin Cella Graduate Seminar 02/04/2005.
OO Design Principles Copyright © Vyacheslav Mukhortov, Nikita Nyanchuk-Tatarskiy, Copyright © INTEKS LLC,
Translating Traversals to AspectJ. Outline Motivation Demeter Process for Traversals AspectJ Translation Process.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
M1G Introduction to Programming 2 3. Creating Classes: Room and Item.
Evaluating an Object-Oriented Design ©SoftMoore ConsultingSlide 1.
Chapter 5 Bend or Break. Life does not stand still We need to make every effort to write code that is as loose – as flexible – as possible Reversability:
Design. Practices Principles Patterns What are the characteristics of good design? What are good solutions to common design problems? How do we go about.
Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another using Arrays. How does one work with these.
Watching the movie the hard way…. Page 256 – Head First Design Patterns.
Object- oriented Design Principles
The Law of Demeter For Operating System Course. Motivation Several programs were written in bad style Are difficult to maintain There is a simple remedy.
Talk only to your friends that share the same concerns (Law of Demeter for Concerns) Midterm Review February 2004.
Basic Characteristics of Object-Oriented Systems
CSCE 240 – Intro to Software Engineering Lecture 3.
1 The Law of Demeter By Rick Mercer with help from Object-Oriented Design Heuristics, Arthur Riel Addison-Wesley, 1996, ISBN X and Applying.
Engineered for Tomorrow Unit:8-Idioms Engineered for Tomorrow sharmila V Dept of CSE.
AP/DJ AP: a generic technology
Mantas Radzevičius ifm-2/2
Jim Fawcett CSE776 – Design Patterns Summer 2005
Introduction to the Unified Modeling Language
TK2023 Object-Oriented Software Engineering
Introduction to Design Patterns
Chapter 1-4 CSc 212 Data Structures, Sec AB CCNY, Spring 2012
The Law of Demeter (LoD) and how it should be used
The Law of Demeter (LoD) and how it should be used
Lecture 6 Project review
ADAPTIVE PROGRAMMING Sezen ERDEM December 2005.
Introduction to the Unified Modeling Language
Introduction to the Unified Modeling Language
AP/DJ AP: a generic technology
Karl J. Lieberherr Northeastern University College of Computer Science
Lecture 21: Crosscutting Aspect-Oriented Programming Background
Objects First with Java A Practical Introduction using BlueJ
Objects First with Java A Practical Introduction using BlueJ
Law of Demeter (LoD) 7/23/2019 LoD
Presentation transcript:

Law of Demeter

What is it: Style Rule for building systems. Proposed by my research group: The Demeter Research Group in 1987, published in Covered in many major books on OO design and programming.

Law of Demeter Principle Each unit should only use a limited set of other units: only units “closely” related to the current unit. “Each unit should only talk to its friends.” “Don’t talk to strangers.” Main Motivation: Control information overload. We can only keep a limited set of items in short-term memory.

Law of Demeter FRIENDS

“closely related”

Application to OO Unit = method –closely related = methods of class of this/self and other argument classes methods of immediate part classes (classes that are return types of methods of class of this/self ) In the following we talk about this application of the Law of Demeter Principle to OO: example follows in a few slides.

Citibank Quote: Law of Demeter The Law of Demeter forms one of the cornerstones of the design approach of the Global Finance Application Architecture (quote from: Global Finance Application Architecture: Business Elements Analysis, Oct. 1991, Citibank confidential document) Widely used in big projects, for example, at JPL for the Mars exploration software.

Jet Propulsion Laboratory(JPL) Quote: Law of Demeter 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.

What others say about the Law of Demeter Two examples: –Booch –Rumbaugh

Booch and the Law of Demeter Context Chapter: Classes and Objects, Section: On Building Quality Classes and Objects, Subsection: Choosing Relationships

Booch and the Law of Demeter 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.

Rumbaugh and the Law of Demeter Context Chapter: Programming Style, Section: Extensibility

Rumbaugh and the Law of Demeter Quote: Avoid traversing multiple links or methods. A method should have limited knowledge of an object model. A method must be able to traverse links to obtain its neighbors and must be able to call operations on them, but it should not traverse a second link from the neighbor to a third class.

Law of Demeter (alternative formulation) A method should have limited knowledge of an object model. Leads to another Demeter favorite: Use grammars to define both class structure and an application-specific language. See the Structure-Shy Object Pattern.

Agreement that LoD Good Idea How to follow LoD: good solutions exist but not widely known. Two approaches to following LoD: –OO approach –Adaptive approaches Traversal support APPC DemeterJ

The Law of Demeter (cont.) Violation of the Law class A {public: void m(); P p(); B b; }; class B {public: C c; }; class C {public: void foo(); }; class P {public: Q q(); }; class Q {public: void bar(); }; void A::m() { this.b.c.foo(); this.p().q().bar();}

Violations: Dataflow Diagram A BC 1:b 2:c PQ 3:p() 4:q() foo() bar() m

OO Following of LoD A BC 1:b c PQ 3:p() q() foo() bar() m 2:foo2() 4:bar2() foo2 bar2

Adaptive Following of LoD void A::m() { (C) Traversal.long_get(this,”A->C”).foo(); (Q) Traversal.long_get(this,”A->Q”).bar();} void A::m() { this.b.c.foo(); this.p().q().bar();} // violation

Law of Demeter FRIENDS

What if your friends are far away? You pay them to travel to you or you send an agent to them to collect the information you need. –Approximate Directions: You give them or your agent directions about what kind of information to collect but you don’t care about accidental details of the travel. –Detailed Directions: You give them or your agent detailed travel directions.

Adaptive Following LoD FRIENDS S A b C X a:From S to A b:From S to B c:From S via X to C a c

Traversal strategies create friends Class Traversal is an intermediate class between classes that need to communicate Traversal.long_get(Object o, Strategy s)

Are not friends for accidental reasons Other classes exist for other reasons Ideal class graph: all are friends, even “far” away classes.