Download presentation
Presentation is loading. Please wait.
Published byاحسان میردامادی Modified over 5 years ago
1
AOSD and the Law of Demeter: Shyness in Programming
Karl Lieberherr Demeter Research Group Northeastern University Boston DEMETER DHMHTRA
2
PhD Visitation Weekend 2003
Coupling Aspect-Oriented and Adaptive Programming: Shyness in Programming PhD Visitation Weekend 2003
3
Our Intuition behind AOP
Our Aspect-Oriented Programming (AOP) intuition has been: "adaptiveness". It comes in two flavors: adaptiveness to the class graph ("painting the class graph in broad strokes with code") and to the call graph of the traversal ("picking points in the graph where additional code gets called").
4
MIT Technology Review 2001 DEMETER DHMHTRA
Aspect-oriented programming is called “adaptive programming” at Northeastern University. DEMETER DHMHTRA
5
Definitions y is x-shy if:
(1) y relies only on minimal information of x (2) y can adapt to small changes in x (3) y is loosely coupled with x (4) y can work with x1, x2, ... which are close or similar to x. What is a concern? A concern is something that the programmer cares about.
6
Examples of concerns the programmer has to deal with
Production concerns How do I compute the price allowing for multiple pricing schemes? Non-Production concerns What do I have to print to understand why this program does not work? Are all objects of class A created in class Afactory?
7
Scattering and Tangling: Static
aspecti is scattered across many classes (i = 1,2,3) class X tangles aspects 1, 2 and 3 class A consisting of three aspects class diagram aspect1 aspect2 aspect3 Class X classes for aspect1 classes for aspect3 classes for aspect2 Adding to classes
8
Scattering and Tangling: Dynamic
Each aspect (colors) is scattered across many classes (shapes) Class tangles all three aspects program execution involving three aspects (colors r b g) this(s) f(..) target(t) program call tree (classes executing method calls) Enhancing calls t.f(..); classes At those calls the aspect enhances the behavior
9
AP-Concern-shy DEMETER DHMHTRA
AP-Structure-shy AP-WildCard AP-Strategy AP-Call AP-Demeter AP-Concern-shy Program with multiple building blocks, say b1:B1 and b2:B2, that are woven together: b1*b2. b1 relies only on partial information about b2 which makes b1 more robust and reusable. Goal: b1 should be loosely coupled to b2. DEMETER DHMHTRA
10
Law of Demeter (LoD) for Concerns
A concern implementation should not rely on too much information about other concern implementations. (Classic LoD: A method should not rely on too much information about other classes/objects.) Ian Holland PhD 1992: Vice President of Architecture and Systems Engineering at Kronos Incorporated in Chelmsford, MA empls.
11
Adaptive Programming. AP-Concern-shy: concerns shy of other concerns
AP-Structure-shy: concerns shy of graph structure AP-WildCard: aspects shy through wildcards AspectJ: *, .., this, target, args, call, execution, … (call graph) AP-Strategy: three level model using strategies AP-Call: aspects shy of call graph using strategies AspectJ: cflow AP-Demeter: behavior shy of class graph using strategies; advice on traversal AP-DJ: ClassGraph, Strategy, Visitor (in Java) AP-DAJ: Strategy enhances ClassGraph; Visitor enhances traversal defined by Strategy (in AspectJ) AP-DemeterJ (new programming language) 50 pages of theory Best of both worlds
12
Adaptive Programming. AP-Concern-shy AP-Structure-shy AP-WildCard
AP-Strategy AP-Call AP-Demeter Alternative organization: AP-Concern-shy AP-Graph-shy AP-CallGraph-shy AP-ClassGraph-shy AP-WildCard AP-Strategy X-shy subX-shy Mechanism-to-achieve-shyness
13
AP-Concern-shy AP-Structure-shy AP-WildCard AP-Strategy AP-Call AP-Demeter AP-Structure-shy Program with multiple building blocks including b1and b2:Graph, that are woven together. b1 relies only on partial information about b2 which makes b1 more robust and reusable. Goal: b1 should be loosely coupled to b2. b1 enhances b2 at nodes and edges.
14
AP-WildCard DEMETER DHMHTRA
AP-Concern-shy AP-Structure-shy AP-WildCard AP-Strategy AP-Call AP-Demeter AP-WildCard Program with multiple building blocks including b1 and b2:Graph, that are woven together. b1 uses wildcard techniques which makes b1 more robust and reusable. Goal: b1 should be loosely coupled to b2. DEMETER DHMHTRA
15
One contributor: Ignacio Silva-Lepe PhD 1994
Currently at IBM Watson Research Lab AP-Concern-shy AP-Structure-shy AP-WildCard AP-Strategy AP-Call AP-Demeter AP-Strategy Program with multiple building blocks including b1:Strategy and b2:Graph, that are woven together. b1 reveals only partial information about b2 which makes b1 more robust and reusable. b1 is written against an abstraction of b2 so that the application to b2 is well defined. Goal: b1 should be loosely coupled to b2.
16
A General Strategy-based Adaptive Mechanism
From TOPLAS 2003 paper (Lieberherr, Patt-Shamir, Orleans) A General Strategy-based Adaptive Mechanism Three layers of graphs: Bottom, Middle, Top Bottom layer: trees to select subtrees guided by top layer. Each bottom layer tree has a graph from the Middle layer associated with it that contains meta-information about the bottom layer tree. Acts as an abstraction barrier between the top and bottom layers. Used to reduce search space. Top layer graph is basically a subgraph of the transitive closure of the middle layer graph, decorated with additional information attached to the edges.
17
Top graph: subgraph of transitive closure of middle layer
Middle graph: Abstraction barrier B A C Bottom tree: select subtrees B c1:C c2:C c3:C A
18
Strategy-based adaptiveness
The call graph application (AspectJ): Top: computational pattern, Middle: static call graph, Bottom: call tree. The standard application (Demeter): Top: strategy graph, Middle: class graph, Bottom: object trees. DEMETER DHMHTRA
19
AP-Concern-shy AP-Structure-shy AP-WildCard AP-Strategy AP-Call AP-Demeter AP-Call Program with multiple building blocks including b1:CrossCut and b2:CallGraph, that are woven together. b1 reveals only partial information about b2 which makes b1 more robust and reusable. Advice on b2 at b1.
20
AP-Concern-shy AP-Structure-shy AP-WildCard AP-Strategy AP-Call AP-Demeter AP-AspectJ Program with multiple building blocks including b1:PointCut and b2:CallGraph, that are woven together. b1 reveals only partial information about b2 which makes b1 more robust and reusable. Advice on b2 at b1.
21
AP-AspectJ Many AspectJ programs are adaptive (designed for a family of Java programs) Context: Java program or its execution tree (lexical joinpoints or dynamic join points) Features enabling adaptiveness: *, .. (wildcards) cflow, + (graph transitivity) this(s), target(s), args(a), call (…), … (inheritance as wild card) pc(Object s, Object t): this(s) && target(t) && call(… f …)
22
AP-Concern-shy AP-Structure-shy AP-WildCard AP-Strategy AP-Call AP-Demeter AP-COOL Program with multiple building blocks including b1:Coordinator and b2:CallGraph, that are woven together. b1 reveals only partial information about b2 which makes b1 more robust and reusable. Advice on b2 at b1. Crista Lopes, PhD 1997 Assistant Professor at UC Irvine, first PhD thesis on AOP.
23
AP-Concern-shy AP-Structure-shy AP-WildCard AP-Strategy AP-Call AP-Demeter AP-Demeter Program with multiple building blocks including b1:Strategy and b2:ClassGraph, that are woven together. b1 reveals only partial information about b2 which makes b1 more robust and reusable. Advice on traversal defined by b1 and b2.
24
AP-DJ Program in terms of ClassGraph-, Strategy- and Visitor-objects.
AP-Concern-shy AP-Structure-shy AP-WildCard AP-Strategy AP-Call AP-Demeter AP-DJ Program in terms of ClassGraph-, Strategy- and Visitor-objects. Example: in Java: cg.traverse(o, s, v); The easiest tool to learn good structure-shy programming
25
AP-Concern-shy AP-Structure-shy AP-WildCard AP-Strategy AP-Call AP-Demeter AP-DAJ Program in terms of Strategy-objects that introduce traversal methods into ClassGraph-objects (adaptiveness to class graph). Enhance the execution of the traversal methods with Visitor-objects that may modify the run-time traversal (adaptiveness to the traversal execution). Example: In AspectJ: declare traversal f(): s V; o.f();
26
crosscutting base Adaptive to family of bases Connected join points Isolated join points
27
Demeter crosscutting I
Class graph or Object graph From Company to Salary Adaptive to family of class graphs From BusRoute via BusStop to Person Connected join points
28
Demeter crosscutting II
Static call graph or Dynamic call graph From Company to Salary Adaptive to family of traversal call graphs …_salary(Employee, Object) Connected join points Isolated join points
29
AspectJ crosscutting I
Class graph or Object graph Company.cache(){} Vector BusRoute.busses; Isolated join points
30
Static call graph or Dynamic call graph AspectJ crosscutting II Adaptive to family of call graphs cflow target(Employee) Connected join points Isolated join points
31
Demeter crosscutting Graph From BusRoute via Bus to Person Adaptive to family of graphs …_salary(Employee, Object) Connected join points Isolated join points
32
How are AP and AOP coupled?
AOP: module-shy programming Modularize programs that cut across modules (with minimal reliance on information in modules). Programming is module-shy if the modular structure of the program does not prevent concerns that cut across other concerns to be modularized. AP: concern-shy programming Can we view concern implementations as modules?
33
Many open questions DEMETER DHMHTRA
Doug Orleans: Simple model of AOP: Fred Johan Ovlinger: Modules and Aspects Pengcheng Wu: Statically Executable Advice Theo Skotiniotis: Contracts for Aspects DEMETER DHMHTRA
34
The End
35
Crosscutting in Demeter
generated Java program Demeter program structure-shy functionality structure replicated! aspect descriptions are not just copied into programs, they may be replicated several times. synchronization
36
range of AOP languages means of … join points JPM join points
identifying specifying semantics at AspectJ dynamic JPM points in execution call, get, set… signatures w/ wildcards & other properties of JPs advice static JPM class members signatures add members DemeterJ, DAJ dynamic JPM static JPM 1 static JPM 2 static JPM 3 when traversal reaches object or edge class members visitor method signatures traversal spec. s class graph g class names class graph visitor method bodies s + g (result = traversal implementation) add members class graph with tokens=grammar (result = parsing and printing implementation)
37
Adaptiveness The next 7 viewgraphs show how two traversals (parts of an adaptive program) adapt to two different class graphs.
38
Class graph: Find undefined things
Ident System * Thing * usedThings * * def S T Definition body Body D B definedThings= from System bypassing Body to Thing usedThings = from System through Body to Thing
39
M1: Equation System EquationSystem equations Equation_List Ident *
Variable lhs Equation Numerical rhs Expression_List Simple args Expression * op Add Compound
40
M1: Equation System definedThings = from EquationSystem
bypassing Expression to Variable M1: Equation System EquationSystem equations Equation_List Ident * lhs Equation Variable Numerical rhs Simple args Expression Expression_List S T * op Add Compound D B
41
M1: Equation System usedThings = from EquationSystem
through Expression to Variable M1: Equation System EquationSystem equations Equation_List Ident * lhs Equation Variable Numerical rhs Simple args Expression Expression_List S T * op Add Compound D B
42
CS1: Grammar 0..* Entry EParse entries Grammar BParse Production Body
rhs parts Part lhs 0..* NonTerm Concrete Abstract
43
CS1: Grammar definedThings = from Grammar bypassing Body to NonTerm S
0..* Entry EParse entries Grammar BParse Production Body rhs parts Part lhs 0..* NonTerm S T Concrete Abstract D B
44
CS1:Grammar usedThings = from Grammar through Body to NonTerm S T D B
0..* Entry EParse entries Grammar BParse Production Body rhs parts Part lhs 0..* NonTerm S T Concrete Abstract D B
45
Software Structure with ACs Software Structure with ACs
P1 P1 P1 P2 P2 P2 P3 P3 P3 P1 P1 P1 P4 P4 P4 P5 P5 P5 P3 P3 P3 P2 P2 P2 P2 P2 P2 P6 P6 P6 P1 P1 P1 45 45 45
46
AP-structure-shy: notion of crosscutting
B2 = program and its execution trees or an abstraction thereof: UML class diagram and its object diagrams. A program b1*b2 is aspect-oriented if it is crosscutting. Examples: Adaptive, aspect-oriented programs: Policies (Concurrency, Distribution, Authentication, Logging), Adaptive Method, Law of Demeter Checker in AspectJ
47
Scattering B2 is a graph. Count number of nodes and edges that are enhanced. Scat(b1*b2) = number of nodes and edges in b2 enhanced by b1. The higher the number, the more the crosscutting. A program b1*B2 is crosscutting if there is an infinite sequence R1, R2, … of B2 so that Scat(b1*R1), Scat(b1*R2), … is strictly increasing.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.