Leveraging Adaptive Programming and the Law of Demeter in Java for Effective Object Management
This document explores the implementation of Adaptive Programming (AP) and the Law of Demeter (LoD) in Java, facilitated by the DJ library. It discusses the significance of limiting knowledge between units to reduce information overload, advocating for methods that only engage with closely related units. By utilizing XPath expressions for DOM navigation within Java, developers can achieve a low-learning-curve implementation. The document highlights features of DJ, including traversal specifications that enhance generic programming and offers practical examples including the use of visitor patterns.
Leveraging Adaptive Programming and the Law of Demeter in Java for Effective Object Management
E N D
Presentation Transcript
AP/DJ • AP: a generic technology • DJ: a low-learning-curve implementation of AP
From Mike Mannion • Regarding DOM navigation, I would be very interested in taking a look at the library you mention (DJ). Can I use XPath expressions to navigate a DOM within Java? That would be very useful indeed!
Law of Demeter Principle • Each unit should only have limited knowledge about other units: only about 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
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
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. AP is a reaction to this view of the Law of Demeter
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 • DJ • APPC • Demeter/Java
DJ • An implementation of AP using only the DJ library and the Java Generic Library (JGL) • All programs written in pure Java • Intended as prototyping tool: makes heavy use of introspection in Java • Integrates Generic Programming (a la STL) and Adaptive programming
Integration of Generic and Adaptive Programming • A traversal specification turns an object graph into a container. • Can invoke 50+ generic algorithms on those containers. Examples: add, delete, find, etc. • What is gained: genericity not only with respect to data structure implementations but also with respect to class graph
Sample DJ code // Find the user with the specified uid Container libUsers = new Container(library, "from Library to User"); User user = libUsers.find("uid", uid);
In addition: Traverse method: excellent support for Visitor Pattern // class ClassGraph Object traverse(Object o, Strategy s, Visitor v); traverse navigates through Object o following traversal specification s and executing the before and after methods in visitor v ClassGraph is computed using introspection
Using DJ: BusRoute example public int count (){ // class BusRoute getT().traverse( this, new Visitor(){ int r; public void before(Person host) { r++; } public void start() { r = 0;} …) } }
Using DJ TraversalGraph getT() {return new TraversalGraph( classGraph1, new Strategy( “from BusRoute via BusStop to Person”));}
More info • DJ Home Page: www.ccs.neu.edu/research/demeter/DJ