Aspect Oriented Programming Scott Nykl CSSE 411 Senior Seminar.

28
Aspect Oriented Aspect Oriented Programming Programming Scott Nykl Scott Nykl CSSE 411 Senior Seminar CSSE 411 Senior Seminar

Transcript of Aspect Oriented Programming Scott Nykl CSSE 411 Senior Seminar.

Page 1: Aspect Oriented Programming Scott Nykl CSSE 411 Senior Seminar.

Aspect Oriented Aspect Oriented ProgrammingProgramming

Scott NyklScott Nykl

CSSE 411 Senior SeminarCSSE 411 Senior Seminar

Page 2: Aspect Oriented Programming Scott Nykl CSSE 411 Senior Seminar.

OverviewOverview

Motivation for Aspect Oriented Motivation for Aspect Oriented ProgrammingProgramming

What is Aspect Oriented Programming?What is Aspect Oriented Programming?

How Does Aspect Oriented Programming How Does Aspect Oriented Programming Work?Work?

Case StudyCase Study

Page 3: Aspect Oriented Programming Scott Nykl CSSE 411 Senior Seminar.

Motivation for Aspect Oriented Motivation for Aspect Oriented ProgrammingProgramming

First Computer ProgramsFirst Computer Programs Consisted of cryptic binary stringsConsisted of cryptic binary strings

Page 4: Aspect Oriented Programming Scott Nykl CSSE 411 Senior Seminar.

Motivation for Aspect Oriented Motivation for Aspect Oriented ProgrammingProgramming

Next GenerationNext Generation ““Higher-Level” AssemblyHigher-Level” Assembly

Page 5: Aspect Oriented Programming Scott Nykl CSSE 411 Senior Seminar.

Motivation for Aspect Oriented Motivation for Aspect Oriented ProgrammingProgramming

1957: Birth of Real “High-Level” Language1957: Birth of Real “High-Level” Language IBM’s FortranIBM’s Fortran

Division created between a computer’s hardware Division created between a computer’s hardware and the coupling that hardware imposed upon the and the coupling that hardware imposed upon the software.software.

Subprograms, or subroutinesSubprograms, or subroutines Modular code blocks allowed decomposition into smaller Modular code blocks allowed decomposition into smaller

piecespieces Increased reusability and maintainability of codeIncreased reusability and maintainability of code

Page 6: Aspect Oriented Programming Scott Nykl CSSE 411 Senior Seminar.

Motivation for Aspect Oriented Motivation for Aspect Oriented ProgrammingProgramming

Late 1960s: Object Oriented ParadigmLate 1960s: Object Oriented Paradigm Collection of related data and operations Collection of related data and operations

useful to that datauseful to that data Paved way for Modern C++, Java, C#, etc.Paved way for Modern C++, Java, C#, etc.

Page 7: Aspect Oriented Programming Scott Nykl CSSE 411 Senior Seminar.

Motivation for Aspect Oriented Motivation for Aspect Oriented ProgrammingProgramming

Problems with OOProblems with OO Although specific groups of data and Although specific groups of data and

operations are cohesively grouped together, operations are cohesively grouped together, other secondary operations still need to be other secondary operations still need to be duplicated across these objects.duplicated across these objects.

High-level, system-wide requirementsHigh-level, system-wide requirementsApplication logging Application logging

Security requirements Security requirements

Other Policy Layer RequirementsOther Policy Layer Requirements

Page 8: Aspect Oriented Programming Scott Nykl CSSE 411 Senior Seminar.

Motivation for Aspect Oriented Motivation for Aspect Oriented ProgrammingProgramming

General Application-wide requirements still General Application-wide requirements still need to be supported via these objects but need to be supported via these objects but are not explicitly encapsulated in one are not explicitly encapsulated in one single object typesingle object type

Page 9: Aspect Oriented Programming Scott Nykl CSSE 411 Senior Seminar.

Motivation for Aspect Oriented Motivation for Aspect Oriented ProgrammingProgramming

Assume following scenarioAssume following scenario Employee record systemEmployee record system

Collection of employee related objectsCollection of employee related objects

Each object has cohesive set of operations that Each object has cohesive set of operations that perform necessary manipulations on the data perform necessary manipulations on the data contained within that objectcontained within that object

Page 10: Aspect Oriented Programming Scott Nykl CSSE 411 Senior Seminar.

Motivation for Aspect Oriented Motivation for Aspect Oriented ProgrammingProgramming

Employee Record SystemEmployee Record System Now imagine that the system is extended to Now imagine that the system is extended to

support logging each employee change to a support logging each employee change to a logging database.logging database.

Page 11: Aspect Oriented Programming Scott Nykl CSSE 411 Senior Seminar.

