1 / 47

Aspect-Oriented Programming

Aspect-Oriented Programming. Department of Computer and System Instituto Tecnológico de Morelia jcolivar@itmorelia.edu.mx http://antares.itmorelia.edu.mx/~jcolivar/. M. Sc. Juan Carlos Olivares Rojas. Disclaimer.

kimcooper
Télécharger la présentation

Aspect-Oriented Programming

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-OrientedProgramming Department of Computer and System Instituto Tecnológico de Morelia jcolivar@itmorelia.edu.mx http://antares.itmorelia.edu.mx/~jcolivar/ M. Sc. Juan Carlos Olivares Rojas

  2. Disclaimer Some material in this presentation has been obtained from various sources, each of which has intellectual property, so in this presentation will only have some rights reserved. These slides are free, so you can add, modify, and delete slides (including this one) and slide content to suit your needs. They obviously represent a lot of work on my part. In return for use, I only ask the following: if you use these slides (e.g., in a class) in substantially unaltered form, that you mention their source.

  3. Agenda • Introduction • Aspect-Oriented Programming Fundamentals • Examples • Project of Unit 4

  4. Objectives of theSession • The students will know a new programming paradigm: AOP (Aspect-Oriented Programming). • The students will development a program usin this paradigm or refactoring existing applications.

  5. Software DevelopmentToday

  6. Evolución del SW Software Evolution

  7. Evolución del SW Software Evolution

  8. Software DevelopmentProblems A complex system can visualize like as combined implementation of multiple concerns

  9. Software DevelopmentProblems class Book { ….. <all things about book> <error handling> … } class Partner { ….. <all things about Partner> <error handling> <access controlling> } class Rent {….. <all things about Rent> <error handling> <access controlling> }

  10. Software DevelopmentProblems Access Control class BookStore { private Book [] books ; private Partner [] partners; public BookStore() { … public void load( Partner p, Book b) { if validControlAccess() then{ // code of the method } else{ throwException(); } } public void addPartner(Partner p){ if validContolAccess() then{ // code of the method } else{ throwException(); } } // the rest of the class methods }

  11. Tyranny of the Dominant Decomposition • Descomposition by Form, Color or size • We must choose only one principal model

  12. Tyranny of the Dominant Decomposition Order by Form • Order by Color

  13. Color-FormHierarchy We must elect one principal model. In this case: color and later, form

  14. AOP Definition AOP was created by Gregor Kickzales and his Research Team at Palo Alto Research Center in 1996. “ An Aspect is a modular unit that is spreand in another functional units. The aspects exist in the design stage as in the implementación stage…” An Aspect is a concept that it’s difficult encapsulate clear and easily.

  15. AOP • An aspect is the unit which encapsulate a cross-cutting concern. • AOP promoves the separation of concerns through mechanisms which abstract and compose this concepts troughout the system.

  16. TraditionalStructure of a Compiler

  17. CompilingProcess in POA

  18. AOP Program Structure Program

  19. AOP Program Structure

  20. POA Concepts

  21. AnAspectDefinition Aspect Control { JoinPoint securityOperations = call s BookStore.loan & calls BookStore.addPartner& ... Before securityOperations: { if !=(validControlAcces()) then{ throwsExcepcion(); } }

  22. AOP and OOP Relation Class A Class A1 Clase A2 Attb1 Attb2 Attb 3 Method 1 Method 2 Method 1 OOP: common concerns AOP: crosscuting concerns

  23. OOP Evolutionto AOP

  24. POA Advantages A code less complicated, more natural and more reduced. More facilities to think about the concerns, since they are separate and dependencies between them are minimal. An easier code to debug and to mantein.

  25. AOP Tools Langauges for Programming Aspects: AspectJ: A Java Extension. The most popular. AspectC++, AspectS, CAESAR. .NET Languages: Weave.NET, Source Weave.

  26. Class Bank Problem Transaction Persistence Traceability public class Bank { // other declarations public double processDebit(long idAccount, double amount) { // openning a transaction try { // retrieve account // business validation // business logic associated with debit // persistencia del nuevo estado // traceo del movimiento para auditoria // cierre exitoso de la transacción (commit) return new amount account; } catch (Exception e) { // trace the audit exception // close abnormal transaction (rollback) // relauch the exception in superior layers } } // other business process declarations }

  27. Aspectual Descomposition Separation of concerns Aims to isolate cross cutting concerns Each one of these concerns will be implemented in a separate unit.

  28. Aspectual Recomposition

  29. AOP Bank Version Transaccionalidad Persistencia Trazabilidad public classBank { // other declarations public doubleprocessDebit(long account, double amount) { // validaciones del negocio // debit business logic returnnew amount account; } // another business methods declarations }

  30. HelloWorldwithAspects package mx.edu.itmorelia.aspects; public class HW { private String message; public HW() { this.message = “Hello World"; } public void setMessge(String M){ this.menssage = M; } public String getMessage(){ return this.message; } public void showMessage(){ System.out.println(this.message); } }

  31. HelloWorldwithAspects package mx.edu.itmorelia.aspects; public class HelloWorld { public static void main(String[] args) { HW H; H= new HW(); H.showMenssage(); } }

  32. package mx.edu.itmorelia.aspects; public aspect Aspect { pointcut messagesToPrint() : call (void HW.showMessage()); before(): messagesToPrint(){ System.out.println(“Hi everybody!"); } after(): messagesToPrint(){ System.out.println(“Chao everybody!"); } } HelloWorldwithAspects

  33. Alternatives to Aspects • Languages (OO, Componet-Based) • Design Patterns • Reflection

  34. PointCut

  35. PointCut • When a particular methos is executed: • execution(void Point.setX(int)) • When a method is invocated: call(void Point.setX(int)) • When a error handling is invocated: handler(ArrayOutOfBoundsException) • When the object is actually executed: this(SomeType).

  36. PointCut Examples • When a method belongs to a class: • within(MyClass) • When the JoinPoint is in the control flow of a call main method we need to use: cflow • The target point refers to any possible JoinPoint

  37. Pointcut Designator Wildcards • It’s possible to use wildcards • What do the next instructions do? • execution(* *(..)) • call(* set(..)) • execution(int *()) • call(* setY(long)) • call(* Point.setY(int)) • call(*.new(int, int))

  38. PointCut JoinPoint Types Methods Constructors Get/Set Exception Handler a Line call join points dispatch execution join points

  39. PointCut Examples • We can applicate the next operations: or (“||”), and (“&&”) and not (“!”). • Examples: • target(Point) && call(int *()) • call(* *(..)) && (within(Line) || within(Point)) • within(*) && execution(*.new(int)) • !this(Point) && call(int *(..))

  40. Advice Type • before advice • after advice • after returning • after throwing • after • around advice

  41. Parameterized Advice • We can acces to the context of a JoinPoint as follow: • pointcut setXY(FigureElement fe, int x, int y): call(void FigureElement.setXY(int, int)) && target(fe) && args(x, y); • after(FigureElement fe, int x, int y) returning: setXY(fe, x, y) { System.out.println(fe + " moved to (" + x + ", " + y + ").");}

  42. Another Example Figure FigureElement moveBy(int, int) makePoint(..)makeLine(..) Point Line getX()getY()setX(int)setY(int)moveBy(int, int) getP1()getP2()setP1(Point)setP2(Point)moveBy(int, int) Display * 2 HistoryUpdating

  43. Another Example SessionInterceptor requestMap(request) beforeBody(req, resp) ... Session getAttribute(name) setAttribute(name, val) invalidate() ... HTTPRequest getCookies() getRequestURI()(doc) getSession()getRequestedSessionId() ... HTTPResponse getRequest() setContentType(contentType) getOutptutStream()setSessionId(id) ... Servlet

  44. Project for Unit 4 • Developping a J2ME project which implements the Triangle and Ecuation2 class. • The validation and error handling process must be programming in aspects. • The development team will have two members exlcusively.

  45. Project for Unit 4 • The project consist in two parts: programming (70%) and modelling (20%) (Aspect extension for UML) • The program must be test on a mobile devices (not emulators) • Deadline: Thursday, December 4 at class.

  46. References Mejía, P. (2008), Programación Orientada a Aspectos, CINVESTAV, México. Quintero, A. (2000), VisiónGeneral de la ProgramaciónOrientada a Aspectos. Languages and Information System Department. Informatic and Estadistic Faculty, Sevilla University, Spain. Rodriguez M., POA, Gerente Relaciones Académicas, Microsoft Cono Sur

  47. ¿Preguntas?

More Related