Presentation is loading. Please wait.

Presentation is loading. Please wait.

Adaptive Programming in JAsCo

Similar presentations


Presentation on theme: "Adaptive Programming in JAsCo"— Presentation transcript:

1 Adaptive Programming in JAsCo
Wim Vanderperren | pag. 1

2 Adaptive Programming (1)
Law of Demeter: a method should talk only to its friends: arguments and part objects (computed or stored) and newly created objects Example: class Test { Test2 test2; public void do() { test2.getTest3().start(5); } NOT GOOD JAsCoAP | pag. 2

3 Adaptive Programming (1)
Law of Demeter: a method should talk only to its friends: arguments and part objects (computed or stored) and newly created objects Better Example: class Test2 { Test3 test3; public void do(int i){ test3.start(i); } class Test { Test2 test2; public void do() { test2.do(5); } BUT: “do” logic is spread over several classes now JAsCoAP | pag. 3

4 Adaptive Programming (2)
Law of Demeter: a method should talk only to its friends: arguments and part objects (computed or stored) and newly created objects Dilemma: Crosscutting concerns (if follow) or Unmaintainable code (if not follow) AP is the solution to this dilemma JAsCoAP | pag. 4

5 Adaptive Programming (3)?
 Save contents of DataStores to file … ClassA DS Visitor from ClassA to DataStore AP Option Class SaveVisitor { saveA(ClassA a); saveB(ClassB b); } Class Save { save(ClassA a) { a.B.C.DataStore; a.F.E.Datastore; } DS Visitor Option 1 Option 2 Crosscutting Concerns Violates Law of Demeter ClassB ClassF Data- Store ClassC ClassD ClassE Data- Store JAsCoAP | pag. 5

6 Adaptive Programming (4)
Traversal Strategy: Specifies the abstract traversal that has to be followed. Example: From Test to Test3 Adaptive Visitor: Implements the behavior itself Visits the concrete traversal as specified by the abstract traversal strategy. JAsCoAP | pag. 6

7 Structure-shy visitors Robustness wrt evolution of base program
Advantages of AP Structure-shy visitors Robustness wrt evolution of base program Separation of concerns Complementary to all OO based approaches, even AOP Optimal visiting process JAsCoAP | pag. 7

8 Why combine AP & JAsCo? class DataStorePersistence extends Visitor { public void before(DataStore store) { if(changedPV(store)) { Writer writer = … writer.writeObject(store.getData()); } public boolean changedPV(DataStore s) { \\ true if changed since last visit What if we want to deploy AP visitors in a component- based context … AP visitors are already structure-shy. But, still refer to specific types and APIs! void backup(ClassA a) { ClassGraph cg = new ClassGraph(“a”); Strategy sg = new Strategy(“from ClassA to DataStore”); TraversalGraph tg = new TraversalGraph(sg, cg); tg.traverse(a, new DataStorePersistence()); } JAsCoAP | pag. 8

9 Represent Adaptive Visitors as regular aspect beans
JAsCo AP: the idea Represent Adaptive Visitors as regular aspect beans Introduce a new kind of connector, called “traversal connector”, for instantiating an aspect bean as an adaptive visitor on a certain traversal strategy. JAsCoAP | pag. 9

10 Brief JAsCo Intro call, cflow, withincode, target
class DataPersistence { hook Backup { Backup(triggeringmethod(..args)) { execution(triggeringmethod); } isApplicable() { // true when changed since last visit } before() { Writer writer = … writer.writeObject(getDataMethod()); } refinable Object getDataMethod(); Deploy Abstract method parameter call, cflow, withincode, target connector PersistenceConnector { DataPersistence.Backup hook = new DataPersistence.Backup( * DataStore.set*(*)); } Abstract pointcut Triggering condition around, around throwing, around returning, after, after throwing, after returning refining DataPersistence.Backup for DataStore { public Object getDataMethod() { DataStore d = thisJoinPointObject; return d.getData(); } JAsCoAP | pag. 10

11 Aspect Beans as AP visitors
 An dedicated connector: traversal connector. from … bypassing … from … via … Traversal Strategy traversalconnector BackupTraversal(“from ClassA to *”) { DataPersistence.Backup hook = new DataPersistence.Backup(visiting DataStore); hook.before(); } Object type to visit Advice to invoke & Precedence visiting * visiting Data* public void backup(ClassA a) { BackupTraversal myBackup = BackupTraversal.getInstance(); myBackup.traverse(a); } hook1.before(); hook2.before(); hook2.after(); hook1.after(); Explicit invocation JAsCoAP | pag. 11

12 JAsCoAP: Precedence Strategies
traversalconnector BackupTraversal("from system.Root to *") { DataPersistenceAspectBean.Backup backup = new DataPersistenceAspectBean.Backup(visiting DataStore) { public void getDataMethod(Object context) { DataStore store = (DataStore) context; return store.getData(); } }; Logger.FileLogger logger = new Logger.FileLogger(visiting *); logger.before(); backup.before(); JAsCoAP | pag. 12

13 Combining multiple AP visitors
 How to combine the behavior of multiple AP visitors? Only execute if Save Visitor executed ClassA Save Visitor Save Visitor Log Visitor ClassB ClassF from ClassA to * Data- Store ClassC ClassD ClassE Data- Store But keep AP visitor implementation independent … JAsCoAP | pag. 13

14 JAsCoAP: Combination Strategies
public class TwinCombinationStrategy implements CombinationStrategy { private Object hookA,hookB; TwinCombinationStrategy(Object a, Object b) { this.hookA=a; this.hookB=b; } HookList verifyCombinations(HookList list) { if(!list.contains(hookA)) list.remove(hookB); return list; JAsCoAP | pag. 14

15 JAsCoAP: Combination Strategies (2)
traversalconnector BackupTraversal("from system.Root to *") { DataPersistenceAspectBean.Backup backup = new DataPersistenceAspectBean.Backup(visiting DataStore) { public void getDataMethod(Object context) { DataStore store = (DataStore) context; return store.getData(); } }; Logger.FileLogger logger = new Logger.FileLogger(visiting *); logger.before(); backup.before(); TwinCombinationStrategy twin = new TwinCombinationStrategy(backup,logger); addCombinationStrategy( twin); JAsCoAP | pag. 15

16 JAsCoAP: Advanced Traversal Strategies
Bypassing: “from A bypassing C to B” via : “from A via C to B” ->Start,field,End : “from A via ->C,f,* to B” x A C f D B E E g g JAsCoAP | pag. 16

17 Structure-shy visitors Robustness wrt evolution of base program
Advantages of JAsCo AP Structure-shy visitors Robustness wrt evolution of base program Separation of concerns Increases Aspect Bean reusability Completely Abstract Adaptive Visitors represented as Aspect Beans Managing combined visiting behavior by Precedence&Combination strategies JAsCoAP | pag. 17

18 Exercises JAsCoAP | pag. 18


Download ppt "Adaptive Programming in JAsCo"

Similar presentations


Ads by Google