Motivation for Aspect Oriented Motivation for Aspect Oriented ProgrammingProgramming

OO ApproachOO ApproachDevelopers will have to Developers will have to Dive inside the employee objectsDive inside the employee objects

Add additional operations to support loggingAdd additional operations to support loggingWading through existing code and finding the exact locations Wading through existing code and finding the exact locations to insert logging operations can be a time-consuming, to insert logging operations can be a time-consuming, tedious, and error-prone process which can lead to software tedious, and error-prone process which can lead to software instability and security flaws.instability and security flaws.

Writing to a database, or ensuring sufficient Writing to a database, or ensuring sufficient privileges are obtained does not coherently fit privileges are obtained does not coherently fit with what employee objects represent – BAD with what employee objects represent – BAD COHESION (Dr. Rowe = Angry) COHESION (Dr. Rowe = Angry)

Page 12: Aspect Oriented Programming Scott Nykl CSSE 411 Senior Seminar.

What Is Aspect Oriented What Is Aspect Oriented Programming?Programming?

Solution = Aspect Oriented Approach Solution = Aspect Oriented Approach We want to add new functionality in a We want to add new functionality in a single location and simply specify that this single location and simply specify that this new functionality is to be invoked when new functionality is to be invoked when certain conditions arise. certain conditions arise. Added functionality is achieved without Added functionality is achieved without wading through existing code and wading through existing code and appending duplicate functionality across appending duplicate functionality across existing operations. existing operations.

Page 13: Aspect Oriented Programming Scott Nykl CSSE 411 Senior Seminar.

What Is Aspect Oriented What Is Aspect Oriented Programming?Programming?

Employee Record SystemEmployee Record System We will be able to create one central We will be able to create one central

collection of operations responsible for collection of operations responsible for logging to a databaselogging to a database

Specify that a database write shall be invoked Specify that a database write shall be invoked when an employee object is modified.when an employee object is modified.

Page 14: Aspect Oriented Programming Scott Nykl CSSE 411 Senior Seminar.

How Does Aspect Oriented How Does Aspect Oriented Programming Work?Programming Work?

Cross-Cutting Concern: Some aspects of Cross-Cutting Concern: Some aspects of implementation, such as logging, error implementation, such as logging, error handling, standards enforcement, and handling, standards enforcement, and policy-layer modification are difficult to policy-layer modification are difficult to implement in a modular way. The result is implement in a modular way. The result is that code is tangled across a system; this that code is tangled across a system; this leads to quality, productivity, and leads to quality, productivity, and maintenance issues. These issues are maintenance issues. These issues are cross-cutting concerns cross-cutting concerns

Page 15: Aspect Oriented Programming Scott Nykl CSSE 411 Senior Seminar.

How Does Aspect Oriented How Does Aspect Oriented Programming Work?Programming Work?

Advice: This is the additional code that Advice: This is the additional code that one wants to apply to an existing model to one wants to apply to an existing model to add support for features such as logging, add support for features such as logging, error handling, standards enforcement, error handling, standards enforcement, and policy-layer modification and policy-layer modification

Page 16: Aspect Oriented Programming Scott Nykl CSSE 411 Senior Seminar.

How Does Aspect Oriented How Does Aspect Oriented Programming Work?Programming Work?

Point Cut: This is the term given to the point of Point Cut: This is the term given to the point of execution in the application at which a cross-execution in the application at which a cross-cutting concern needs to be applied. In our cutting concern needs to be applied. In our example, a point-cut is reached when a thread of example, a point-cut is reached when a thread of execution within the employee record system execution within the employee record system enters a method responsible for modifying an enters a method responsible for modifying an employee; a second point-cut is reached when employee; a second point-cut is reached when that thread of execution leaves the method that thread of execution leaves the method responsible for modifying an employee responsible for modifying an employee

Page 17: Aspect Oriented Programming Scott Nykl CSSE 411 Senior Seminar.

How Does Aspect Oriented How Does Aspect Oriented Programming Work?Programming Work?

Aspect: The combination of the point-cut Aspect: The combination of the point-cut and the advice is termed an aspect. In our and the advice is termed an aspect. In our example, we add a logging aspect to our example, we add a logging aspect to our employee record system by defining a employee record system by defining a point-cut and giving the correct advice to point-cut and giving the correct advice to execute when that point-cut is reached execute when that point-cut is reached

+ =

Page 18: Aspect Oriented Programming Scott Nykl CSSE 411 Senior Seminar.

aspect vs class

aspect specific data and operations are “static”aspect is a singleton pointcut declaration

advice

Page 19: Aspect Oriented Programming Scott Nykl CSSE 411 Senior Seminar.

