1 / 35

Aspect Oriented Patterns

Aspect Oriented Patterns. محمد مهدی طالبی. دانشگاه صنعتی امیرکبیر ( پلی تکنیک تهران) پاییز 89- چند ساعت قبل از شب یلدا. اتاریخچه برنامه نویسی جنبه گرا. اولین بار توسط گريگور كيزالس (پروفسور دانشگاه UBC )، در کشور هلند مطرح شد .

lavonne
Download Presentation

Aspect Oriented Patterns

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Aspect Oriented Patterns محمد مهدی طالبی دانشگاه صنعتی امیرکبیر (پلی تکنیک تهران) پاییز 89- چند ساعت قبل از شب یلدا

  2. اتاریخچه برنامه نویسی جنبه گرا • اولین بار توسط گريگور كيزالس (پروفسور دانشگاه UBC )، در کشور هلندمطرح شد . • اولين ارائه رسمي از اين موضوع طی مقاله ای در یک کنفرانس اروپایی در رابطه با برنامه نویسی شئ گرا تحت عنوان ” Aspect-oriented programming“ طی سال 1997 مطرح شد . G. Kiczales, J. Irwin, J. Lamping, J.-M. Loingtier,C. Lopes, C. Maeda, and A.Menhdhekar. Aspect-oriented programming. In M. Ak¸sit and S. Matsuoka, editors, Proceedings of the European Conference on Object-Oriented Programming, volume 1241, pages 220–242, Berlin, Heidelberg, and New York, 1997. Springer-Verlag. • به تدریج با افزایش اقبال به سمت این تفکر AspectJ به وجود آمد ، که از برنامه نویسی جنبه گرا حمایت میکرد .

  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 Maghawry, Ahmed R.Dawood. Aspect Oriented GOF Design Patterns .In Proceedings of the The 7th International Conference on Informatics and Systems (INFOS), 2010 . [4] Jan Hannemann and GregorKiczales. 2002. Design pattern implementation in Java and aspectJ. In Proceedings of the 17th ACM SIGPLAN conference on Object-oriented programming, systems, languages, and applications (OOPSLA `02). [5] Eduardo Kessler Piveta and Luiz 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] RadoslavMenkyna. Towards combining aspect-oriented design patterns.InM´ariaBielikov´a, editor, Proc. Informatics and Information Technologies Student Research Conference, IIT.SRC 2007, pages 1–8, Bratislava,Slovakia, 2007. [7] Miles, R.: AspectJ Cookbook. O’Reilly, 2004. [8] Bruno Dufour, Christopher Goard, Laurie Hendren, Oege de Moor, GaneshSittampalam, and Clark Verbrugge. 2004. Measuring the dynamic behaviour of AspectJ 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. بررسی قدرت AOP در Exception Handling • در هر نرم افزاری قسمتی از کد به یافتن خطا و برخورد با آن اختصاص میابد . • پروسه به هم آمیختن رفتار معمول و رفتار غیر معمول در برنامه میتواند منجر به پدیده Cross Cutting Concern شود . • تفکر AOP میتواند به نحو قابل توجهی حجم کد مربوط به برخورد با خطا را کاهش دهد برای نشان دادن این مطلب به سراغ JWAM Framework رفته و آن را بازنویسی میکنیم [2]. • JWAM یک Framework است که در دانشگاه هامبورگ آلمان تهیه شد و بعد ها حالت تجاری به خود گرفت و دارای بیش از 600 کلاس میباشد . • برای نشان دادن قدرت AOP ، کد JWAM با رویکرد جنبه گرا باز نویسی شد و مشاهده شد حجم کد مربوط به برخورد با خطا به اندازه ¼ کاهش پیدا کرده است.

  6. ارائه یک مثال Class point { Static bool compare(point p1,pointp2){ if(p1 == null || p2==null ) throw Exception(“Arg Null Exception in Point.Compare”); …. } // end of compare(point p1,pointp2) . . . }//end of Class point • فرض بگیرید 600 کلاس دارید که هر کدام 20 تابع با آرگومان از نوع Object دارند : 12000= 20*600 (تعداد مکان برخورد با خطا ) 24000= 2*12000 (تعداد خط کد برای برخورد با این خطا )

  7. ارائه یک مثال Class point { Static bool compare(point p1,pointp2){ …. } // end of compare(point. . . . }//end of Class point /************************************<Aspect>******************************************/ aspect ExceptionHandler { pointcut HasObjArg(data e, Args *args): execution(void *.*(*)) ) && target(e) before(data e , Args *args) : HasObjArg () { if ( chkArgnull(Args) == true ) throw Exception(“Arg Null Exception in”+e.Name); } }//end of aspectUpdateSignaling Point.*() Point.Set*(int,int) Aspect 24000  10 PointCut Advice

  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 ) Adding pointcut design pattern to an advice design pattern requires usually a change in existing advice pattern.

  9. بررسی برخی مدل های ارائه شده در کتاب GOF گاهی مجرد سازی به تنهایی قادر به کپسوله سازی Cross Cutting Concern ها نیست. در بسیاری موارد pattern ها با کد اصلی چنان در هم می آمیزند که عملا نمی توان به آنها به عنوان یک ماجول مستقل نگاه کرد. اختلاط کد مربوط به pattern ها باعث میگردد تا اضافه و یا حذف کردن آنها هزینه بر باشد. در پیاده سازی های صورت گرفته از pattern های GOF در 75% موارد افزایش modularity و در 52% موارد افزایش reusability را داشته ایم[4] . Jan Hannemann and GregorKiczales. 2002. Design pattern implementation in Java and aspectJ. In Proceedings of the 17th ACM SIGPLAN conference on Object-oriented programming, systems, languages, and applications (OOPSLA `02).

  10. پیاده سازی Observer با استفاده از Aspect Oriented Programming زمانی که از observer استفاده میکنیم باید تغییراتی را در کلاس های موجود ایجاد کنیم : • 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. ارائه یک مثال public class Celsius{ private doubledegrees; public double getDegrees() {return degrees;} public void setDegrees(doubleaDegrees) { degrees = aDegrees; } Celsius(double aDegrees){ setDegrees(aDegrees); } } public class Thermometer{ privateCelsiustempSource; public voidsetTempSource(Celsius atempSource){tempSource = atempSource;} public Celsius getTempSource(){ return tempSource; } public void drawTemperature(){} } public class CelsiusThermometer extends Thermometer{ public void drawTemperature(){ System.out.println("Temperature in Celsius"+ getTempSource().getDegrees()); } } public class FahrenheitThermometer extends Thermometer{ public void drawTemperature(){ System.out.println("Temperature in Fahrenheit:"+ (1.8 * getTempSource().getDegrees())+32);} }

  13. ارائه یک مثال abstract aspect ObserverPattern { abstract pointcutstateChanges(Subject s); after(Subject s): stateChanges(s) { for (inti = 0; i < s.getObservers().size(); i++) ((Observer)s.getObservers().elementAt(i)).update(); } private Vector Subject.observers = new Vector(); public void Subject.add(Observer obs) { observers.addElement(obs); obs.setSubject(this); } public void Subject.remove(Observer obs) { observers.removeElement(obs); obs.setSubject(null); } public Vector Subject.getObservers() { return observers; } private Subject Observer.subject = null; public void Observer.setSubject(Subject s) { subject = s; } public Subject Observer.getSubject() { return subject; } }

  14. ارائه یک مثال aspect ObserverPatternImpl extends ObserverPattern { declare parents: CelsiusimplementsSubject; public Object Celsius.getData() { return this; } declare parents: Thermometer implements Observer; public void Thermometer.update() { drawTemperature(); } pointcutstateChanges(Subject s): target(s) && call(void Celsius.setDegrees(..)); }

  15. پیاده سازی Observer با استفاده از Aspect Oriented Programming

  16. پیاده سازی Observer با استفاده از Aspect Oriented 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 are going to be notified as well as what is going to be executed when the Concrete Observers are notified.

  17. پیاده سازی Observer با استفاده از Aspect Oriented Programming استفاده از تفکر جنبه گرا باعث میشود که بدون آنکه بدنه کلاس ها را تغییر دهید ، بتوانید از مزایای آن استفاده نمایید . استفاده از تفکر جنبه گرا باعث میشود تا کد مربوط به observer از کد اصلی برنامه جدا شود . با استفاده از این روش reusability را افزایش داده ایم زیرا میتوان تنها با تغییر concrete aspect ها از آن برای کلاس های دیگر نیز استفاده نمود.

  18. مزایای استفاده از رویکرد جنبه گرا در Observer • 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. • Compositiontransparency: 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-inheritance in their implementation . The open class mechanism in AspectJ provides us with a more flexible way of implementing these patterns, as it allows to attach both interfaces and implementations (code) to existing classes. • Breaking cyclic dependencies : In the AspectJ implementation ,the indirection introduced by the ConcreteMediator aspect removes the cyclic dependencies. aspectJ Java

  20. Mediator

  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.

  23. Director • Problem You want to define a set of roles to be implemented by unknown sets of application classes so they can be interacted with generically by an abstract aspect.

  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.

  25. Border Control Design public aspect BorderControllerAspect { /** * Specifies the testing region. */ public pointcut withinTestingRegion( ) : within(com.oreilly.aspectjcookbook.testing.+); /** * Specifies My Applications region. */ public pointcut withinMyApp( ) : within(com.oreilly.aspectjcookbook. myapp.+); /** * 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(..)); } pointcut regionsOfInterest( ) : BorderControllerAspect.withinMyAppMainMethod( ) || BorderControllerAspect.withinThirdParty( ) || BorderControllerAspect.withinTestingRegion( );.

  26. Combination 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.

  27. BenchMarks • Quick Sort • Checking for return of null • Visitor • Strategy • Bean

  28. Checking for return of null

  29. checking for return of null

  30. Visitor

  31. Strategy

  32. AOSD-Europe

  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 Maghawry, Ahmed R.Dawood. Aspect Oriented GOF Design Patterns .In Proceedings of the The 7th International Conference on Informatics and Systems (INFOS), 2010 . [4] Jan Hannemann and GregorKiczales. 2002. Design pattern implementation in Java and aspectJ. In Proceedings of the 17th ACM SIGPLAN conference on Object-oriented programming, systems, languages, and applications (OOPSLA `02). [5] Eduardo Kessler Piveta and Luiz 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] RadoslavMenkyna. Towards combining aspect-oriented design patterns.InM´ariaBielikov´a, editor, Proc. Informatics and Information Technologies Student Research Conference, IIT.SRC 2007, pages 1–8, Bratislava,Slovakia, 2007. [7] Miles, R.: AspectJ Cookbook. O’Reilly, 2004. [8] Bruno Dufour, Christopher Goard, Laurie Hendren, Oege de Moor, GaneshSittampalam, and Clark Verbrugge. 2004. Measuring the dynamic behaviour of AspectJ programs. In Proceedings of the 19th annual ACM SIGPLAN conference on Object-oriented programming, systems, languages, and applications (OOPSLA '04).

  34. با تشکر ؟

  35. Lemos, O. A., Ferrari, F. C., Masiero, P. C., and Lopes, C. V. 2006. Testing aspect-oriented programming Pointcut Descriptors. In Proceedings of the 2nd Workshop on Testing Aspect-Oriented Programs (Portland, Maine, July 20 - 20, 2006). WTAOP '06. ACM, New York, NY, 33-38. DOI= http://doi.acm.org/10.1145/1146374.1146380

More Related