1 / 13

Demeter Approach in Aspect-Oriented Programming

Explore Demeter approach in AOP, separate program text and class structure, and use traversal strategies to maintain code. Law of Demeter explained.

fountainr
Download Presentation

Demeter Approach in Aspect-Oriented Programming

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. Lecture 21: Crosscutting Aspect-Oriented Programming David Evans http://www.cs.virginia.edu/~evans CS655: Programming Languages University of Virginia Computer Science

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

More Related