Karl J. Lieberherr Northeastern University College of Computer Science

Slides:



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

©Ian Sommerville 2000 Software Engineering, 6th edition. Chapter 12Slide 1 Software Design l Objectives To explain how a software design may be represented.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
CSc 335: Three More OO Design Principles
“Enhancing Reuse with Information Hiding” ITT Proceedings of the Workshop on Reusability in Programming, 1983 Reprinted in Software Reusability, Volume.
An Introduction to Software Architecture
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
Introduction To System Analysis and Design
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
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.
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.
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.
Drawbacks of object-oriented software development Lecture P12 T120B pavasario sem.
1 Single Responsibility Principle Open Closed Principle Liskov Substitution Principle Law of Demeter CSC 335: Object-Oriented Programming and Design.
Pattern Language for AP1 Pattern Language for Adaptive Programming (AP) Karl Lieberherr Northeastern University.
Demeter Aspects We study techniques for the emerging area of Aspect-Oriented Software Development and focus on the following areas:  Aspectual Collaborations.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
1 2 Z C1 C2 C3 C4 C5 Collab-1 Collab-2Collab-3 Collab-4 OOAD Implementation C1 C2 C3 C4 C5 No language constructs that capture collaborations.
AOP/cross-cutting What is an aspect?. An aspect is a modular unit that cross-cuts other modular units. What means cross-cutting? Apply AOP to AOP. Tease.
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.
Banaras Hindu University. A Course on Software Reuse by Design Patterns and Frameworks.
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.
AP/DJ AP: a generic technology
Design Patterns: MORE Examples
Chapter 1: Introduction to Systems Analysis and Design
Chapter 10 Design Patterns.
Chapter 5:Design Patterns
MPCS – Advanced java Programming
Introduction to Design Patterns
Chapter 11 Object-Oriented Design
Chapter 1 OBJECT-ORIENTED ANALYSIS AND DESIGN
CMPE 135: Object-Oriented Analysis and Design October 24 Class Meeting
Week 10: Object Modeling (1)Use Case Model
Structure and Interpretation of an Aspect Language for Datatype
Demeter Aspects Who We Are Aspectual Collaborations
TIM 58 Chapter 8: Class and Method Design
The Law of Demeter (LoD) and how it should be used
The Law of Demeter (LoD) and how it should be used
Adaptive Object-Oriented Software Development
Software Design and Development
ADAPTIVE PROGRAMMING Sezen ERDEM December 2005.
Adaptive Plug and Play Components for
Chapter 20 Object-Oriented Analysis and Design
Object oriented analysis and design
Building Modular Object-Oriented Systems with Reusable Collaborations
More Object-Oriented Programming
Midterm Review CSU 670 Spring 2004.
AP/DJ AP: a generic technology
Analysis models and design models
Adaptive and Aspect-Oriented Object-Oriented Software Development
Lecture 21: Crosscutting Aspect-Oriented Programming Background
An Introduction to Software Architecture
Adaptive Plug and Play Components for
Chapter 1: Introduction to Systems Analysis and Design
APPCs revisited 2/25/2019 APPCs revisited.
CMPE 135 Object-Oriented Analysis and Design March 21 Class Meeting
Software Development CSU 670 Karl Lieberherr
Nested Collaborations
Objects, components, and Frameworks with UML
Chapter 22 Object-Oriented Systems Analysis and Design and UML
College of Computer Science
Agenda Software development (SD) & Software development methodologies (SDM) Orthogonal views of the software OOSD Methodology Why an Object Orientation?
Aspect Oriented Software Design
Chapter 1: Introduction to Systems Analysis and Design
Law of Demeter (LoD) 7/23/2019 LoD
UML  UML stands for Unified Modeling Language. It is a standard which is mainly used for creating object- oriented, meaningful documentation models for.
From Use Cases to Implementation
Presentation transcript:

Smaller, More Evolvable Software: An Introduction to the Demeter Method Karl J. Lieberherr Northeastern University College of Computer Science lieber@ccs.neu.edu/www.ccs.neu.edu/home/lieber 1/11/2019 UBS/LoD

