Aspect-Oriented Programming
Coming up…What is AOP?Why do we need AOP?How does AOP work?Hello World2
What is AOP?Part of AOSDAn extension of OOP extracting cross-cutting functional units of systemsA means of programming such functional units separate from other code, which are then ‘woven’ togetherAn attempt at promoting good SE practices within OOP3
Why do we need AOP?Situation in a typical O-O system:A single requirement implemented by multiple components (tangling)A single component may include elements of multiple requirements (scattering)Change could require understanding and changing many componentsThis impacts on software reuse4
Tangling example – sharedbuffer5Tangled buffer management codeTangled synchronisation codeSource: Sommerville, I. (2007)
Scattering example – patient records6Source: Sommerville, I. (2007)The highlighted operations implement secondary concerns for the system, such as keeping patient and consultant details anonymousIf the policy regarding anonymity changed, we would need to recode each version of anonymise() throughout the system
The AOP approachThree key stages:Aspectual DecomopositionConcern ImplementationAspectual Recomposition7
How are concerns identified?8A prismSource: Laddad, R. (2003)
Concern TypesFunctionalQuality of ServicePolicySystemOrganisational9Functionalrelated to specific functionality to be included in the systemin a train control system, a specific functional concern is train brakingQuality of Servicerelated to the non-functional behaviour of a systemperformance, reliability, availabilityPolicyrelated to the overall policies that govern the use of the systemsecurity, safety, concerns related to business rulesSystemrelated to attributes of the system as a wholemaintainability, configurabilityOrganisationalrelated to organisational goals and prioritiesproducing a system within budget, making use of existing software assets, maintaining the reputation of an organisation
Concern ClassificationsCoreFunctional concerns that directly relate to the primary purpose of a system.SecondaryFunctionality that shares information with the core concernsFunctionality that satisfies NFRsCross-cuttingConcerns that apply to the system as a whole10
Cross-cutting example11New customerreq.Customer management req.Account managementreq.Securityreq.Cross-cuttingconcernsRecoveryreq.Core concernsSource: Sommerville, I. (2007)
Implementing ConcernsCoreClasses & OperationsCross-cutting / SecondaryAspectsAdvice; Join Points; Pointcuts12
AspectAspects are similar to classes in that they can:include data members and operationshave access specificationsdeclare themselves to be abstractextend classes and abstract aspects and implement interfacesbe embedded inside classes and interfaces as nested aspectsThey are dissimilar in that they cannot:be directly instantiatedinherit from concrete aspectsbe marked as privileged13
Join PointAny identifiable execution point in a systemA call to a methodThe method’s executionAssignment to a variableReturn statementObject constructionConditional checkComparisonException handlerLoopsNot all may be exposed by each AOP languageAspectJ does not expose loops14
PointcutPointcuts capture, or identify, one or more join points&&	||Need not be given a nameAnonymous pointcutsmust be specified as part of adviceCan include wildcards15
AdviceThe code to be executed at a join pointThe join point must have been selected by a pointcutCan be defined as:BeforeAfterAround16
O-O Hello World17> javacMessageCommunicator.java Test.java> java testWanna learn AspectJHarry, having fun?
A-O Hello World18pointcutadvice> ajc MessageCommunicator.java MannersAspect.java Test.java> java TestHello! Wanna learn AspectJ?Hello! Harry, having fun?
A-O Hello World 219> ajc MessageCommunicator.java MannersAspect.java Hindi Salutation.java Test.java> java TestHello! Wanna learn AspectJ?Hello! Harry-ji, having fun?
Another aspect example20
How do we get a working program?21Source: Laddad, R. (2003)
Benefits of AOPCleaner responsibilities of the individual moduleHigher modularisationEasier system evolutionLate binding of design decisionsMore code reuseImprove time-to-marketReduced costs of feature implementation22
Realities of AOPProgram flow is hard to followDoesn’t solve any new problemsBreaks encapsulation23
Terms to rememberAdvicethe code implementing a concernAspectprogram abstraction defining a cross-cutting concern. Includes a definition of one or more pointcutsand the advice associated with that concernJoin pointan event in a program where the advice associated with an aspect may be executedJoin point modelset of events referenced in a pointcutPointcutaspect statement defining join points where the associated aspect advice should be executedWeavingincorporation of advice code at specified join points by an aspect weaver24
Useful SourcesBooksLaddad, R. (2003), AspectJ in Action, Manning Publications Co.Sommerville, I. (2007), Software Engineering, 8th edition, Pearson Education Ltd.OnlineThe AspectJ Project - http://www.eclipse.org/aspectj/VideoGoogleTechTalks (2007), Aspect Oriented Programming: Radical Research in Modularity. Available at: http://www.youtube.com/watch?v=cq7wpLI0hco25

