130 likes | 136 Views
Explore Demeter approach in AOP, separate program text and class structure, and use traversal strategies to maintain code. Law of Demeter explained.
E N D
Lecture 21: Crosscutting Aspect-Oriented Programming David Evans http://www.cs.virginia.edu/~evans CS655: Programming Languages University of Virginia Computer Science
Adaptive Programming: Demeter • Instance of AOP [Lieberherr92] • Separate the program text and the class structure • Program is independent of class graph • Accomplish tasks by traversals • Specification for what parts of received object should be traversed • Code fragments for what to execute when specific object types are encountered University of Virginia CS 655
Law of Demeter • Law of Demeter: a method should talk only to its friends: • arguments and part objects (computed or stored) • newly created objects • Dilemma: • Small method problem of OO (if followed) • Unmaintainable code (if not followed) • Traversal strategies are the solution to this dilemma • Demeter = Greek Goddess of Agriculture (grow software from small blocks) University of Virginia CS 655
Slide adapted from Karl Lieberherr talk AP Example: UML Class Diagram busStops BusRoute BusStopList buses 0..* BusStop BusList waiting 0..* passengers Bus PersonList Person 0..* AOP/Demeter
Slide adapted from Karl Lieberherr talk Collaborating Classes Find all persons waiting at any bus stop on a bus route busStops BusRoute BusStopList OO solution: one method for each red class buses 0..* BusStop BusList waiting 0..* passengers Bus PersonList Person 0..* AOP/Demeter
Java Solution (excerpt) class BusRoute { BusStopList busstops; void printWaitingPassengers () { busstops->printWaitingPassengers (); } } class BusStopList { BusStop stops[]; void printWaitingPassengers () { for (int i = 0; i < stops.length; i++) stops[i].printWaitingPassengers (); } } University of Virginia CS 655
Java Solution (cont.) class BusStop { PersonList waiting; void printWaitingPassengers () { waiting.print (); } } class PersonList { Person people[]; void print () { for (int i = 0; i < people.length; i++) people[i].print (); } } class Person { String name; void print () { System.stdout.println (name); } } University of Virginia CS 655
Demeter Approach • Devise a traversal strategy • Specify code for different types of objects reached on a traversal • Example: code prints name if object is a Person • Independent of class graph University of Virginia CS 655
Slide adapted from Karl Lieberherr talk Traversal Strategy First try:from BusRoute to Person busStops BusRoute BusStopList BusRoute BusStopList buses 0..* BusStop BusStop BusList BusList waiting 0..* passengers Bus PersonList Bus PersonList Person Person 0..* AOP/Demeter
Slide adapted from Karl Lieberherr talk Traversal Strategy from BusRoute through BusStop to Person busStops BusRoute BusStopList buses 0..* BusStop BusList waiting 0..* passengers Bus PersonList Person 0..* AOP/Demeter
Slide adapted from Karl Lieberherr talk Robustness of Strategy from BusRoute bypassing Bus to Person villages BusRoute BusStopList buses VillageList busStops 0..* 0..* BusStop BusList Village waiting 0..* passengers Bus PersonList Person 0..* AOP/Demeter
Slide adapted from Karl Lieberherr talk Filter out noise in class diagram • only three out of seven classes • are mentioned in traversal • strategy! from BusRoute through BusStop to Person replaces traversal methods for the classes BusRoute VillageList Village BusStopList BusStop PersonList Person AOP/Demeter
Summary • Aspect-Oriented Programming and Adaptive Programming provide programmers with new expressive options • Active research area (Separation of Concerns Workshops at OOPSLA, ICSE, ECOOP, etc.) • Many directions to explore University of Virginia CS 655