Aspect Oriented Programming

Download Report

Transcript Aspect Oriented Programming

Introduction to
Aspect Oriented Programming
Presented By:
Kotaiah Choudary. Ravipati
M.Tech IInd Year.
School of Info. Tech.
Contents
•
•
•
•
Introduction
AOP Methodology
AOP Languages
AspectJ
19 August 2005
Aspect Oriented Programming
2
Introduction
Programming Methodologies
Concern
Abstraction
AO Programming
Function
Abstractio
n
OO Programming
Object
Abstraction
Procedural Programming
Assembly Programming
m/c Programming
19 August 2005
Programming Methodologies
Evaluation
Aspect Oriented Programming
3
Introduction
Present Design Problem
• The architect’s dilemma
– How much design is too much?
19 August 2005
Aspect Oriented Programming
4
Introduction
Terminology
• Concern
– A specific requirement or consideration that must be
addressed in order to satisfy the overall system goal
– E.g. Banking system:
Customer and Account management, Interbanking
transactions, ATM transactions,
Persistence of all entities, Transaction integrity,
Authorization of access to various services, Logging,
Security, Error checking, Policy enforcement
19 August 2005
Aspect Oriented Programming
5
Introduction
Terminology
cont
• Core concerns
– Central functionality of a Business logic (single
module)
– Customer and Account management, Interbanking
transactions, ATM transactions
• Crosscut concerns
– System-level, peripheral requirements (multiple
modules)
– Persistence of all entities, Transaction integrity,
Authorization of access to various services, logging,
Security, Error checking, Policy enforcement
19 August 2005
Aspect Oriented Programming
6
Introduction
Typical Application
Business Logic
Concern
Accounting
ATM
Logging
Persistence
Database
Logging
Persistence
Implementation
modules
System
19 August 2005
Aspect Oriented Programming
7
Introduction
Implementation by Traditional
Languages
API
invocations
Accounting Module
ATM Module
Logging Module
Database Module
19 August 2005
Aspect Oriented Programming
8
Introduction
Implementation by AOP
Automatically Weaving
Invocations
Accounting Module
API
Invocation
ATM Module
Logging Aspect
Logging Module
Database Module
Aspect: A modular unit of crosscutting concern
implementation
19 August 2005
Aspect Oriented Programming
9
AOP Methodology
AOP Methodology
• The idea behind AOP is “Separation of
concern”
• AOP builds upon Existing Methodologies
(OOP, Procedural programming),
augmenting them with concepts and
constructs in order to modularize
crosscutting concerns.
• Now, concern consists of what?
19 August 2005
Aspect Oriented Programming
10
AOP Methodology
AOP Development Stages
Security
Business
Logic
Identify
concerns
Integrating all
concerns
Logging
Persistence
19 August 2005
Aspect Oriented Programming
11
AOP Languages
AOP Languages
• AOP Language Specification
– Describes the language constructs and
syntax
– Specification for implementing the
individual concerns and rules for weaving
different concerns
19 August 2005
Aspect Oriented Programming
12
AOP Languages
AOP Languages
cont
• AOP language implementation
– Verifies the code and translates the code into
an executable form
– Implementation done by two steps
• Combining individual concerns using the weaving
rules - Weaving
• Then converting result into executable code.
• Some AOP Languages
– AspectJ, Aspect#, AspectC++, PHPaspect.
19 August 2005
Aspect Oriented Programming
13
AspectJ
AspectJ
• Aspect-oriented extension to Java language
• Language Specification
• Language Implementation
– Concern by Java Language
– Weaving rules by extension Tools
• Crosscutting in AspectJ
– Implementation of the weaving rules by the compiler
– 2 Types:
• Dynamic Crosscutting
– Weaving of new Behavior into execution of program
• Static Crosscutting
– weaving of modifications into the static structure, to support the
Dynamic Crosscutting
19 August 2005
Aspect Oriented Programming
14
AspectJ
AspectJ
cont
• Crosscutting Elements
– Join Point
• Well-defined points in the execution of a program
– Pointcut
• A means of referring to collections of join points and certain values at
those join points
– Advice
• Method-like constructs to define additional behavior at join points
– Introduction
• Instruction that changes to the classes, interfaces of the system
– Compile time Declaration
• Instruction for adding compile time warnings and errors to the system
– Aspect
• Unit of modular crosscutting implementation
19 August 2005
Aspect Oriented Programming
15
AspectJ
Example
Public class Logging{//class
public void check( ) {
System.out.println("Inside Logging Class Method"); }}
public class ATM { //class
public void foo(int number, String name) {
System.out.println("Inside foo Method"); }
public static void main(String[] args) {
MyClass myObject = new MyClass( );
myObject.foo(1, “SIT"); } }
Name of
Pointcut
public aspect Log { //Aspect
pointcut callPointcut( ) : call(void ATM.foo(int, String)); //Pointcut
before( ) : callPointcut( ) { //Advice
System.out.println( “I am from Log aspect");
before
Logging logg= new Logging();
after
logg.check(); } }
around
19 August 2005
Aspect Oriented Programming
16
AspectJ
Example
cont
• Compile
–
–
ajc ATM.java Log. java
output:
• ATM.class Log.class
• Run
– java ATM
– Output:
• I am from Log aspect
Inside foo Method
Here let us assume Logging class already compiled
19 August 2005
Aspect Oriented Programming
17
References
• AspectJ Cookbook , O'Reilly, December 2004
• Aspectj In Action - Practical Aspect-Oriented Programming,
2003
• http://www.codeproject.com/gen/design/aop.asp#2
• http://en.wikipedia.org/wiki/Aspect_oriented_programming
• Erik Putrycz, Guy Bernard, IEEE 2002, Using Aspect
Oriented Programming to build a portable load balancing
service
19 August 2005
Aspect Oriented Programming
18