Recipe16.4.Declaring a Default Interface Implementation


Recipe 16.4. Declaring a Default Interface Implementation

Problem

You want to provide a default implementation for an interface.

Solution

Declare the default implementations for the interface's methods within the aspect.

Discussion

Example 16-5 shows how a default implementation is provided for the MyInterface interface by implementing the void bar(String) method.

Example 16-5. Using aspects to provide a default implementation of an interface method
public aspect DefaultInterfaceImplementationRecipe {         declare parents : MyClass implements MyInterface;                  // Declare the default implementation of the bar method         public void MyInterface.bar(String name)         {                 System.out.println("bar(String) called on " + this);         } }

Figure 16-4 shows how the static class structure is affected by the application of the aspect in Example 16-5.

Figure 16-4. The static class structure before and after the aspect is applied to introduce the default interface implementation


Figure 16-4 gives a misleading picture of the outcome of applying Example 16-5. You won't see a DefaultImplementation class when the aspects are woven and compiled. However, for the sake of visualizing the architecture, the result is similar to how this figure presents it except that the void bar(int,String) is transparently added to the MyClass class.

What you end up with is something like multiple inheritance. Interfaces in Java provide a means by which a class can have an explicitly declared public interface, or role, to which it can be used. This allows multiple interfaces to be implemented by a single class because no method implementations to worry exist. Traditionally, a Java class can explicitly inherit from only one other class, which prohibits multiple inheritance. This is not the case with aspects in AspectJ.

A Java interface can have an implementation of its methods declared by an aspect and still be declared an interface allowing a form of multiple inheritance. A good candidate situation where it is useful to provide default implementations of an interface is when traditional OO design patterns are applied. A design pattern can be an invasive element of an applications design. This result may not always be a good thing when the business logic of the classes becomes blurred with the pattern logic being applied. You should probably apply the patterns wherever possible as aspects, incorporating default implementations of the appropriate roles where needed, and keep the business logic within the classes clear and manageable.

Many of the traditional design patterns use abstract classes as their base for the different roles they use. In Java, only one base class is allowed and this means that the architecture can be complicated by the conflicting desire for having a clear expression of the business relationships between classes and the desire to apply good practice design patterns. AspectJ, by providing the means of specifying a default implementation for an interface, has removed the need to have an abstract class as the base for many design patterns.

See Also

Recipe 16.1 explains how classes can be extended by aspects introducing new methods and attributes; OO design patterns and the benefits that those patterns can gain by using an aspect-oriented approach are examined in Chapter 17 through Chapter 19.



AspectJ Cookbook
Aspectj Cookbook
ISBN: 0596006543
EAN: 2147483647
Year: 2006
Pages: 203
Authors: Russ Miles

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net