This article will continue to introduce factory method patterns that use Aspectj to implement design patterns, and articles describe the reference implementation of the factory method mode aspectj version in the form of a farm gardener managing fruit (reader readers read by Java and mode).
Factory Method Models Create specific products according to the product's level structure, which generally includes abstract factories, specific factories, and abstract products, specific products, each particular factory for creating a corresponding product. The simple UML legend of the pattern is as follows
Below is a factory method mode UML map implemented using aspectj
Abstract Aspects FactoryMethodProtocol is simple to define Abstract PointCut CreateMethod to capture a creation method for specific applications (or omitted).
FactoryMethodProtocol abstract
Public Abstract aspect factoryMethodprotocol {Abstract Pointcut CreateMethod ();
FactoryMethodImpl.java
Public aspect factorymethodIMPL EXTENDS FACTROYMEMEMEMEMEMEMPROTOCOL {
Public Fruit Fruitgardener.Factory () {// To create interface definition factories
Return NULL;
}
// Inter-Type declare specifically created classes and implements creation interfaces
Declare Parents: Applegardner Implements Fruitgardener;
Declare Parents: grapegardener imports fruitgardener;
// Specify CreateMethod to capture the creation method of FruitGardener and its subclasses
Pointcut CreateMethod (): Call (Fruit Fruitgardener .factory ());
Fruit Around (Fruitgardner Garden): Target (Gardener && CreateMethod () {
Return Choosegardener (Gardener); // Factory method returns an abstract product.
}
Private Fruit Choosegardner (Fruitgardener Gardener) {
IF (Gardener InstanceOf AppleGarden) {
Return new apple ();
}
Else IF (Gardener InstanceOf Grapegardner) {
Return new grape ();
}
Else {
Throw New RuntimeException ("No Such Kind of Fruit";
}
}
// Declare compilation error: When the customer uses NEW to create a specific product.
// Objective: The forced program is created in accordance with the form of factory methods.
// Optional as a declaration warning Declare Warning: [Pointcut]: [WARN MSG]
Declare Error:! (With "&&! createMethod ())
&&! Withnotimpl && call (Fruit .new (..))
: "You can only create fruits through the method factory provides subclass";
Three empty creation object declarations, the creation logic is defined in FactoryMethodImpl
Public interface fruitgardener {}
Public class grapegardener {} public class applegardener {}
Fruit.java Abstract Product Class
Public Abstract Class Fruit {
Public abstract void growth ();
Public abstract void harvest ();
Public Abstract void Plant ();
}
Apple.java product class
Public class apple extends fruit {
Public void greow () {
System.out.println ("Apple Is Growing ...");
}
Public void harvest () {
System.out.Println ("Apple Has Been Harvested");
}
Public void plant () {
System.out.println ("Apple Has Been Planted");
}
}
Grape.java product class
Public class grape extends fruit {
Public void greow () {
System.out.println ("Grape is glout ...");
}
Public void harvest () {
System.out.Println ("Grape Has Been Harvested");
}
Public void plant () {
System.out.Println ("Grape Has Been Planted");
}
}
Demo.java test code
Public class demo {public static void main (string [] args) {fruitgardner gardener = new applegardner ();
FRUIT FRUIT = Gardener.Factory (); // Create Apple
Fruit.grow ();
FRUIT.harvest ();
FRUIT.PLANT ();
Gardener = new grapegardener ();
Fruit = gardener.Factory (); // Create a grape
Fruit.grow ();
FRUIT.harvest ();
FRUIT.PLANT ();
// new apple (); new grape (); // Generate compilation errors, not directly generating products
}
The test code created apples and grapes using Apple and vineyardia, and its operation is as follows.
Apple is groing ...
Apple Has Bend.
Apple Has Been Planted.
Grape is groing ..
Grape Has Been Harvested.
Grape Has Been Plated.
One thing is worth noting that if you add new apple () or new grape (), add new Apple () or new grape (), add new Apple () or new grape (), the AJC compiler will not compile success in accordance with the mistakes declared in FactoryMethodImpl, and will get the following output:
D: /javadev/jbuider/aopfactorymethod/src/aopfactorymethod/demo.java 26 you can only Create Fruits through the method factory provivid by FruitgardenNer and ITS Subclass 1 Error.
At this point, I have fully implemented an example of a farm gardener designing using the factory method pattern. The four of this series will introduce how to implement abstract factory models with aspectj. Reference
1. << Java and mode >> Hongyan Electronic Industry Press
2.http://www.eclipse.org/aspectj/
statement
This article retains copyright by STARCHU1981, and if you need to pay, please write the author and source.