Recipe 23.3. Applying the Border Control Design PatternProblemYou 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. SolutionApply 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 patternThe key roles in the Border Control pattern shown in Figure 23-3 are:
DiscussionThe 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 patternpublic 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:
The Border Controller pattern is useful when you want to do the following:
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 AlsoThe 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. |