Advance Software Engineering Aspect-Oriented Programming: By: Dr. Salwa Osama
Advance Software Engineering Aspect-Oriented Programming: By: Dr. Salwa Osama
Aspect-Oriented Programming
By:
Dr. Salwa Osama
OOP
Member Variables
Procedures
Log Message()
Burning Earning
LoyaltyProgram Customer
Privilege
1..*
0..* 0..* name: String
Member Variables
1..* partners title:String
enroll(c:Customer) program
ProgramPartner isMale: Boolean
Procedures 1 dateOfBirth: Date
numberOfCustomers: Integer 0..* Membership
age(): Integer
partner 1 {ordered} 1..* 1 actualLevel 1 1 owner
0..1
Logger
ServiceLevel 0..* card
LoyaltyAccount card
Member Variables CustomerCard
name: String 1
deliveredServices 0..* points: Integer
Procedures 1 level valid: Boolean
Service earn(i: Integer)
0..* validForm: Date
burn(i: Integer)
condition: Boolean goodThru: Date
availableServices isEmpty(): Boolean
pointsEarned: Integer color: enum{silver,
1 account
pointsBurned: Integer transactions 0..* gold}
DB
description: String printedName: String
Transaction
Member Variables
generatedBy 1 points: Integer card
0..* 1
Procedures
transactions
date:Date 0..*
Date program(): LoyaltyProgram transactions
$now: Date
Burning Earning
Cuts across multiple abstractions
Boilerplate code
Difficult to decompose
High-coupling
Problems
• Poor traceability – multiple concerns in the
same module breaks linkage between
requirement and its implementation, making
it harder to understand what a piece of code
is doing to address the problem domain.
• Lower productivity – developers are
Code tangling spending too much time and attention to
peripheral issues rather than the problem
domain.
• Less code reuse – boilerplate code
propagated through cut and paste… no code
reuse here!
• Harder refactoring – changing requirements
means touching many modules for a single
Code scattering
concern.
Aspect Oriented Programming (AOP)
Concerns
• Concerns are not program issues but reflect the system requirements and the
priorities of the system stakeholders.
• Examples of concerns are performance, security, specific functionality, etc.
• By reflecting the separation of concerns in a program, there is clear traceability
from requirements to implementation.
• Core concerns are the functional concerns that relate to the primary purpose of a
system; secondary concerns are functional concerns that reflect non-functional
requirements.
Stakeholder concerns
• Functional concerns which are related to specific functionality to be included in a
system.
• Quality of service concerns which are related to the non-functional behaviour of
a system.
• Policy concerns which are related to the overall policies that govern the use of
the system.
• System concerns which are related to attributes of the system as a whole such as
its maintainability or its configurability.
• Organisational concerns which are related to organisational goals and priorities
such as producing a system within budget, making use of existing software assets
or maintaining the reputation of an organisation.
Cross-cutting Concerns
AOP
Class A Class B Class C
More maintainable
Aspect
• It is a class that implements enterprise application concerns that cut across multiple
classes such as transaction management.
• Aspects can be a normal class configuration through the spring XML configuration or we
can use spring AspectJ integration to define class as Aspect using @aspect annotation
AOP
Terminologies
• Join point:
• It is a specific point in the application such as method execution, exception
handling, changing object variable values. In Spring AOP, a join point is
always the execution of the method.
AOP
Terminologies
• Advice
• It are actions taken for a particular joint point
AOP Advice Types
• Before Advice: These advices runs before the execution of join point methods. We can use
@Before annotation to mark an advice type as Before advice.
• After (finally) Advice: An advice that gets executed after the join point method finishes executing,
whether normally or by throwing an exception. We can create after advice using @After
annotation.
• After Returning Advice: Sometimes we want advice methods to execute only if the join point
method executes normally. We can use @AfterReturning annotation to mark a method as after
returning advice.
• After Throwing Advice: This advice gets executed only when join point method throws exception,
we can use it to rollback the transaction declaratively. We use @AfterThrowing annotation for this
type of advice.
• Around Advice: This is the most important and powerful advice. This advice surrounds the join
point method and we can also choose whether to execute the join point method or not. We can
write advice code that gets executed before and after the execution of the join point method. It is
the responsibility of around advice to invoke the join point method and return values if the
method is returning something. We use @Around annotation to create around advice methods.
AOP Advice Types
AOP • @Pointcut("execution(* com.tutorialspoint.*.*(..))")
• @Pointcut("execution(* com.tutorialspoint.Student.getName(..))")
Terminologies
• Point cut
• It is expressions that are matched with joint point to determine whether advice needed to be
executed or not
• Identifies the specific events with which advice should be associated.
• AOP is the ability to multicast the same aspects into many places in your code, this feature is called
Point Cut
Point Cut
AOP • @Pointcut("execution(* com.tutorialspoint.*.*(..))")
• @Pointcut("execution(* com.tutorialspoint.Student.getName(..))")
Terminologies
• Weaving
• Combines advices with point cuts
• The incorporation of advice code at the specified join points by an aspect
weaver.
Aspect Weaving
AOP Terminologies