Top Banner
Introduction to Aspect Oriented Programming (AOP)
24

Introduction to Aspect Oriented Programming (AOP) · AOP Goal separation of cross-cutting concerns – organization leading to each component doing one thing only Cross-cutting concern

Jul 15, 2018

Download

Documents

dinhque
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction to Aspect Oriented Programming (AOP) · AOP Goal separation of cross-cutting concerns – organization leading to each component doing one thing only Cross-cutting concern

Introduction to Aspect Oriented Programming (AOP)

Page 2: Introduction to Aspect Oriented Programming (AOP) · AOP Goal separation of cross-cutting concerns – organization leading to each component doing one thing only Cross-cutting concern

AOP

Goal● separation of cross-cutting concerns

– organization leading to each component doing one thing only

Cross-cutting concern

Core system concerns (functional requirements)

security

logging

fault-tolerance

usermgmt

accountmgmt

HR

Page 3: Introduction to Aspect Oriented Programming (AOP) · AOP Goal separation of cross-cutting concerns – organization leading to each component doing one thing only Cross-cutting concern

Cross­cutting Concerns

[1] http://en.wikipedia.org/wiki/Aspect-oriented_software_development#Examples_of_crosscutting_concerns

Error detection

Security

Caching

Logging

Monitoring

Validation

Business rules

i18N

Persistence

Transactions

Page 4: Introduction to Aspect Oriented Programming (AOP) · AOP Goal separation of cross-cutting concerns – organization leading to each component doing one thing only Cross-cutting concern

Motivation

Tangling● occurs if the implementation (code) of multiple concerns

is intermixed in a single module

Page 5: Introduction to Aspect Oriented Programming (AOP) · AOP Goal separation of cross-cutting concerns – organization leading to each component doing one thing only Cross-cutting concern

Tangling Examplesynchronized void put (SensorRecord rec ) {// Check that there is space in the buffer; wait if not

if ( numberOfEntries == bufsize)wait () ;

// Add record at end of buffer

// If at end of buffer, next entry is at the beginning

// indicate that buffer is available

notify () ;

}

Page 6: Introduction to Aspect Oriented Programming (AOP) · AOP Goal separation of cross-cutting concerns – organization leading to each component doing one thing only Cross-cutting concern

Motivation

Scattering● occurs if the implementation of a single concern is spread

over multiple modules

Page 7: Introduction to Aspect Oriented Programming (AOP) · AOP Goal separation of cross-cutting concerns – organization leading to each component doing one thing only Cross-cutting concern

Scattering Example

public void service1 ( ) {

try{

// service1 code

} catch (Exception e) {handleException(e);

}

}

public void service2 ( ) {

try{

// service2 code

} catch (Exception e) {handleException(e);

}

}

Page 8: Introduction to Aspect Oriented Programming (AOP) · AOP Goal separation of cross-cutting concerns – organization leading to each component doing one thing only Cross-cutting concern

Core Concepts

Callee.someMethod()

public void someMethod(){ ...}

Join Point Advice

bind to(expressed in pointcut)

Caller

Callee

Page 9: Introduction to Aspect Oriented Programming (AOP) · AOP Goal separation of cross-cutting concerns – organization leading to each component doing one thing only Cross-cutting concern

Core Conceptsadvice● the code implementing a concern

– additional code that should be applied to existing core functionality

– e.g. ● logging● security

Page 10: Introduction to Aspect Oriented Programming (AOP) · AOP Goal separation of cross-cutting concerns – organization leading to each component doing one thing only Cross-cutting concern

Core Conceptsjoin point● an event in an executing program where the advice

associated with the aspect may be executed

join point model● many possible types of events

– call events – calls to a method– execution events – the execution of a method– initialization events – class or object initialization– data events – accessing or updating fields– exception events

Page 11: Introduction to Aspect Oriented Programming (AOP) · AOP Goal separation of cross-cutting concerns – organization leading to each component doing one thing only Cross-cutting concern

Core Conceptspointcut● defines the join point where the associated advice should

be executed– events are associated with particular items

● examples– before the execution of all update methods– after a normal or exceptional return from a method– when the name field is changed

Page 12: Introduction to Aspect Oriented Programming (AOP) · AOP Goal separation of cross-cutting concerns – organization leading to each component doing one thing only Cross-cutting concern