Aspect Oriented Programming

  • 1.
  • 2.
    Coming up…What isAOP?Why do we need AOP?How does AOP work?Hello World2
  • 3.
    What is AOP?Partof AOSDAn extension of OOP extracting cross-cutting functional units of systemsA means of programming such functional units separate from other code, which are then ‘woven’ togetherAn attempt at promoting good SE practices within OOP3
  • 4.
    Why do weneed AOP?Situation in a typical O-O system:A single requirement implemented by multiple components (tangling)A single component may include elements of multiple requirements (scattering)Change could require understanding and changing many componentsThis impacts on software reuse4
  • 5.
    Tangling example –sharedbuffer5Tangled buffer management codeTangled synchronisation codeSource: Sommerville, I. (2007)
  • 6.
    Scattering example –patient records6Source: Sommerville, I. (2007)The highlighted operations implement secondary concerns for the system, such as keeping patient and consultant details anonymousIf the policy regarding anonymity changed, we would need to recode each version of anonymise() throughout the system
  • 7.
    The AOP approachThreekey stages:Aspectual DecomopositionConcern ImplementationAspectual Recomposition7
  • 8.
    How are concernsidentified?8A prismSource: Laddad, R. (2003)
  • 9.
    Concern TypesFunctionalQuality ofServicePolicySystemOrganisational9Functionalrelated to specific functionality to be included in the systemin a train control system, a specific functional concern is train brakingQuality of Servicerelated to the non-functional behaviour of a systemperformance, reliability, availabilityPolicyrelated to the overall policies that govern the use of the systemsecurity, safety, concerns related to business rulesSystemrelated to attributes of the system as a wholemaintainability, configurabilityOrganisationalrelated to organisational goals and prioritiesproducing a system within budget, making use of existing software assets, maintaining the reputation of an organisation
  • 10.
    Concern ClassificationsCoreFunctional concernsthat directly relate to the primary purpose of a system.SecondaryFunctionality that shares information with the core concernsFunctionality that satisfies NFRsCross-cuttingConcerns that apply to the system as a whole10
  • 11.
    Cross-cutting example11New customerreq.Customermanagement req.Account managementreq.Securityreq.Cross-cuttingconcernsRecoveryreq.Core concernsSource: Sommerville, I. (2007)
  • 12.
    Implementing ConcernsCoreClasses &OperationsCross-cutting / SecondaryAspectsAdvice; Join Points; Pointcuts12
  • 13.
    AspectAspects are similarto classes in that they can:include data members and operationshave access specificationsdeclare themselves to be abstractextend classes and abstract aspects and implement interfacesbe embedded inside classes and interfaces as nested aspectsThey are dissimilar in that they cannot:be directly instantiatedinherit from concrete aspectsbe marked as privileged13
  • 14.
    Join PointAny identifiableexecution point in a systemA call to a methodThe method’s executionAssignment to a variableReturn statementObject constructionConditional checkComparisonException handlerLoopsNot all may be exposed by each AOP languageAspectJ does not expose loops14
  • 15.
    PointcutPointcuts capture, oridentify, one or more join points&& ||Need not be given a nameAnonymous pointcutsmust be specified as part of adviceCan include wildcards15
  • 16.
    AdviceThe code tobe executed at a join pointThe join point must have been selected by a pointcutCan be defined as:BeforeAfterAround16
  • 17.
    O-O Hello World17>javacMessageCommunicator.java Test.java> java testWanna learn AspectJHarry, having fun?
  • 18.
    A-O Hello World18pointcutadvice>ajc MessageCommunicator.java MannersAspect.java Test.java> java TestHello! Wanna learn AspectJ?Hello! Harry, having fun?
  • 19.
    A-O Hello World219> ajc MessageCommunicator.java MannersAspect.java Hindi Salutation.java Test.java> java TestHello! Wanna learn AspectJ?Hello! Harry-ji, having fun?
  • 20.
  • 21.
    How do weget a working program?21Source: Laddad, R. (2003)
  • 22.
    Benefits of AOPCleanerresponsibilities of the individual moduleHigher modularisationEasier system evolutionLate binding of design decisionsMore code reuseImprove time-to-marketReduced costs of feature implementation22
  • 23.
    Realities of AOPProgramflow is hard to followDoesn’t solve any new problemsBreaks encapsulation23
  • 24.
    Terms to rememberAdvicethecode implementing a concernAspectprogram abstraction defining a cross-cutting concern. Includes a definition of one or more pointcutsand the advice associated with that concernJoin pointan event in a program where the advice associated with an aspect may be executedJoin point modelset of events referenced in a pointcutPointcutaspect statement defining join points where the associated aspect advice should be executedWeavingincorporation of advice code at specified join points by an aspect weaver24
  • 25.
    Useful SourcesBooksLaddad, R.(2003), AspectJ in Action, Manning Publications Co.Sommerville, I. (2007), Software Engineering, 8th edition, Pearson Education Ltd.OnlineThe AspectJ Project - http://www.eclipse.org/aspectj/VideoGoogleTechTalks (2007), Aspect Oriented Programming: Radical Research in Modularity. Available at: http://www.youtube.com/watch?v=cq7wpLI0hco25