The Law of Demeter (LoD) and how it should be used in CUBE 1/11/2019 UBS/LoD Copyright, 1996 © Dale Carnegie & Associates, Inc.

Overview Introduction to Demeter Law of Demeter Principle How it was used at Citibank, JPL etc. Recommendation Benefits, Review of Benefits Close 1/11/2019 UBS/LoD

Thanks to Industrial Collaborators/Sponsors IBM: Theory of contracts, Adaptive Programming Citibank, SAIC: Adaptive Programming Mettler Toledo: OO Evolution Xerox PARC: Aspect-Oriented Programming (Gregor Kiczales et al.) supported by DARPA (EDCS) and NSF 1/11/2019 UBS/LoD

What is Demeter A high-level interface to object-oriented programming and specification systems Demeter System/OPL = Demeter Method + Demeter Tools/OPL So far: OPL = {Java, C++, Perl, Borland Pascal, Flavors} Demeter Tools/OPL = Demeter/OPL 1/11/2019 UBS/LoD

History: Many good things come from Zurich Hades (HArdware DEScription language by Niklaus Wirth at ETH Zurich) Zeus (a brother of Hades, a silicon compilation language developed at Princeton University/MIT, implemented at GTE Labs; predecessor of VHDL) Demeter (a sister of Zeus, used to implement Zeus, started at GTE Labs) 1982 82-85 1985- 1/11/2019 UBS/LoD

History Law of Demeter First traversal specifications 1987 Law of Demeter First traversal specifications Synchronization as an aspect Fast and general compilation algorithm APPCs (Adaptive Plug-and-Play Components) 1990 1993 1996/97 1998 1/11/2019 UBS/LoD

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 in many major books on OO design and programming. 1/11/2019 UBS/LoD

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. 1/11/2019 UBS/LoD

Law of Demeter FRIENDS 1/11/2019 UBS/LoD

“closely related” 1/11/2019 UBS/LoD

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. 1/11/2019 UBS/LoD

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. 1/11/2019 UBS/LoD

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. 1/11/2019 UBS/LoD

What others say about the Law of Demeter Two examples: Booch Rumbaugh 1/11/2019 UBS/LoD

Booch and the Law of Demeter Context Chapter: Classes and Objects, Section: On Building Quality Classes and Objects, Subsection: Choosing Relationships 1/11/2019 UBS/LoD

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. 1/11/2019 UBS/LoD

Rumbaugh and the Law of Demeter Context Chapter: Programming Style, Section: Extensibility 1/11/2019 UBS/LoD

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. 1/11/2019 UBS/LoD

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. 1/11/2019 UBS/LoD

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 Demeter/Java 1/11/2019 UBS/LoD

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();} 1/11/2019 UBS/LoD

Violations: Dataflow Diagram 2:c foo() 1:b B C A 4:q() bar() 3:p() P Q 1/11/2019 UBS/LoD

OO Following of LoD m foo2 c foo() 1:b B C A 2:foo2() 4:bar2() bar2 3:p() q() P Q 1/11/2019 UBS/LoD

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

Law of Demeter FRIENDS 1/11/2019 UBS/LoD

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. 1/11/2019 UBS/LoD

Adaptive Following LoD C A FRIENDS a S X c b a:From S to A b:From S to B c:From S via X to C 1/11/2019 UBS/LoD

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

Are not friends for accidental reasons Other classes exist for other reasons Ideal class graph: all are friends, even “far” away classes. 1/11/2019 UBS/LoD

Adaptive Following LoD: Key idea Introduce an ideal class graph Write current behavior in terms of ideal class graph Map ideal class graph flexibly into concrete class graph using traversal strategies 1/11/2019 UBS/LoD

Adaptive Following of LoD: Key Idea Collaborations are not explicit in the software. This is what APPCs will support. So why? 1/11/2019 UBS/LoD

