Aspect-Orientation (AO*)
Total Page:16
File Type:pdf, Size:1020Kb
Lecture 7 Aspect-orientation (AO*) A new paradigm in Software Engineering Copyright © Yijun Yu, 2005 Spring 2005 ECE450H1S Software Engineering II Last lecture and tutorial … Software Quality Measurements • We have shown the use of quality measurements to monitor the progress of software development • The development/restructuring (maintenance) activities (refactoring, tuning, adding features) can be guided by the metrics of softgoals Spring 2005 ECE450H1S Software Engineering II Today … On Aspect Orientation • Today we explain the paradigm of aspect- orientation 1. Concepts: What are aspects? 2. Practices: Aspect-orientation at large – AOP: Aspect-oriented programming – AOSD: Aspect-oriented software development – AORE: Aspect-oriented requirements engineering – AOSR: Aspect-oriented software reuse (probably next lecture) 3. A case study of AORE 4. Summary Spring 2005 ECE450H1S Software Engineering II 1. What are aspects? 1. Some design principles • Divide and conquer: problem solving/design principle • Modularization: high cohesion/low coupling Separation of concerns • DRY: Don’t Repeat Yourself Increase the fan-in 2. Previous paradigms • 70s – 80s: Structured programming (Goto’s considered harmful) => Structured Analysis, Structured Design • 80s – 90s: Object-oriented programming (OOP) => OOA/OOD => UML 3. Why another paradigm ? • Since late 90s … Separation of the crosscutting concerns 4. What are aspects? – Modularizing the crosscutting concerns Spring 2005 ECE450H1S Software Engineering II 1.1 Some design principles Structured programming • What is structured program? – A program has no more GOTO’s – Only three kinds of structure prevails • Sequential • If-then-else • Loops [Dijkstra: Goto considered harmful] – In other words, every statement block has single-entry, single-exit as Hammock Graph [Weiser: Program slicing] • “Whenever possible, we wish to maximize fan-in during the design process. Fan-in is the raison d’être of modularity. Each instance of multiple fan-in means that some duplicate code has been avoided.” raison d'être: grounds for existence (http://www.french-linguistics.co.uk/dictionary/) [Yourdon & Constantine79] Structured Design (pg. 172, see also http://wwwpa.win.tue.nl/wstomv/quotes/structured-design.html) [parnas: Modularization, information hiding] Spring 2005 ECE450H1S Software Engineering II (1) A decomposition hierarchy from abstract to concrete: Divide and Conquer, Structured Design; (2) Don’t Repeat Yourself, Factoring / Refactoring … C C C CC C C Spring 2005 ECE450H1S Software Engineering II Example MOVE UNTIL VALUE FIND SCAN REMOVALL STRINGCOMP Yourdon & Constantine, SD, pg.168 Spring 2005 ECE450H1S Software Engineering II 1.1 Some design principles Object-oriented programming • Everything is an object (Smalltalk) • Information hiding / Encapsulation: object groups related data and the operations on the data into a module • Object has structural relationships: – inheritance: generalization / specialization: isA/instanceOf – aggregation : hasA / isPartOf – associations: 1-to-many, 1-to-1, many-to-many • In the end, the structurally-related objects are packaged into components Spring 2005 ECE450H1S Software Engineering II 1.2 Aspect-orientation • Component language (any structured or OO language, even corresponding design and requirements specification) • What are crosscutting concerns? • An aspect language – What are joinpoints? – What are pointcuts? – What are advices? • A weaving mechanism Spring 2005 ECE450H1S Software Engineering II Aspect concepts • Concepts: cross-cutting, AOP hides the join points component, aspect, join points, weaving C Aspect Spring 2005 ECE450H1S Software Engineering II AOP (THE MAGIC) MOVE UNTIL VALUE FIND SCAN REMOVALL STRINGCOMP Spring 2005 ECE450H1S Software Engineering II AOP (NOT REALLY MAGIC) VALUE MOVE UNTIL FIND SCAN REMOVALL FIND: AT LINE 5 VALUE: AT LINE7 SCAN: AT LINE 15 MOVE UNTIL: AT LINE 8 REMOVALL: AT LINE 2 STRINGCOMP Spring 2005 ECE450H1S Software Engineering II AOP example ApplicationSession StandardSession privat e lon g la stAcc essed = cr eatio nTim e; if (ina ctive Inte rval != -1 ) { stream. write Obje ct(ne w Lon g(las tAcce ssed Time) ); pri vate int inact iveIn terva l = - 1; int thisI nterv al = (int) (Syst em.cu rrent Time Milli s() - void a ccess ed() { lastAcc essed ) / 1000; // s et l ast a ccess ed to this Acce ssTim e as it wi ll be lef t ove r sM.acc essed (ctx , req uest, sess ionId ); // f rom the p revio us ac cess if ( thisI nterv al > inact iveI nterv al) { last Acce ssed = thi sAcce ssTim e; inval idate (); public void acc essed ( Con text ctx, Requ est r eq, S tring id ) { /* public void inva lidat e() { package org. apac he.to mcat. sessi on; /** this Acce ssTim e = S ystem .curr entT imeMi llis( ); Appl icat ionSe ssion apS= (Appl icat ionSe ssion )find Sessi on( ctx, * ==== ===== ==== ===== ===== ===== ===== ==== ===== ===== ===== ===== ==== ===== == serv erSe ssion .remo veApp licat ionS essio n(con text) ; /** * Bind an o bject to t his s essio n, u sing the s pecif ied n ame. If an ob ject Serve rSess ionMa nager ssm = id); * * Retur n th e <co de>Ht tpSes sion< /cod e> fo r whi ch th is * of th e sa me na me is alre ady b ound to t his s essio n, th e ob ject is vali date (); S erver Sessi onMan ager .getM anage r(); if( apS= =null ) ret urn; * The Apach e So ftwar e Lic ense, Vers ion 1.1 // r emov e eve rythi ng in the sess ion import java. io.I OExce ption ; object * repla ced. } * import java. io.O bject Input Strea m; * is th e fa cade. * <p> ssm.r emove Sessi on(th is); Serv erSe ssion serv S=apS .getS erve rSess ion() ; * Copy right (c) 1999 The Apach e Sof twar e Fou ndati on. All r ight s Enum erat ion e num = valu es.ke ys() ; import java. io.O bject Outpu tStre am; */ * After thi s met hod e xecut es, a nd i f the obje ct im pleme nts voi d val idat e() { } serv S.ac cesse d(); * rese rved. whil e (e num.h asMor eElem ents( )) { import java. io.S erial izabl e; pub lic H ttpS essio n get Sessi on() { * <code >Htt pSess ionBi nding Liste ner< /code >, th e con taine r ca lls // i f we have an i nacti ve in terv al, c heck to se e if we'v e exc eeded } apS. acce ssed( ); * Stri ng na me = (Stri ng)en um.n extEl ement (); import java. util .Enum erati on; * <code >val ueBou nd()< /code > on the objec t. it } * Redi strib utio n and use in so urce and binar y for ms, w ith o r wi thout remo veVal ue(na me); import java. util .Hash table ; retu rn ( (Http Sessi on) t his); * if ( inac tiveI nterv al != -1) { // c ache it - no n eed t o com pute it a gain * modi ficat ion, are permi tted provi ded that the f ollow ing c ondi tions } import java. util .Vect or; * @para m na me Na me to whic h the obj ect i s bou nd, c annot be null int thisI nterv al = privat e lon g la stAcc essed Time = cre atio nTime ; req. setS essio n( ap S ); * are met: import javax .ser vlet. Servl etExc eptio n; } * @para m va lue O bject to b e bou nd, canno t be null (int) (Syst em.cu rrent Time Milli s() - last Acces sed) / 10 00; * vali d = false ; import javax .ser vlet. http. HttpS essio n; * } * 1. R edist ribu tions of s ource code mus t ret ain t he ab ove c opyr ight } import javax .ser vlet. http. HttpS essio nBin dingE vent; * @exce ptio n Ill egalS tateE xcept ion if th is me thod is ca lled on a n if ( thisI nterv al > inact iveI nterv al) { * n otice , th is li st of cond ition s an d the foll owing disc laim er. import javax .ser vlet. http. HttpS essio nBin dingL isten er; // ----- ---- ----- ----- ----- ----- ---- ----- ----- ----- - * inva lida ted s essio n inval idate (); /** * pub lic b oole an is New() { import javax .ser vlet. http. HttpS essio nCon text; Session Publ ic M ethod s * } /** * Retur n th e las t tim e the clie nt s ent a requ est * 2. R edist ribu tions in b inary form mus t rep roduc e the abov e co pyrig ht if ( ! va lid) { import org.a pach e.tom cat.c atali na.*; * @depr ecat ed As of V ersio n 2.2 , th is me thod is re place d by } * Inval idat e all sess ions that have expi red. associa ted w ith this * n otice , th is li st of cond ition s an d the foll owing disc laim er in Stri ng ms g = s m.get Strin g("a pplic ation Sessi on.se ssio n.ise "); import org.a pach e.tom cat.u til.S tring Mana ger; * <cod e>se tAttr ibute ()</c ode> } */ * sessi on, as th e num ber o f mil lise conds sinc e mid night , * t he do cume ntati on an d/or other mat erial s pro vided with the /** */ pri vate void proc essEx pires () { January 1, 1 970 * d istri buti on. thro w new Ille galSt ateEx cept ion(m sg); /* * Updat e th e acc essed time info rmat ion f or th is se ssion . pub lic v oid putVa lue(S tring name , Ob ject value ) { public long get LastA ccess edTim e() { * GMT. Act ions that your appli cati on ta kes, such as * } /** This m*e tPheordfo rm t he in terna l pro cessi ng r equir ed to inva lidat e if ( vali d) { long tim eNow = Sys tem.c urren tTim eMill is(); getting or s etti ng * 3.