Editor's Notes

  • #4 AOSD – Aspect Oriented Software DevelopmentSE – Software EngineeringOOP – Object-Oriented Programming
  • #6 Shared bufferAccessed by a number of components such as this oneLook at the first conditionalLook at notify()
  • #7 Introduce the classesBring attention to the lower operations – note common operation – anonymise()Good indication of scattering is common operation names
  • #8 Aspectual Decomposition – identification of individual concerns from reqsConcern Implementation – codingAspectual Recomposition – weaving of code into working system
  • #9 An analogy – a prism can be used to separate light into constituent coloursAspectual Decomposition is the identification of individual concerns from the system’s requirementsSo, if we call our prism a ‘Concern Identifier’ and, instead of light, we pass through the system’s requirements…We can see that it is possible to identify individual concernsNote that the concern identifier is specific to a particular system, depending heavily on the focus of the business
  • #10 Functional – related to specific functionality to be included in the systemExample– in a train control system, a specific functional concern is train brakingQuality of Service – related to the non-functional behaviour of a systemExamples– performance, reliability, availabilityPolicy – related to the overall policies that govern the use of the systemExamples– security, safety, concerns related to business rulesSystem – related to attributes of the system as a wholeExamples – maintainability, configurabilityOrganisational – related to organisational goals and prioritiesExamples – producing a system within budget, making use of existing software assets, maintaining the reputation of an organisation
  • #12 Lets take the example of an Internet banking systemThe system will have requirements relating to:New customers, such as credit checking / address verificationIt will also have reqs relating to the management of existing customers… As well as those relating to customer accountsAll of these will generate CORE concerns as they associate with the system’s primary focus – the provision of an Internet banking serviceHowever, the system also has security requirements based on the bank’s security policy…And recovery requirements to ensure that data is not lost in the event of a system failure…These generate cross-cutting concerns as they may influence the implementation of all the other system requirements
  • #19 Lets say we want to add ‘Hello! ‘ to the beginning of the response.We could just amend MessageCommunicator to this effect, but imagine having to sift through multiple files trying to find all the relevant execution codeA much simpler means of doing this is to use an aspect. We can accomplish this be defining a pointcut that registers any call to deliver, and also advice to be applied before the execution of deliver.Note the asterisk in the pointcutThis means that this pointcut will capture ANY call to MessageCommunicator.deliver regardless of access specifierEg Public / Private / Static / Return typeNote the use of ajc instead of javac – this is the AspectJ compilation command
  • #20 Now, say we want to include a language-appropriate salutation.In Hindi, the suffix ‘ji’ is often added to a person’s name to show respect, much like appending ‘san’ in Japanese. We can make this modification by defining another aspect that will pickup on the call to deliverNote the binary operator defining a second argument to the pointcut…This allows for the assignment of person, corresponding to the first argument.Note the use of String here. The second argument is not given a defining name as it is not required within this scope
  • #21 So lets take another example…Here we have an aspect called authentication that contains one piece of adviceNote that this advice will occur before the identified operation is executedNote also that, in this example, we have an anonymous piece of advice
  • #22 So how do we get a working program from these distinct concerns?This is where we use Aspectual Recomposition to combine themThis is accomplished by WEAVING the code into the final working systemThe WEAVER is a special type of compiler that is capable of this functionality
  • #23 So what are the reported benefits of AOP?AOP allows a module to take responsibility only for its core concern; a module is no longer liable for other cross-cutting concernsAOP provides a mechanism to address each concern separately with minimal coupling; resulting in a system with much less duplicated codeAOP modularises individual aspects and makes core modules oblivious to the aspects; adding new functionality is now a matter of including a new aspect and requires no change to the core modulesThe developer can delay making design decisions for future requirements because it is possible to implement those as separate aspectsCore modules aren’t aware of each other, only aspects are aware of any coupling in the implementationLate binding of design decisions allows for a much faster design cycleBy avoiding the cost of modifying many modules to implement a cross-cutting concern
  • #24 The programmer needs to be aware of any join points that relate to a particular piece of codeAOP is not attempting to provide solutions to unsolved programming problems, merely attempting to provide a means of going about those solved problems with less effort and improved maintainabilityIn OOP, a class encapsulates all the behaviour of an object. Aspects remove this level of control from the class