Headline

Advanced modularization with Language:AspectJ

Motivation

Concepts of aspect-oriented programming (say, advanced modularization) are put to work. The starting point is an object model for companies with properties but no other instance methods. The basic operations are implemented by adding the corresponding methods through inter-type declarations "after the fact"---in an extra module. Such a modular development should be compared with the simple approach of Contribution:javaComposition where all methods are readily implemented within the classes. Further, logging is supported by around advice on any call for cutting salaries.

Illustration

The following Language:AspectJ aspect declares instance methods for cutting salaries on the classes of the object model for companies; the actual method bodies, which are elided, do not differ in any way from a straightforward OO implementation:

public aspect Cut {
   public void Company.cut() { ... }
   public void Department.cut() { ... }
   public void Employee.cut() { ... }
}

The next goal is to log applications of cut so that the total of an object is logged before and after applying cut. The following fragment of a log illustrates the idea:

...
> BEGIN Cut Department "Dev1.1". Total: 4689.0
> BEGIN Cut Employee "Karl". Total: 2345.0
> END Cut Employee "Karl". Total: 1172.5
> BEGIN Employee "Joe". Total: 2344.0
> END Employee "Joe". Total: 1172.0
> END Department "Dev1.1". Total: 2344.5
...

Hence, the cut methods of company, department, and employee objects should be intercepted. As a preparatory step, the design of the object model is improved so that the pointcut can be expressed more compactly with the help of interface polymorphism. That is, an interface for all common operations is introduced and implemented "after the fact" for all the types of the object model for companies:

public interface Operations {
   String getName();
   double total();
   void cut();
}

public aspect Polymorphism {
   declare parents: Company implements Operations;
   declare parents: Department implements Operations;
   declare parents: Employee implements Operations;
}

It remains to declare a pointcut for all calls of cut and to advice each intercepted method call with logging actions before and after (say, around) it.

pointcut cut(Operations o):
   target(o) && call(void Operations.cut());
        
void around(Operations o): cut(o) {
   ... // Log "before" state
   proceed(o);
   ... // Log "after" state
}

Architecture

Package org.softlang.company hosts the object model for Feature:Hierarchical company. The Java classes of this object model do not implement any other features. Package org.softlang.aspectj hosts aspects for Feature:Total, Feature:Cut, Feature:Depth, and Feature:Logging. There is another aspect Polymorphism which rehashes existing operations (getName, total, cut) as an interface Operations on the relevant classes. Finally, package org.softlang.tests hosts JUnit tests; see below.

Usage

  • The implementation is provided as an Eclipse project.
  • AJDT must be installed in Eclipse if Eclipse is to be used.
  • Open the project with Eclipse; this will also build the project.
  • There are JUnit tests available as the package org.softlang.tests.
  • Run class Operations with JUnit to exercise several operations.

There are no revisions for this page.

User contributions

    This user never has never made submissions.

    User edits

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.