|
Aspects, Concerns, and Java
AspectJ may take some getting used to, but aspect-oriented programming can be a nice complement to object-oriented programming
by James W. Cooper
March 2003 Issue
I've always found the naming conventions of Aspect-oriented programming confusing. They refer to Aspects, Concerns, and Crosscuts (like the old saw?) and none of them means what it usually means. However, the essence of Aspect-oriented programming (AOP, if you will) is that in any object-oriented program, even if it's well designed, there will be some method calls that appear in several places throughout the program. If you change the call to that method, or change the error handling of that method, you may need to make changes in all of the occurrences of this call. If the change is subtle, the compiler may not catch it, which could lead to annoying run-time errors that might be difficult to track down.
For a simple-minded example, consider a graphics program that can draw lines, squares, and circles. You probably would organize such a program to use line objects, square objects, and circle objects, each of which might have a draw method. In addition, you might have a screen update method that each of these objects would have to call to refresh the display. Thus, even though the program is well structured in the classical object-oriented sense, it would be nice if there were a view of the program where all of these separate calls could be treated as a single spot in the view. Then you could change and polish that code by making the change in a single place.
While our drawing example is necessarily simple and has simpler solutions, larger complex programs frequently have code that is not and cannot be modularized. Calls to some methods are often scattered throughout any otherwise well-designed program. Specifically, calls to elements such as program loggers must of necessity be scattered throughout your program. But in general, if you modularize some aspects of your program, it may not be possible to modularize others at the same time. When code is scattered in different fragments throughout a program, it is hard to see its structure and hard to get a good view of the apparent tangling of the code. It's hard to change such code efficiently and hard to find all the cases that have to be changed.
Join Together
It also would be nice if there were a language where you could carry out this sort of viewing and changing efficiently. The AspectJ language is a language for just this purpose. Furthermore, AspectJ is itself written in Java and therefore is available wherever Java runs. You can download AspectJ from its Web site,www.AspectJ.org, which also posts many articles about the language. In addition to the compiler code, there are plug-ins for Eclipse, JBuilder, and emacs. AspectJ was developed at PARC under a DARPA grant, and while the project at PARC is ending soon, the AspectJ organization will persist.
Back to top
|