Motivation OOAD Implementation --> Tangling Collab-1 C1 C4 C2 C3 C5 Z C1 C4 C2 C3 C5 Collab-4 Collab-2 Collab-3 C1 C2 C3 C4 C5 Object-oriented languages do not provide adequate constructs to capture collaborations between several classes. Has been recognized in different forms in the object-oriented community: OO accommodates the addition of new variants of data types better than procedural programming but, the opposite is true when new operations on existing data types are needed visitor pattern: the matter of concern -- definition of new operations on an existing object structure (aggregation is involved besides inheritance) several works complain the lack of constructs for expressing collaboration-based designs Implementation Collaboration -- a distinct (relatively independent aspect of an application that involves several participants, or roles roles played by application classes each class may play different roles in different collaborations each role embodies a separate aspect of the overall class behavior --> Tangling 1/11/2019 UBS/LoD Collaboration-based design s Require to view oo applications in two different ways: (a) in terms of participants or types involved (b) in terms of the tasks or concerns of the design

What is the Problem? “OO technology has not met its expectations when applied to real business applications partly due to the fact that there is no place where to put higher-level operations which affect several objects. … if built into the classes involved, it is impossible to get an overview of the control flow. It is like reading a road map through a soda straw'’ [Lauesen, IEEE Software, April ‘98] 1/11/2019 UBS/LoD

SE Group at UBI Lab Important work done there: Framework integration Role modeling 1/11/2019 UBS/LoD

Design Goal: Adaptiveness one generic collaboration P3 P1 reused APPL 1 C1 C1 APPL C2 C4 n C2 Requirements on the design: Generic specification of the collaboration with respect to the class structure it will be applied to: \ (a) the same component to be used with several different concrete applications, and (b) one collaborative component to be mapped in different ways, i.e., with different class-to-participant mappings, into the same class structure. Loose coupling of behavior to structure C3 C3 C4 C5 with a family of concrete applications 1/11/2019 UBS/LoD

Design Goal: Adaptiveness one generic collaboration P3 P1 reused with different mappings of participants to classes APPL 1 C1 APPL C1 1 C4 C4 C5 C5 C2 C3 C2 C3 OK, we want a construct (1) orthogonal to the standard object-oriented models - not substitute, rather complement classes (2) support a decomposition granularity that lies between classes and package modules a la Oberon But, what do we mean by adaptive? of the same application 1/11/2019 UBS/LoD

Recommendation I recommend that the CUBE project use the Law of Demeter Principle in OO designs and programs and that the Law of Demeter be followed in an adaptive, collaboration-based style (Demeter style). Justfication: Works well for Citibank, JPL, Hewlett-Packard and many other companies. 1/11/2019 UBS/LoD

Benefits of following Law of Demeter in Demeter Style robustness to changes shorter programs design matches program more understandable code partially automated evolution keep all benefits of OO technology improved productivity ideas can be used without tools Applicable to design and documentation of your current systems with immediate benefits. 1/11/2019 UBS/LoD

Benefits Review Robustness to changes: Elimination of violations of LoD in OO style leads to many tiny methods. Statistics from (Wilde, Matthews, Huitt, 1993): More than half of the member functions and methods have fewer than two C++ statements or four Smalltalk lines of code. Those tiny methods will be generated. 1/11/2019 UBS/LoD

Benefits Review Shorter programs : Tiny methods observed by Wilde et al. don’t have to be written and maintained. Programs get shorter. 1/11/2019 UBS/LoD

UML Class Diagram busStops BusRoute BusStopList buses 0..* BusStop BusList Example for developing strategies and writing adaptive programs waiting 0..* passengers Bus PersonList Person 0..* 1/11/2019 UBS/LoD

find all persons waiting at any bus stop on a bus route Traversal Strategy from BusRoute through BusStop to Person busStops BusRoute BusStopList buses 0..* BusStop BusList waiting 0..* passengers Bus PersonList Person 0..* 1/11/2019 UBS/LoD

Robustness of Strategy find all persons waiting at any bus stop on a bus route Robustness of Strategy from BusRoute through BusStop to Person villages BusRoute BusStopList buses VillageList busStops 0..* 0..* BusStop BusList Village waiting 0..* passengers Bus PersonList Person 0..* 1/11/2019 UBS/LoD

Benefits Review Design matches program: Because each behavior is written for an ideal class graph behavior is well encapsulated and easier to understand because of noise elimination. 1/11/2019 UBS/LoD

Benefits Review Partially automated evolution: tiny traversal methods are automatically regenerated when structure changes. Programmer needs to test them. 1/11/2019 UBS/LoD

Benefits Review Keep all benefits of OO technology: all code is written in plain Java with a traversal package added. You can use all good ideas of OO technology. 1/11/2019 UBS/LoD

Benefits Review Improved productivity: Several testimonials. Example: Hewlett-Packard Printer Installation Software is written in Demeter Style using C++. Televised Demeter class with student in Vancouver, Canada, lead to prototype. Prototype was so successful that entire package was re-implemented. 1/11/2019 UBS/LoD

Dangers if Law of Demeter Principle not followed adaptively CUBE might get as hard to maintain as ABACUS because structural information will be duplicated many times. CUBE code will be tangled: hard to understand and to maintain. 1/11/2019 UBS/LoD

During implementation separate issues are mixed together What is the problem? Tangling! During implementation separate issues are mixed together in den vorherigen Kapiteln wurde erklärt: 1. in der Informatik darum, Rechnermodelle zu bauen, die Prozessen in der reellen Welt simulieren. 2. Es gibt formale Mitteln, die es erlauben, die Modelle in eine für den Rechner verständlich en Sprache zu schreiben.t. In dieser Vorlesung erden wir das sogenannte objekt-orientierte Programmiermodel als das Mittel für die Beschreibung der Modelle benutzen. Mit anderen Worten, werden wir objekt-orientierte Rechnermodelle der reellen Welt bauen. Wir werden zuerst, die Konstrukte des objekt-orientierten Programmiermodels kennenlernen, d.h. die Bestandteile unsere Toolboxes. Als zweites werden wir kurz During maintenance individual issues need to be factored out of the tangled code 1/11/2019 UBS/LoD

Cross-cutting of components and aspects better program ordinary program structure-shy functionality Components structure Aspect 1 avoid tangled programs AOP synchronization Aspect 2 1/11/2019 UBS/LoD

Close I recommend that the Law of Demeter Principle be used in a Demeter Style in the CUBE project. Benefits: More flexible and simpler programs that can more easily be adapted to new business needs. 1/11/2019 UBS/LoD

Questions ?!

More details Traversal support Strategy graphs APPCs 1/11/2019 UBS/LoD

Traversal Support for Java: class Traversal static Object long_get(Object o, Strategy s); static Iteration long_collect(Object o, Strategy s); static Object traverse(Object o, Strategy s, Visitor v[]); 1/11/2019 UBS/LoD

Traversal Support for Java static X long_get(Object o, Strategy s); starting at object o, traverse down following s and return target object of s s must have a single target and s must specify unique path 1/11/2019 UBS/LoD

Traversal Support for Java static Iteration long_collect(Object o, Strategy s); starting at object o traverse following s and return the collection of target objects of s. 1/11/2019 UBS/LoD

Traversal Support for Java static Object traverse(Object o, Strategy s, Visitor v[]); starting at object o traverse down following s and execute the visitors in v. Return the object returned by first visitor. 1/11/2019 UBS/LoD

Traversal Support: visitors class SampleVisitor extends Visitor{ // local variables public SampleVisitor(); // initialize public void before(Visited host); public void after (Visited host); public void before_z(X source, Y dest); public void after_z (X source, Y dest); Object get_return_val(); public void start(); public void finish(); } 1/11/2019 UBS/LoD

Traversal Support: visitors abstract class Visitor { // GENERATED CODE EXAMPLE public Visitor() {super();} public void start() { } public void finish() { } public void before(Visited host); public void after (Visited host); public void before_p(Q source, H dest); public void after_p (Q source, H dest); … } 1/11/2019 UBS/LoD

Refinement definition A graph G is a refinement-instance of a graph S, if S is a connected subgraph of the pure transitive closure of G with respect to the node set of S. 1/11/2019 UBS/LoD

Pure transitive closure The pure transitive closure of G=(V,E) with respect to a subset W of V is the graph G*=(V,E*), where E*={(i,j): there is a W-pure path from vertex i to vertex j in G}. A W-pure path from i to j is a path where i and j are in W and none of the inner points of the path are in W. 1/11/2019 UBS/LoD

G1 refinement G2 F F D D E E B B C C G2 G1 A A refinement: connectivity of G2 is in pure form in G1 Allows extra connectivity. G1 A A

The APPC Pattern Benefiting from advances in component software technology using standard languages 1/11/2019 UBS/LoD

Design patterns and programming language features They are conceptually equivalent. Useful to explain language features using design pattern terminology. Danger of design patterns: workarounds needed to implement them can be tedious. Patterns are only used if benefit outweighs the cost. 1/11/2019 UBS/LoD

Motivation New component software ideas have a big acceptance threshold if they require non-standard language extensions. Use many of the good ideas with standard languages and suitable libraries/frameworks without language extension. Describe ideas using pattern terminology for easy reuse by designers/programmers. 1/11/2019 UBS/LoD

The APPC Pattern Benefiting from advances in component software technology using standard languages 1/11/2019 UBS/LoD

A C View B Map A C X Host B 1/11/2019 UBS/LoD

Design patterns and programming language features They are conceptually equivalent. Useful to explain language features using design pattern terminology. Danger of design patterns: workarounds needed to implement them can be tedious. Patterns are only used if benefit outweighs the cost. 1/11/2019 UBS/LoD

Motivation New component software ideas have a big acceptance threshold if they require non-standard language extensions. Use many of the good ideas with standard languages and suitable libraries/frameworks without language extension. Describe ideas using pattern terminology for easy reuse by designers/programmers. 1/11/2019 UBS/LoD

APPC:Intent Filter out noise from implementations: Describe popular behavior in terms of an idealized class structure (ideal class graph = view), using traversal strategy graphs and visitors as appropriate. Map idealized class structure to concrete class structure using traversal strategy graphs as appropriate. 1/11/2019 UBS/LoD

APPC:Motivation Have an encapsulated unit for behavioral abstraction for new behavior covering multiple classes. Concrete class structures are overworked and to favor reusability each behavior should be expressed in terms of an idealized class structure that is mapped flexibly into a concrete class structure. 1/11/2019 UBS/LoD

Motivation ICG/CCG approach not only favors reusability, but it also makes programs simpler. Program towards an abstract structure. Distinguish between "IS-A" and "PLAYS-THE-ROLE-OF" relationships. 1/11/2019 UBS/LoD

Motivation Make visitor pattern more flexible. Reuse with different class structures or multiple times in same class structure. 1/11/2019 UBS/LoD

APPC:Applicability Use to implement "popular" behaviors. Whenever you have a class hierarchy (IS-A) and a behavior assignment to classes (PLAYS-THE-ROLE-OF) so that the IS-A structure cross-cuts the behavior assignment. 1/11/2019 UBS/LoD

APPC:Applicability Whenever the implementation of a behavior uses classes that are not central to the behavior. 1/11/2019 UBS/LoD

APPC:Structure Strategy graph, expansion Ideal class graph, expansion An APPC has local data and functions and a constructor and an upstream and downstream interface. 1/11/2019 UBS/LoD

APPC:Structure An APPC provides an implementation for a set of functions (the upstream interface) that modify classes that implement the downstream interface of the APPC. 1/11/2019 UBS/LoD

APPC:Implementation Code runs in CCG objects. Demeter/Java also uses this approach 1/11/2019 UBS/LoD

APPC:Implementation Mapping from ICG to CCG is best specified with Traversal Support for Java. 1/11/2019 UBS/LoD

Law of APPCs The downstream interface must be a set of pure abstract functions. Clients of the APPC must not use the downstream interface. 1/11/2019 UBS/LoD

Law of APPCs The implementation of the popular functions must be protected against changes by the client classes. 1/11/2019 UBS/LoD

Law of APPCs The implementation of popular functions is allowed to use the DI/UI/local methods and methods of classes returned by the DI/UI/local methods and nothing more (Application of Law of Demeter Principle). Less restrictive but it is ok to have violations of standard LoD. 1/11/2019 UBS/LoD

Simulating APPCs Translate UI into Java interface. Assigning to class structure: use delegation to ego objects. 1/11/2019 UBS/LoD