Recipe23.1.Applying the Cuckoo s Egg Design Pattern


Recipe 23.1. Applying the Cuckoo's Egg Design Pattern

Problem

You want to override the type of object instantiated on a constructor call to return an object of a different class transparently to the original business logic.

Solution

Apply the Cuckoo's Egg aspect-oriented design pattern. Figure 23-1 shows the key components of the Cuckoo's Egg pattern.

Figure 23-1. The structure of the Cuckoo's Egg pattern


The key roles in the Cuckoo's Egg pattern shown in Figure 23-1 are:


CuckoosEggAspect

The aspect at the center of the design pattern that intercepts the creation of the OriginalClass class and instead returns an instance of ReplacementClass.


SharedInterface

Optional component of the pattern. According to the AspectJ rules for overriding around( ), the ReplacementClass must inherit from the type expected on the constructor call. In this example, that type is the SharedInstance interface.


OriginalClass

The class originally being constructed when that constructor call is overridden by the CuckoosEggAspect.


ReplacementClass

The class instantiated when the constructor call to OriginalClass is overridden.


MainApplication

Represents an example area within your application where the constructor call to OriginalClass is to be overridden.

Discussion

As simple as the Cuckoo's Egg pattern is, it is one of the more powerful capabilities of aspect orientation as implemented in AspectJ. It is reasonably common to want to control and change an object instantiated on a constructor call or on a factory method when an aspect is applied. The Cuckoo's Egg design pattern formalizes this common AspectJ use case.

Example 23-1 is modified from Recipe 20.2 and shows how the Cuckoo's Egg pattern can be implemented in AspectJ to return an instance of AnotherClass when a MyClass constructor is called.

Example 23-1. An example of the Cuckoo's Egg design pattern
public aspect ControlClassSelectionAspect // Cuckoo's Egg Aspect {    public pointcut myClassConstructor( ) : call(MyClass.new( ));        Object around( ) : myClassConstructor( )    {       return new AnotherClass( );    } }

The key characteristics of the Cuckoo's Egg pattern are:

  • It provides a suitable pointcut declaration to capture the construction of a class to be overridden.

  • You can obtain any arguments supplied on the original constructor call by using the args([Types | Identifiers]) pointcut to pass the identifiers to the corresponding advice if applicable.

The Cuckoo's Egg pattern is useful in circumstances where you do the following:

  • Implement mock objects for testing purposes

  • Provide a proxy object in place of real instance of the class

The Cuckoo's Egg pattern can collaborate with the Border Controller design pattern, which can be used to limit the constructor calls to a particular class overridden by the Cuckoo's Egg pattern.

See Also

The article at http://www.onjava.com/pub/a/onjava/2004/10/20springaop2.html shows how to implement the Cuckoo's Egg Pattern using the Spring Framework; using the call(Signature) pointcut to capture and override a call to a constructor is described in Recipes Recipe 7.1 and Recipe 20.1; the around( ) form of advice is covered in Recipe Recipe 13.4; the AspectJ rules on how overriding around( ) advice can be applied are covered in more detail in Recipe Recipe 20.2.



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