Aspect-Oriented Programming

25
ASPECT-ORIENTED PROGRAMMING

description

Aspect-Oriented Programming. Coming up…. What is AOP? Why do we need AOP? How does AOP work? Hello World. What is AOP?. Part of AOSD An extension of OOP extracting cross-cutting functional units of systems - PowerPoint PPT Presentation

Transcript of Aspect-Oriented Programming

Page 1: Aspect-Oriented Programming

ASPECT-ORIENTED PROGRAMMING

Page 2: Aspect-Oriented Programming

2

Coming up…

What is AOP?

Why do we need AOP?

How does AOP work?

Hello World

Page 3: Aspect-Oriented Programming

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

Page 4: Aspect-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

Page 5: Aspect-Oriented Programming

5

Tangling example – shared buffer

Tangled buffer management code

Tangled synchronisation codeSource: Sommerville, I. (2007)

Page 6: Aspect-Oriented Programming

6

Scattering example – patient records

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

Source: Sommerville, I. (2007)

Page 7: Aspect-Oriented Programming

7

The AOP approach

Three key stages:

Aspectual Decomoposition

Concern Implementation

Aspectual Recomposition

Page 8: Aspect-Oriented Programming

8

How are concerns identified?

Source: Laddad, R. (2003)

A prism

Page 9: Aspect-Oriented Programming

9

Concern Types

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 Organisational related to organisational goals and priorities producing a system within budget, making use of existing

software assets, maintaining the reputation of an organisation

Page 10: Aspect-Oriented Programming

10

Concern Classifications

CoreFunctional concerns that directly relate to the

primary purpose of a system.

SecondaryFunctionality that shares information with the core

concernsFunctionality that satisfies NFRs

Cross-cuttingConcerns that apply to the system as a whole

Page 11: Aspect-Oriented Programming

11

Cross-cutting example

New customerreq.

Customer management

req.

Account management

req.

Securityreq.

Recoveryreq.

Cross-cuttingconcerns

Core concerns

Source: Sommerville, I. (2007)

Page 12: Aspect-Oriented Programming

12

Implementing Concerns

CoreClasses & Operations

Cross-cutting / SecondaryAspects

○ Advice; Join Points; Pointcuts

Page 13: Aspect-Oriented Programming

13

Aspect

Aspects 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

aspects

They are dissimilar in that they cannot:be directly instantiated inherit from concrete aspectsbe marked as privileged

Page 14: Aspect-Oriented Programming

14

Join Point

Any identifiable execution point in a systemA call to a methodThe method’s executionAssignment to a variableReturn statementObject constructionConditional checkComparisonException handlerLoops

Not all may be exposed by each AOP languageAspectJ does not expose loops

Page 15: Aspect-Oriented Programming

15

Pointcut

Pointcuts capture, or identify, one or more join points&& ||

Need not be given a nameAnonymous pointcuts must be specified as

part of advice

Can include wildcards

Page 16: Aspect-Oriented Programming

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

Page 17: Aspect-Oriented Programming

17

O-O Hello World

> javac MessageCommunicator.java Test.java> java testWanna learn AspectJHarry, having fun?

Page 18: Aspect-Oriented Programming

18

A-O Hello World

> ajc MessageCommunicator.java MannersAspect.java Test.java> java TestHello! Wanna learn AspectJ?Hello! Harry, having fun?

pointcut

advice

Page 19: Aspect-Oriented Programming

19

A-O Hello World 2

> ajc MessageCommunicator.java MannersAspect.java Hindi Salutation.java Test.java> java TestHello! Wanna learn AspectJ?Hello! Harry-ji, having fun?

Page 20: Aspect-Oriented Programming

20

Another aspect example

Page 21: Aspect-Oriented Programming

21

How do we get a working program?

Source: Laddad, R. (2003)

Page 22: Aspect-Oriented Programming

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

Page 23: Aspect-Oriented Programming

23

Realities of AOP

Program flow is hard to follow

Doesn’t solve any new problems

Breaks encapsulation

Page 24: Aspect-Oriented Programming

24

Terms to remember Advice

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

Page 25: Aspect-Oriented Programming

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 - http://www.eclipse.org/aspectj/

Video GoogleTechTalks (2007), Aspect Oriented Programming:

Radical Research in Modularity. Available at: http://www.youtube.com/watch?v=cq7wpLI0hco