Recipe17.3.Implementing the Abstract Factory Pattern


Recipe 17.3. Implementing the Abstract Factory Pattern

Problem

You want to apply the abstract factory pattern using AspectJ.

Solution

The abstract factory pattern supports groups of related classes being instantiated while shielding the clients of the factory from the exact implementations.

To implement the abstract factory pattern using AspectJ, create an aspect that can remove the abstract factory pattern's reliance on an abstract base class using static cross-cutting techniques to provide default implementations of the factory methods.

Discussion

Creating a generic factory using abstract aspects makes no sense because the factory contains methods specific to the objects that can be created. The only advantage an AspectJ implementation offers to this design pattern is the ability to remove the reliance on an abstract base class for the abstract factory and replace it with a simple interface. This means that the specialized factories can inherit from other more appropriate classes rather than having use their one allowed inheritance relationship to support the design pattern.

Example 17-5 shows how the abstract factory pattern could be applied to the factory needs of a simple application by providing default implementations of the methods declared in a Java interface.

Example 17-5. Applying the abstract factory pattern using aspects
public interface ComputerFactory {    public Computer createPentiumProcessorComputer( );             public Computer createComputerWithHardDisk(HardDisk hardDisk); } public aspect DefaultComputerFactoryImplementation  {             public Computer ComputerFactory.createPentiumProcessorComputer( )    {       Processor processor = new Processor("Pentium 4 : 9089085043");       Motherboard motherboard = new Motherboard("019283", processor);       HardDisk hardDisk = new HardDisk("738947");       FloppyDisk floppyDisk = new FloppyDisk("93746");       Computer computer = new Computer("12345", motherboard, hardDisk,        floppyDisk);       return computer;    }    public Computer ComputerFactory.createComputerWithHardDisk(HardDisk                                                                hardDisk)    {       Processor processor = new Processor("Pentium Standard : 123478");       Motherboard motherboard = new Motherboard("434244", processor);       FloppyDisk floppyDisk = new FloppyDisk("432434");       Computer computer = new Computer("56789", motherboard, hardDisk,        floppyDisk);       return computer;         } }

See Also

Chapter 16 contains recipes that show how static cross-cutting techniques can be used to extend classes and provide default implementations of interface elements.



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