Download presentation
Presentation is loading. Please wait.
1
Aspect-Oriented Programming
2
Coming up… What is AOP? Why do we need AOP? How does AOP work?
Hello World
3
What is AOP? Part of AOSD An extension of OOP extracting cross- cutting functional units of systems A means of programming such functional units separate from other code, which are then ‘woven’ together An attempt at promoting good SE practices within OOP AOSD – Aspect Oriented Software Development SE – Software Engineering OOP – Object-Oriented Programming
4
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 components This impacts on software reuse
5
Tangling example – shared buffer
Tangled buffer management code Shared buffer Accessed by a number of components such as this one Look at the first conditional Look at notify() Tangled synchronisation code Source: Sommerville, I. (2007)
6
Scattering example – patient records
Source: Sommerville, I. (2007) The highlighted operations implement secondary concerns for the system, such as keeping patient and consultant details anonymous If the policy regarding anonymity changed, we would need to recode each version of anonymise() throughout the system Introduce the classes Bring attention to the lower operations – note common operation – anonymise() Good indication of scattering is common operation names
7
The AOP approach Three key stages: Aspectual Decomoposition
Concern Implementation Aspectual Recomposition Aspectual Decomposition – identification of individual concerns from reqs Concern Implementation – coding Aspectual Recomposition – weaving of code into working system
8
How are concerns identified?
A prism An analogy – a prism can be used to separate light into constituent colours Aspectual Decomposition is the identification of individual concerns from the system’s requirements So, 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 concerns Note that the concern identifier is specific to a particular system, depending heavily on the focus of the business Source: Laddad, R. (2003)
9
Concern Types Functional Functional Quality of Service Policy
System Organisational Functional related to specific functionality to be included in the system in a train control system, a specific functional concern is train braking Quality of Service related to the non-functional behaviour of a system performance, reliability, availability Policy related to the overall policies that govern the use of the system security, safety, concerns related to business rules System related to attributes of the system as a whole maintainability, configurability Functional – related to specific functionality to be included in the system Example – in a train control system, a specific functional concern is train braking Quality of Service – related to the non-functional behaviour of a system Examples – performance, reliability, availability Policy – related to the overall policies that govern the use of the system Examples – security, safety, concerns related to business rules System – related to attributes of the system as a whole Examples – maintainability, configurability Organisational – related to organisational goals and priorities Examples – producing a system within budget, making use of existing software assets, maintaining the reputation of an organisation Organisational related to organisational goals and priorities producing a system within budget, making use of existing software assets, maintaining the reputation of an organisation
10
Concern Classifications
Core Functional concerns that directly relate to the primary purpose of a system. Secondary Functionality that shares information with the core concerns Functionality that satisfies NFRs Cross-cutting Concerns that apply to the system as a whole
11
Cross-cutting example
New customer req. Customer management req. Account management req. Cross-cutting concerns Security req. Lets take the example of an Internet banking system The system will have requirements relating to: New customers, such as credit checking / address verification It will also have reqs relating to the management of existing customers… As well as those relating to customer accounts All of these will generate CORE concerns as they associate with the system’s primary focus – the provision of an Internet banking service However, 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 Recovery req. Core concerns Source: Sommerville, I. (2007)
12
Implementing Concerns
Core Classes & Operations Cross-cutting / Secondary Aspects Advice; Join Points; Pointcuts
13
Aspect Aspects are similar to classes in that they can:
include data members and operations have access specifications declare themselves to be abstract extend classes and abstract aspects and implement interfaces be embedded inside classes and interfaces as nested aspects They are dissimilar in that they cannot: be directly instantiated inherit from concrete aspects be marked as privileged
14
Join Point Any identifiable execution point in a system
A call to a method The method’s execution Assignment to a variable Return statement Object construction Conditional check Comparison Exception handler Loops Not all may be exposed by each AOP language AspectJ does not expose loops
15
Pointcut Pointcuts capture, or identify, one or more join points
&& || Need not be given a name Anonymous pointcuts must be specified as part of advice Can include wildcards
16
Advice The code to be executed at a join point
The join point must have been selected by a pointcut Can be defined as: Before After Around
17
O-O Hello World > javac MessageCommunicator.java Test.java
Wanna learn AspectJ Harry, having fun?
18
A-O Hello World pointcut
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 code A 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 pointcut This means that this pointcut will capture ANY call to MessageCommunicator.deliver regardless of access specifier Eg Public / Private / Static / Return type Note the use of ajc instead of javac – this is the AspectJ compilation command advice > ajc MessageCommunicator.java MannersAspect.java Test.java > java Test Hello! Wanna learn AspectJ? Hello! Harry, having fun?
19
A-O Hello World 2 > ajc MessageCommunicator.java MannersAspect.java Hindi Salutation.java Test.java > java Test Hello! Wanna learn AspectJ? Hello! Harry-ji, having fun? 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 deliver Note 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
20
Another aspect example
So lets take another example… Here we have an aspect called authentication that contains one piece of advice Note that this advice will occur before the identified operation is executed Note also that, in this example, we have an anonymous piece of advice
21
How do we get a working program?
So how do we get a working program from these distinct concerns? This is where we use Aspectual Recomposition to combine them This is accomplished by WEAVING the code into the final working system The WEAVER is a special type of compiler that is capable of this functionality Source: Laddad, R. (2003)
22
Benefits of AOP Cleaner responsibilities of the individual module
Higher modularisation Easier system evolution Late binding of design decisions More code reuse Improve time-to-market Reduced costs of feature implementation 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 concerns AOP provides a mechanism to address each concern separately with minimal coupling; resulting in a system with much less duplicated code AOP 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 modules The developer can delay making design decisions for future requirements because it is possible to implement those as separate aspects Core modules aren’t aware of each other, only aspects are aware of any coupling in the implementation Late binding of design decisions allows for a much faster design cycle By avoiding the cost of modifying many modules to implement a cross-cutting concern
23
Realities of AOP Program flow is hard to follow
Doesn’t solve any new problems Breaks encapsulation The programmer needs to be aware of any join points that relate to a particular piece of code AOP 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 maintainability In OOP, a class encapsulates all the behaviour of an object. Aspects remove this level of control from the class
24
Terms to remember Advice Aspect Join point Join point model Pointcut
the code implementing a concern Aspect program abstraction defining a cross-cutting concern. Includes a definition of one or more pointcuts and the advice associated with that concern Join point an event in a program where the advice associated with an aspect may be executed Join point model set of events referenced in a pointcut Pointcut aspect statement defining join points where the associated aspect advice should be executed Weaving incorporation of advice code at specified join points by an aspect weaver
25
Useful Sources Books Laddad, R. (2003), AspectJ in Action, Manning Publications Co. Sommerville, I. (2007), Software Engineering, 8th edition, Pearson Education Ltd. Online The AspectJ Project - Video GoogleTechTalks (2007), Aspect Oriented Programming: Radical Research in Modularity. Available at:
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.