Recipe18.3.Implementing the Adapter Pattern


Recipe 18.3. Implementing the Adapter Pattern

Problem

You want to apply the adapter pattern using AspectJ.

Solution

The adapter pattern provides a means of changing the message sent from one class to the message expected by the real destination class, adapting the messages to glue two classes together.

Example 18-5 shows how to define an application specific adapter pattern aspect.

Example 18-5. Adapting an existing class using aspects
public aspect PrinterScreenAdapter  {   declare parents : Screen implements Printer;   public void Screen.print(String s)   {     outputToScreen(s);   } }

Discussion

The specific PrinterScreenAdapter aspect extends the behavior of the class that is to adapt to the new capabilities required of it by using AspectJ's capabilities to extend an existing class with new methods and potentially new parent classes, being careful at all times to respect the original capabilities of the class.

Figure 18-8 shows the effects before and after the PrinterScreenAdapter aspect is applied to a Screen class that is adapted to support printing behavior.

Figure 18-8. The effects of applying the adapter pattern to the Screen class


See Also

The recipes in Chapter 16 provide more information on how to extend existing classes and incorporate interfaces when defining aspects.



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