Recipe23.3.Applying the Border Control Design Pattern


Recipe 23.3. Applying the Border Control Design Pattern

Problem

You want to formally define important regions within your application so your aspects can reuse those definitions to ensure they are only applied in the correct areas.

Solution

Apply the Border Control aspect-oriented design pattern. Figure 23-3 shows the key components of the Border Control pattern.

Figure 23-3. The structure of the Border Controller pattern


The key roles in the Border Control pattern shown in Figure 23-3 are:


BorderControllerAspect

The aspect at the center of the design pattern that specifies any important regions in your application that can be used by other aspects to limit their effects to the appropriate areas


The application, application.testing, and thirdpartylibrary packages

The example target application's important packages as they are selected by the BorderControllerAspect's reusable pointcut declarations


MainApplication

The main(String[]) method of this class provides an example of an important region inside a class

Discussion

The Border Controller declares important characteristic regions of your code that can be method, class, and package in scope. Example 23-4 is modified from Recipe 22.1 and shows how the Border Controller pattern can be implemented in AspectJ to specify a set of regions that include packages and methods within an example application.

Example 23-4. An example of the Border Controller pattern
public aspect BorderControllerAspect  {    /**     * Specifies the testing region.      */    public pointcut withinTestingRegion( ) :        within(com.oreilly.aspectjcookbook.testing.+);        /**     * Specifies My Applications region.      */    public pointcut withinMyApp( ) : within(com.oreilly.aspectjcookbook.                                            myapp.+);        /**     * Specifies a third party source code region.     */    public pointcut withinThirdParty( ) :        within(com.oreilly.aspectjcookbook.thirdpartylibrary.+);        /**     * Specifies the applications main method.     */    public pointcut withinMyAppMainMethod( ) :        withincode(public void com.oreilly.aspectjcookbook.myapp.MyClass.                  main(..)); }

In Example Example 23-4, the regions declared in the Border Controller aspect could then be reused within the target application's aspects:

 pointcut regionsOfInterest( ) :        BorderControllerAspect.withinMyAppMainMethod( ) ||        BorderControllerAspect.withinThirdParty( ) ||        BorderControllerAspect.withinTestingRegion( );

Here are the key characteristics of the Border Controller pattern:

  • It provides a mechanism for declaring reusable pointcut logic that formalizes your application's architecture.

  • The regions that are declared within a Border Controller aspect can be reused throughout your application's aspects whenever an aspect is to work within one or more specific application areas only.

  • If the structure of your application were to change and the Border Controller pattern had been applied, then it is likely that only the Border Controller aspect would need to be updated to reflect these changes throughout all the aspects in your application.

  • The Border Controller is usually best applied as a singleton in that one Border Controller is usually enough for a single application.

The Border Controller pattern is useful when you want to do the following:

  • Have an application of reasonable complexity where there are defined internal areas to your application to which aspects must be constrained.

  • Protect your application's aspects from future changes to overall application structure. To save yourself the headache of updating all your aspects when your application's structure changes, the Border Controller pattern provides a single point where those changes can be made and automatically reflected across your applications aspects.

The Border Controller design pattern can be used as a foundation to define an application's structure when using most design patterns and applications, including the Cuckoo's Egg and Policy aspect-oriented design patterns.

See Also

The programmatic scope based pointcuts are examined in Chapter 9; the Border Controller aspect is used in Recipe 22.1; the Cuckoo's Egg aspect-oriented design pattern is described in Recipe 23.1; the Policy aspect-oriented design pattern is described in Recipe 23.4.



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