How Does Aspect Oriented How Does Aspect Oriented Programming Work?Programming Work?

Point-cut named “LogEmployeeChange” Point-cut named “LogEmployeeChange” Specify specific points where employee object changedSpecify specific points where employee object changed Deals only with objects of type “Employee” (line 13) Deals only with objects of type “Employee” (line 13) Deals specifically with 4 methods contained within the Deals specifically with 4 methods contained within the

“Employee” class (lines 14-17)“Employee” class (lines 14-17) Line 14 explicitly says, “for any ‘public’ method of class Line 14 explicitly says, “for any ‘public’ method of class

‘Employee’ with any type of return parameter (‘*’), a ‘Employee’ with any type of return parameter (‘*’), a name of ‘setName’, and a parameter set of anything (‘..’), name of ‘setName’, and a parameter set of anything (‘..’), execute the corresponding Adviceexecute the corresponding Advice

Note: use of wildcards allow the aspect programmer to Note: use of wildcards allow the aspect programmer to include a large grouping of functions in a single line. include a large grouping of functions in a single line.

Page 20: Aspect Oriented Programming Scott Nykl CSSE 411 Senior Seminar.

How Does Aspect Oriented How Does Aspect Oriented Programming Work?Programming Work?

before Advicebefore Advice Line 19 says, “Since my associated point-cut has been Line 19 says, “Since my associated point-cut has been

triggered, execute the code in my block before executing triggered, execute the code in my block before executing the method which triggered the point-cut.”the method which triggered the point-cut.”

The actual instance of the Employee class which will be The actual instance of the Employee class which will be modified is passed into the before Advice as “Employee modified is passed into the before Advice as “Employee e”; the advice can modify Employee e.e”; the advice can modify Employee e.

Page 21: Aspect Oriented Programming Scott Nykl CSSE 411 Senior Seminar.

How Does Aspect Oriented How Does Aspect Oriented Programming Work?Programming Work?

After executing before Advice, the method which After executing before Advice, the method which triggered the point-cut is executed; this method triggered the point-cut is executed; this method could be “setName”, “setAddress”, etcetera.could be “setName”, “setAddress”, etcetera.

Once execution returns from this method, the after Once execution returns from this method, the after Advice is considered. Advice is considered.

Page 22: Aspect Oriented Programming Scott Nykl CSSE 411 Senior Seminar.

Case Study: Apache TomcatCase Study: Apache Tomcat

Tomcat: open-source implementation of Tomcat: open-source implementation of Java Servlet and JavaServer Pages Java Servlet and JavaServer Pages developed at the Apache Software developed at the Apache Software Foundation.Foundation. Object-Oriented Object-Oriented

Page 23: Aspect Oriented Programming Scott Nykl CSSE 411 Senior Seminar.

Case Study: Apache TomcatCase Study: Apache Tomcat

Red Shows encapsulated XML Parser in TomcatRed Shows encapsulated XML Parser in Tomcat

Page 24: Aspect Oriented Programming Scott Nykl CSSE 411 Senior Seminar.

Case Study: Apache TomcatCase Study: Apache Tomcat

Red Shows encapsulated URL Pattern Matching in Red Shows encapsulated URL Pattern Matching in TomcatTomcat

Page 25: Aspect Oriented Programming Scott Nykl CSSE 411 Senior Seminar.

Case Study: Apache TomcatCase Study: Apache Tomcat

Red Shows Tomcat LoggingRed Shows Tomcat Logging

A single logging aspect can replace this tangleA single logging aspect can replace this tangle Increase CohesionIncrease Cohesion Decrease CouplingDecrease Coupling Increase MaintainabilityIncrease Maintainability No duplicated logging code across multiple classesNo duplicated logging code across multiple classes

Page 26: Aspect Oriented Programming Scott Nykl CSSE 411 Senior Seminar.

SummarySummary

AspectJ and Aspect-Oriented AspectJ and Aspect-Oriented Programming represent a way to cleanly Programming represent a way to cleanly modularize high-level requirements which modularize high-level requirements which span across the scope of many different span across the scope of many different classes and operations. Using AspectJ classes and operations. Using AspectJ can greatly alleviate issues related to can greatly alleviate issues related to logging, monitoring, application-spanning logging, monitoring, application-spanning requirements, and many other issues that requirements, and many other issues that commonly cause headaches for even well-commonly cause headaches for even well-designed Object-Oriented systems designed Object-Oriented systems

Page 27: Aspect Oriented Programming Scott Nykl CSSE 411 Senior Seminar.
Page 28: Aspect Oriented Programming Scott Nykl CSSE 411 Senior Seminar.