صفحه 1:
|
Aspect Oriented
Patterns
دانشگاه صنعتی امیر کبپر (یلی تکنیک تهران)
پاییز ۸۹- چند ساعت قبل از شب یلدا
صفحه 2:
اتاریخچه برنامه نویسی جنبه گرا
* _اولن بر توسطگریگو یزاس (پرفضوردنشگاه 196 در كثشور هلتد مطرح شد م
وین اه رسمی از این موضوع Mla lo ای در یک کفراس اربایی در ره پا نامه نویسی شی
ad ple VA JLo Lb “Aspect-oriented programming
®, eer, rata, Loo, LD, Lore O. oper, O. Deak, اس وال90 اجه
بوسر 120. Oot od ©. Oxtrche, edie, Proveerkae oP ke Oxropes Ord wrewe a Ober
York, 107. سل rape OOD-OPO, Berbs, Leridbors, wad 160 سا بو وق امین
0
را تحت عنوان *
* . به تدریج با افزایش اقبال په سمت این تفکر [57۸()به وجود آمد . كه از برنامه نويسى جنبه كرا حمایت میکرد
صفحه 3:
[1] Kiczales, G. and Mezini, M. 2005. Aspect-oriented programming and modular reasoning. In Proceedings of
‘the 27th international Conference on Software Engineering (St. Louis, MO, USA, May 15-21, 2005),
[2} Lippert, M. and Lopes, C. V. 2000. A study on exception detection and handling using aspect- oriented
programming.
In Proceedings of the 22nd international Conference on Software Engineering (Limerick, Ireland, June 04-11,
2000),
[3] Noura El Maghawsy, Ahmed R.Dawood. Aspect Oriented GOF Design Patterns .In Proceedings of the The 7%
International Conference on Informatics and Systems (INFOS), 2010,
[4] Jan Hannemann and Gregor Kiczales. 2002. Design pattern implementation in Java and aspect]. In
Proceedings of the
17th ACM SIGPLAN conference on Object-oriented programming, systems, languages, and applications (OOPSLA
02).
[5] Eduardo Kessler Piveta and Lulz Carlos Zancanella. Observer Pattern using Aspect-Oriented Programming.
In Proc. of
the 3rd Latin American Conference on Pattern Languages of Programming, Porto de Galinhas, PE, Brazil, August
2003,
[6] Radoslav Menkyna. Towards combining aspect-oriented design patterns.In Maria Bielikov’a, editor, Proc.
Informatics
and Information Technologies Student Research Conference, IIT:SRC 2007, pages 1-
Bratislava, Slovakia, 2007.
[7] Miles, Rs AspectJ Cookbook. O'Reilly, 2004.
[8] Bruno Dufour, Christopher Goard, Laurie Hendren, Ooge de Moor, Ganesh Sittampalam, and Clark Verbrugge.
2004,
Measuring the dynamic behaviour of Aspect programs. In Proceedings of the 19th annual ACM SIGPLAN
‘conference
‘on Object-oriented programming, systems, languages, and applications (OOPSLA ‘04).
صفحه 4:
Concern ®
Tangling °
Cross-Cutting Concern °
صفحه 5:
Exception Handling 52 AOP 4538 بررسى
مر سريرم افزارى gad از دب یافتن خ و بخورد با آن اختصاس ید
پروسه به هم آميختن رفتار معمول و رفتار غير معمول در برنامهمیوندمنر به پدیده 60669 6۳۵55
8 شود
تفكر 801 ميتوائد به تحو قابل توجهى حجم كد مربؤظ به برخورد با خظا را كاهش دهد بای نشان دادن این مطلب به
él» ۴۲۵۲0۵۷۵۲ 1/۸۵۲[ رفته و أن را بازتويسى ميكنيم (1۳
* اللطالازيى/1/06 508:6 لسككه در دلنشكاء هابوركا لانت هيد شد و بعد ها حا لتقجرىيه خود كرفتو
دارا يثراز 5٠٠ كاشرمبياشد:
© براى نشان دادن قدرت ۰/۵08 کد 4/۵14[ با رويكرد جنبه كرا باز تويسى شد و مشاهده شد حجم کد مربوط به پرخورد
خط به اندازه 3/4 كاهش بيدا كرده است.
صفحه 6:
ارائه یک مثال
Class point
1
Static bool compare(point p1,pointp2){
if{p1 == null |] p2==null )
throw Exception("Arg Null Exception in Point.Compare”);
} I end of compare(point p1,pointp2)
}lend of Class point
* _ فرض بگیرید 0000 کلاس دارید كه هر کدام 200 تابع با آرگومان از نوع 00[668 دارند :
( تعداذ مكازيسرخورد با خطا) 0 -12000
( تسعداد خط كد براعيسرخورد با لينخطا) 0 * -24000
صفحه 7:
ارائه یک مثال
Class point
{
Static boo! compare(point p1,pointp2){
} // end of compare(point.
۱
throw Exception(“Arg Null Exception in”+e.Name);
}iend of aspect UpdateSignaling
صفحه 8:
Exception Introduction Pattern
From the nature of aspects some design pattern can be added to
another without the modification of the existing pattern. ( Exception
Introduction , Policy )
Dublic abstract aspect ExceptoniniroductionAspet {
abstract pointcut operations();
U pointcut operations defines where should exception occur.
When this is defined as another design pattern exception introduction is used together with an existing patiern
Mand any change of its code is required.
Object around : operations) {
try {
return proceed);
}eatch (CheckedException ex) {
throw new RuntimeBixception(ex);
1 CheckedExceprion will be caught and nw runtime exception will be thrown,
}
* Adding pointcut design pattern to an advice design pattern requires
usually a change
in existing advice pattern.
صفحه 9:
بررسی برخی مدل های ارائه شده در کتاب 650۴
\ Cross Cutting Concern گاهی مجرد سازی ب تنهابى قادر به كبسوله سازى *
* ادر بسيارى موارة 6806659 هابا كد اصلى جنان در هم مى أميزند كه عملا نمی تون ب نها به نان یک ماجول مستقل ناه
كرد
* اختلاط كد مربوط به eek te pattern
رد تا اضاقه ويا حذف كردن أنها هزبته بر باشد.
* در بياده سازى هاى صورت تكرفته از 8868677 های 660۴ در 1۷۵ مور افلیش ۳00/109۲310 و در 1۵۲ مور افزایش
reusability را داشته {rll
Jan Hannemann and Gregor Kiczales. 2002. Design pattern implementation in
Java and aspeetJ. In Proceedings of the 17th ACM SIGPLAN conference on
Object-oriented programming, systems, languages, and applications (OOPSLA
*02).
صفحه 10:
پیاده سازی ۵0056۲۷6۲ با استفاده از 0۲16۳60 ]66 ک۸
Programming
زمانى كه از 005611/©1 استفاده ميكنيم بايد تقيراتى را ذر كلاس هاى موجود يجاد كنيم :
ConereteObserver
piste) سه
cbsoverStata
زاو
forall observers
رت
زد سم 0
۳
Subject
‘AtachiQtservet)
Deacty Observer)
Natt!) ده
;
ConcreteSubjact
Gotta) O--+
SoiStaio)
تست
* Attach
* Detach
* Notify
صفحه 11:
interface Observer {
void setSubject(Subject s);
Subject getSubject();
void update();
interface Subject {
void add(Observer obs);
void remove(Observer
obs);
Vector getObservers();
Object getData();
صفحه 12:
صفحه 13:
صفحه 14:
صفحه 15:
پیاده سازی 0056۳۷6۲ با استفاده از 0۲16۳60 ]۸۵5۵66
Programming
“<Aintorace>>
Subject ‘<intrface>>
Observer
‘Sacelods Onserven void
‘Sremove(ous : Observe) void Ssetsunjoctis: Subjecd “vole
‘Suetonserersd ObsenerT] ۱ |
‘SpetDatad و مود
ee we
<cAspacio>
ObsenerPatern
تیه ‘Observer {|
server subject: Subject
ay Class
Any Coes Sexpoinicub> statechanges(s: Subjecd
‘S<<advice=> after(Subject 6); staleChanges(s)0
‘Ssubjectada(ons | Observer) ۵
‘Ssubjectrercve(ohs Obeere) Observer
af امه هو
حابصم 0
Concrete Subject 7
۳
ع0 ]
‘Sceclare parents: ConersteSubject implements Subject
‘Sceclae parents: CancreteOhserve implements Observer
‘S<apointeut>» stateChanges(s: Subjec
صفحه 16:
پیاده سازی ۵0056۲۷6۲ با استفاده از 0۲16۳60 ]66 ک۸
Programming
+ Concrete Subject
Store state information to be used by Concrete Observers. It does
not,
however, send notifications to its Observers. This responsibility is
part of the
Observer Pattern role.
* Concrete Observers
Servers as basis to field and method's introduction performed by the
Observer
Pattern.
* Observer Pattern
The Observer Pattern is an abstract aspect that encapsulate the
behavior of
the Observer pattern. The Observer Pattern contains the fields and
methods
to be included in the classes that are affected by the Concrete
Observer
Pattern.
* Concrete Observer Pattern
This participant specifies in what situations the Concrete Observers
صفحه 17:
پیاده سازی 0056۳۷6۲ با استفاده از 0۲16۳60 ]۸۵5۵66
Programming
استفاده از تفكر جنبه كرا باعث ميشود كه بدون آنكه بدنه كلاس ها را تغبير دهيد ؛ يتونيد از مزاياى أن استفاده تماييد .
© استفلده از تفكر جنبه كرا باعث ميشود تا كد مربوط به 010561۷/6۲ از كد اصلى برتامه جدا شود .
* .با استفاده از اين روش /6 هاه 5لا»؟ را افزايش داده ايم زيرا ميتوان تنها با تفيير gists concrete aspect
براى كلاس la ديكر نيز استفاده تمود.
0
15761 02
11
صفحه 18:
مزایای استفاده از رویکرد جنبه گرا در 6۳ 0056۳۷
Locality : All the code that implements the Observer pattern is in the abstract
and concrete observer aspects none of it is in the participant classes.
Reusability : The core pattern code is abstracted and reusable. The
implementation of ObserverProtocol is generalizing the overall pattern behavior.
Composition transparency : Because a pattern participant's implementation
is not coupled to the pattern, if a Subject or Observer takes part in multiple
observing relationships their code does not become more complicated and the
pattern instances are not confused. Each instance of the pattern can be reasoned
about independently.
(Un)pluggability : Because Subjects and Observers need not be aware of their
role in any pattern instance, it is possible to switch between using a pattern and
not using it in the system.
صفحه 19:
بررسی برخی موارد خاص
Multiple inheritance and Java : some of the patterns make use of multiple-
‘MediatorPattern
(code)
2
ConcreteMediator
(code)
Colleague 1 wr ی Colleague 3
Colleague 3 Colleague 4
aspect)
tt
ع3
Colleague 2
(code)
Colleague 4
(code)
inheritance in
their implementation . The open class mechanism in Aspect] provides us with a
more flexible way of
implementing these patterns, as it allows to attach both interfaces and
implementations (code) to existing
‘ConcreteMediator
(code)
Java
classes.
Colleague 1
(code)
Colleague 3
(code)
صفحه 20:
Mediator
The quick brown fox...
Weight omodium @bold odemibold
Slant Oroman italic oblique
Cleon tensed
صفحه 21:
Aspect Oriented Patterns
* Wormhole
٠» Exception Introduction
© Participant
* Cuckoo’s Egg
° Director
° Border Control
* Policy
¢ Worker Object Creation
صفحه 22:
Cuckoo's Egg
* Problem
You want to override the type of object instantiated on a constructor call to.
return an object of a different class transparently to the original business logic.
اوه راومه وهای ae
‘Maintpplication 1 With the Choos Aspect the Sharedinetace reference
(le poiming oan nance Replacersenass
Sime det = new ign
۳5 تا ver |}
“<interface>>
=
7۳۳
وه Bet coratato a
نا نمی رفس مرا point atchConstucorall)
تست ۱ سس )
صفحه 23:
۳
و
Role!
tooth void
Basinesstiassh
07
Deciare the inbertance relationship
between BusinessCssA/Rolel and
BusinessCass8/Role?
‘<<aspec>>
DitectovAspect
لحك ست
> دما
Rolez
7
>64
SpecalizedAspect
‘eclare parents: BesinessClasc implements عام
declare parents BusinssClasB Implements Robe2
۳ ae. bar) oid
Provides any aditonal method Cs
کرد موز required since the new
interface & appied, that were nat
ariginally part ofthe affected dass.
Director
public vid doSomthingole roles, Ls
Toe? role}
1
|
anything about the
/ inaplemestations of Role1 and Rae 2
:مهما امد
صفحه 24:
Border Control Design
*Problem
You want to formally define important regions within your application so your
aspects can reuse those definitions to ensure they are only applied in the
correct areas.
<<aspect>> سم
ا
+ paintcut withinApplicationPackagel) ! testing application
prt tidied ی سس
+ ۵/0 ع سح ز سس
+ paintcut withinThirdPartyLibrary() ‘Maindpplication
۱ | :وكا [|
‘Any numberof regions canbe declared in|
‘the Border Controller in oder te spey
regions of significance within your
application
صفحه 25:
Border Control Design
public aspect BorderControllerAspect {
J+ * Specifies the testing region. */
public pointcut withinTestingRegion( ) : within(com.oreilly.aspectjcookbook.testing.
[*** Specifies My Applications region. */
public pointcut withinMyApp( ) : within(com.ore!
[*** Specifies a third party source code region. */
public pointcut withinThirdParty( ) :
within(com.oreilly.aspectjcookbook.thirdpartylibrary.+);
[+ * Specifies the applications main method. */
public pointcut withinMyAppMainMethod{ ) : withincode(public void
oreilly.myapp.MyClass. main(..));
1
aspectjcookbook. myapp.+);
pointcut regionsOfinterest( ) : BorderControllerAspect.withinMyAppMainMethod ) ||
BorderControllerAspect.withinThirdParty( ) || BorderControllerAspect.withinTestingRegion( );
صفحه 26:
ation Of Aspect
Patterns
Aspects usually alter behavior of base concerns without
requiring awareness on their side. This makes the combination of
aspect-oriented design patterns easier than the combination of
object oriented design patterns is
۳ ...- متسر
2 Policy
Director
Advice design و0
وت ی Exception
Introduction
pointcut advice ee
design pattern | design pattem ‘Creation
pointeut ‘without change
design pattern | change required
advice ‘without without
design pattem | change change
صفحه 27:
BenchMarks
Quick Sort
* Checking for return of null
* Visitor
* Strategy
* Bean
صفحه 28:
Checking for return of null
(First primitive pointeut matches all calls, second avoids those with void return type.
pointcut methodsThatRetumObjects(): call(* *.*(.)) && leall(void *.*(.));
Object around(): methodsThatRetmmnObjects)
{ Object IRetVal = proceed();
if (IRetVal == null)
{ System.err-printin( “Detected null return value after calling * +
thisJoinPoint.getSignature().toShortString) + * fn file ” +
thisJoinPoint getSourceL ocation().getFileName() + ات“ Line "+
thisJoinPoint.getSourcel ocation().getL.ine());
return IRetVal;
1
صفحه 29:
checking for return of null
‘Orig. | Checked | Orig Fixed Praned
Jaa | Java Aspect Aspect} Aspect
(oo nuit | (itt malt | (atl non-voie | (omy Objects | (ae within
ciects) | checks) | metiods) | methods) _| aspect code)
7
Time (Seconds) TH] 150 WRT 11 17
4 inst, (million bytecodes) 856 | 6 4810 1869 1256
mem. alloc. (million bytes) 18 18 5612 1494 23
"APPLICATION ONLY
lasses Toaded 2 2 WE, 1 [
instructions loaded 2207 | 21 13927 8183 7633
instructions dead gor | 105 6893 4959 4864
code coverage 60, 51 ost oe 0.36
ASPECT] TAG Mix (WHOLE PROGRAM) (2)
DEFAULT () 5 BOT 7
ASPECT-CODE (28) 36 27 50.
DVICEFRECUTE (I) 18 13 20
ADVICE_ARG-SETUP (2) 236 193 69
AROUND.CONVERSION (23) 67 07 10
AROUND_CATLBACK (24) 16.1 133 00
AROUND_PROCEED (25) 72 53 59
CLOSURE.INIT (26) 90 67 00
‘Overhead foal) 517 65 11
pointcut methodsThatRewrObjects(): call(Object *.*(.)) && !within (ib.aspects..”):
صفحه 30:
‘Aspe
133
356
199
1
258
30
038
11
46.9
27
27
11
30
199
7
222
20
0.90
‘WioLe PROGRAM
‘time (seconds)
# insiructions (million bytecodes)
‘memory allocated (million bytes)
Appricatton ONLY
classes loaded
instructions loaded
instructions dead
code coverage
ASPECT) TAG MIx (WHOLE PROGRAM) ("0)
DEFAULT (0)
AspECT.cODE (28)
TNTERMETHOD (14)
‘Overhead (total)
Visitor
صفحه 31:
1
1837
1
7
(hash able)
177
2166
1
1
362
0
9
11
3
‘Or
o7
17
(OLE PROGRAM)
YTECODES)
235
1913,
21
18
1
1932
23
5
aig
30
0.86
‘AsrEcr] Tac wix (WHOLE PEOGEAN)
Overhead (otal
INSTRUCTION COUNT BREAKDOWN (WHL
(tL. L10
Twa 1932
Aspect body
Real” cade (oral) 17
Strategy
Wiioir PRocRant
Tie (scons)
Insuetions(nllion byteeodes)
allocated (milion bytes)
‘APPLICATION ONLY
‘lasses loaded
instuctions loaded
instructions dead
code coverage
1۳۳۸۱0
۳
0 (TY
ADVICE_ARG.SETUP (2)
InreRFHELDGET (15)
InreRFIEL set (16)
Ovechead (ota)
صفحه 32:
AOSD-Europe
age) Viije Universiteit
ww ‘russel
Katholieke
Univer
Leuven
CeUVEN
“SLOVAK UNIVERSITY OF
TECHNOLOGY IN
BRATISLAVA,
GG TCHNGCHE Technische
* UNIVERSITAT Universitaet
S parwstact Darmstadt
Siemens
SIEMENS | ktiongesellschaft
xcs. iy | MEN
= _
۱
= 3
Coordinator:
‘The University of Lancaster
LANCASTER
Trinity Cotege
Dub
a
eM ۰
TEM, ت۳۳
م د ب« /
Institut National de Recherche en
Informatique et en Automatique
۱۳۳ مع
Tl ad
2
صفحه 33:
[1] Kiczales, G. and Mezini, M. 2005. Aspect-oriented programming and modular reasoning. In Proceedings of
‘the 27th international Conference on Software Engineering (St. Louis, MO, USA, May 15-21, 2005),
[2} Lippert, M. and Lopes, C. V. 2000. A study on exception detection and handling using aspect- oriented
programming.
In Proceedings of the 22nd international Conference on Software Engineering (Limerick, Ireland, June 04-11,
2000),
[3] Noura El Maghawsy, Ahmed R.Dawood. Aspect Oriented GOF Design Patterns .In Proceedings of the The 7%
International Conference on Informatics and Systems (INFOS), 2010,
[4] Jan Hannemann and Gregor Kiczales. 2002. Design pattern implementation in Java and aspect]. In
Proceedings of the
17th ACM SIGPLAN conference on Object-oriented programming, systems, languages, and applications (OOPSLA
02).
[5] Eduardo Kessler Piveta and Lulz Carlos Zancanella. Observer Pattern using Aspect-Oriented Programming.
In Proc. of
the 3rd Latin American Conference on Pattern Languages of Programming, Porto de Galinhas, PE, Brazil, August
2003,
[6] Radoslav Menkyna. Towards combining aspect-oriented design patterns.In Maria Bielikov’a, editor, Proc.
Informatics
and Information Technologies Student Research Conference, IIT:SRC 2007, pages 1-
Bratislava, Slovakia, 2007.
[7] Miles, Rs AspectJ Cookbook. O'Reilly, 2004.
[8] Bruno Dufour, Christopher Goard, Laurie Hendren, Ooge de Moor, Ganesh Sittampalam, and Clark Verbrugge.
2004,
Measuring the dynamic behaviour of Aspect programs. In Proceedings of the 19th annual ACM SIGPLAN
‘conference
‘on Object-oriented programming, systems, languages, and applications (OOPSLA ‘04).
صفحه 34:
صفحه 35:
Lene, O. ®., Parra .O., Daren, P. O., 20d Laren, 0.0. 0008. امه رس
مومس ۰ مومت فص موم of hr Ocal Derober mt Penny سوم
سوه (Porkanl, Octe, shiy CO - 20, ADDO). UPHOP WO. BOO, Deus Verk, OY, 99-99,
001 سنا AD ۱۱۵0۸۳۵۵۲۸۵۳۵۵۵۵