Core Conceptsaspect● A program abstraction that defines a cross-cutting

concern. it includes – the definition of a pointcut– the advice associated with that concern

Logging Aspect

...updateDetails(...)...

Patient

Security Aspect

AspectWeaver

...authenticationupdateDetails(...)logging code...

Patient

Page 13: Introduction to Aspect Oriented Programming (AOP) · AOP Goal separation of cross-cutting concerns – organization leading to each component doing one thing only Cross-cutting concern

Core Conceptsweaver● responsible for inclusion of advice at the join points

specified in the pointcuts● three types of weaving

– source code preprocessing● code to code translation first, then compilation

– link time● most common

– dynamic● join points are monitored and corresponding advices are integrated● performance penalties

Page 14: Introduction to Aspect Oriented Programming (AOP) · AOP Goal separation of cross-cutting concerns – organization leading to each component doing one thing only Cross-cutting concern

Interfaces??Conventional design● a cross cutting concern can be modularized using

interfaces– decouples the implementation– example: Log4J

● however, the client code still needs to embed interface code

Page 15: Introduction to Aspect Oriented Programming (AOP) · AOP Goal separation of cross-cutting concerns – organization leading to each component doing one thing only Cross-cutting concern

Example

Page 16: Introduction to Aspect Oriented Programming (AOP) · AOP Goal separation of cross-cutting concerns – organization leading to each component doing one thing only Cross-cutting concern

Aspect Oriented DesignIntent● design process that uses aspects

Identification● start with use case diagrams● look for common features

Design● the outcome of AOD process is an AOD model

Page 17: Introduction to Aspect Oriented Programming (AOP) · AOP Goal separation of cross-cutting concerns – organization leading to each component doing one thing only Cross-cutting concern

Example of AOD model

Inventory

Equipment

Store

Location

Log

Platform

DB

«aspect»Monitor

«aspect»Ordering

«aspect»Availability

«aspect»Maintenance

«joinpoint»Platform «joinpoint»

EquipmentLocation

Page 18: Introduction to Aspect Oriented Programming (AOP) · AOP Goal separation of cross-cutting concerns – organization leading to each component doing one thing only Cross-cutting concern

Example of AOD model

PointcutsviewMain = call getItemInfo(...)mainco = call removeItemInfo(...)mainci = call addItem(...)

«aspect»Maintenance

Class Extensions

<viewItem> {after (<viewMain>)displayHistory}

ViewMaintenance History

In the method viewItem, after the call to the method getItemInfo, a call to the method displayHistory should be included to display the maintenance record.

Page 19: Introduction to Aspect Oriented Programming (AOP) · AOP Goal separation of cross-cutting concerns – organization leading to each component doing one thing only Cross-cutting concern

ChallengesTesting● unit testing – easy● woven code – not so easy

– depends on the combination of aspects● e.g. weave order

Tight coupling● between main code (concern) and aspect code● correctness

– correctly specifying pointcuts is important● matching

– like regex

Page 20: Introduction to Aspect Oriented Programming (AOP) · AOP Goal separation of cross-cutting concerns – organization leading to each component doing one thing only Cross-cutting concern

ChallengesEvolution● FACT: code evolves/changes with time● aspects must also change correspondingly

Page 21: Introduction to Aspect Oriented Programming (AOP) · AOP Goal separation of cross-cutting concerns – organization leading to each component doing one thing only Cross-cutting concern

Design Patterns & AOPSingleton Pattern● crosscutting concerns:

– object creation– count management

Page 22: Introduction to Aspect Oriented Programming (AOP) · AOP Goal separation of cross-cutting concerns – organization leading to each component doing one thing only Cross-cutting concern

Design Patterns & AOP

Observer● crosscutting concerns

– observer management– notification

Page 23: Introduction to Aspect Oriented Programming (AOP) · AOP Goal separation of cross-cutting concerns – organization leading to each component doing one thing only Cross-cutting concern

Design Patterns & AOP

Proxy● Cross-cutting concern

– DoAction()

Page 24: Introduction to Aspect Oriented Programming (AOP) · AOP Goal separation of cross-cutting concerns – organization leading to each component doing one thing only Cross-cutting concern

Design Patterns & AOPHow is AOP different from● proxy design pattern● decorator design pattern