Lecture 21 Crosscutting Aspect Oriented Programming CS 655

  • Slides: 13
Download presentation
Lecture 21: Crosscutting Aspect. Oriented Programming CS 655: Programming Languages David Evans University of

Lecture 21: Crosscutting Aspect. Oriented Programming CS 655: Programming Languages David Evans University of Virginia CS 655 http: //www. cs. virginia. edu/~evans Computer Science

Adaptive Programming: Demeter • Instance of AOP [Lieberherr 92] • Separate the program text

Adaptive Programming: Demeter • Instance of AOP [Lieberherr 92] • 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 10/25/2021 University of Virginia CS 655 2

Law of Demeter 1. Law of Demeter: a method should talk only to its

Law of Demeter 1. Law of Demeter: a method should talk only to its friends: 1. arguments and part objects (computed or stored) 2. newly created objects 2. Dilemma: 1. Small method problem of OO (if followed) 2. Unmaintainable code (if not followed) 3. Traversal strategies are the solution to this dilemma 4. Demeter = Greek Goddess of Agriculture (grow software from small blocks) 10/25/2021 University of Virginia CS 655 3

Slide adapted from Karl Lieberherr talk AP Example: UML Class Diagram bus. Stops Bus.

Slide adapted from Karl Lieberherr talk AP Example: UML Class Diagram bus. Stops Bus. Route Bus. Stop. List buses 0. . * Bus. Stop Bus. List 0. . * passengers Bus Person. List Person 10/25/2021 waiting AOP/Demeter 0. . * 4

Slide adapted from Karl Lieberherr talk Collaborating Classes Find all persons waiting at any

Slide adapted from Karl Lieberherr talk Collaborating Classes Find all persons waiting at any bus stop on a bus route bus. Stops Bus. Route buses Bus. List 0. . * Bus. Stop. List OO solution: one method for each red class Bus. Stop waiting passengers Bus Person. List Person 10/25/2021 0. . * AOP/Demeter 0. . * 5

Java Solution (excerpt) class Bus. Route { Bus. Stop. List busstops; void print. Waiting.

Java Solution (excerpt) class Bus. Route { Bus. Stop. List busstops; void print. Waiting. Passengers () { busstops->print. Waiting. Passengers (); } } class Bus. Stop. List { Bus. Stop stops[]; void print. Waiting. Passengers () { for (int i = 0; i < stops. length; i++) stops[i]. print. Waiting. Passengers (); } } 10/25/2021 University of Virginia CS 655 6

Java Solution (cont. ) class Bus. Stop { Person. List waiting; void print. Waiting.

Java Solution (cont. ) class Bus. Stop { Person. List waiting; void print. Waiting. Passengers () { waiting. print (); } } class Person. List { 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); } } 10/25/2021 University of Virginia CS 655 7

Demeter Approach • Devise a traversal strategy • Specify code for different types of

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 10/25/2021 University of Virginia CS 655 8

Slide adapted from Karl Lieberherr talk Traversal Strategy First try: from Bus. Route to

Slide adapted from Karl Lieberherr talk Traversal Strategy First try: from Bus. Route to Person bus. Stops Bus. Route Bus. Stop. List buses 0. . * Bus. Stop Bus. List 0. . * passengers Bus Person. List Person 10/25/2021 waiting AOP/Demeter 0. . * 9

Slide adapted from Karl Lieberherr talk Traversal Strategy from Bus. Route through Bus. Stop

Slide adapted from Karl Lieberherr talk Traversal Strategy from Bus. Route through Bus. Stop to Person bus. Stops Bus. Route Bus. Stop. List buses 0. . * Bus. Stop Bus. List 0. . * passengers Bus Person. List Person 10/25/2021 waiting AOP/Demeter 0. . * 10

Slide adapted from Karl Lieberherr talk Robustness of Strategy from Bus. Route bypassing Bus

Slide adapted from Karl Lieberherr talk Robustness of Strategy from Bus. Route bypassing Bus to Person Bus. Route buses Bus. List 0. . * villages Village. List 0. . * Village bus. Stops 0. . * Bus. Stop waiting passengers Bus Person. List Person 10/25/2021 Bus. Stop. List AOP/Demeter 0. . * 11

Slide adapted from Karl Lieberherr talk Filter out noise in class diagram • only

Slide adapted from Karl Lieberherr talk Filter out noise in class diagram • only three out of seven classes are mentioned in traversal strategy! from Bus. Route through Bus. Stop to Person replaces traversal methods for the classes Bus. Route Village. List Village Bus. Stop. List Bus. Stop Person. List Person 10/25/2021 AOP/Demeter 12

Summary • Aspect-Oriented Programming and Adaptive Programming provide programmers with new expressive options •

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 10/25/2021 University of Virginia CS 655 13