This article describes the single-case models that use Aspectj to implement design patterns, and the article will implement an AspectJ version of the register single case class.
Example description
Single case model has some basic features
· Single example can only have one instance
· Single cases must create their own unique instances
· Single cases must provide this example to all other objects
In Java, we implement single-size class generally need to use private constructors and a static instance variable, but also provide a method such as getInstance () to construct and return this instance variable. Use aspectj to implement single-case mode, you can complete these details to a specific aspect, and the client can still use the New operator to create an instance of an object, and this aspect will utilize the declare error mechanism combined with PointCut capture single case. The construction behavior of the class class and prohibits it to ensure that the examples of the single-class class will not be constructed by their subclasses. Let us take a single case class with Aspectj to see what kind of magic.
Figure 1: UML diagram of aspectj to implement register
As shown, abstract askingLETONPROTOCOL uses the Inter-Type declaration to define a Singleton identity interface, and the specific single case requires this interface. Private variables Singletons are used to register instance variables of various single case classes. Abstract PointCut is achieved by sub-aspects, while Advice Around implements specific single-size constructors and registration logic.
For specific aspect, REGSITONIMPL inherits the abstract aspect, which defines the specific SubclassConstructor behavior and declares a regsingleton class, declare it to implement the Singleton interface, which is a specific single case class. It also declares a compile error, which occurs when the customer code calls the constraints of the regsingleton subclass.
The class on the left side is the function of testing a single example, and the subclass of the single-size class can construct a single case class, so the logic is simple to contain only constructors.
Source code
Abstract aspect regsingletonprotocol.java
Import java.util. *;
Public Abstract aspect regsingletonprotocol {
Private hashtable singletons = new hashtable ();
Public interface singleton {}
Abstract Pointcut SubclassConstructor ();
Singleton Around (): Call ((Singleton ). New (..)) {// Capturing the constructor call for Singleton subclasses
// Get class variables
Class singleton = thisjoinpoint.get.GetSignature (). GetDeclaringtype ();
IF (Singletons.get (Singleton) == NULL) {
Singletons.put (Singleton, Proceed ()); // If there is no such variable, it will be registered in the table.
}
Return (Singleton) Singletons.get (Singleton); // Returns an instance of the specified category
}
}
It is worth mentioning that AROUND, which first uses the method of thisjoinPoint instance to get class variables, then search for tables, if this type does not exist, use this type itself to create an instance (using the proceed () method to perform the captured method call The original logic, which represents the constructor of the Singleton subclass and stores it in the table, and then returns an instance of this type.
Specific aspect regsingletonimpl..java
Public aspect regsingletonImpl Extends regsingletonprotocol {
Declare Parents: Regsingleton Implements Singleton; Pointcut SubclassConstructor (): Call ((regsingletonchild ). New (..));
DECLARE Error: SubclassConstructor (): "Sub Class Can't Access The Singleton Instance";
}
Specific aspects declare the specific single case, let it implement the Singleton Interface SubclassConstructor to capture all RegSingletonChild and its subclause (" " represents the current class and its subclass), Declare Error generates compilation when it captures this call. error.
Single example regsingleton..java
Public class regsingleton {
Private statin = 0;
Public regsingleton () {
COUNT ;
System.out.println ("Class No." Count);
}
}
Single case class subclass regsingletonchild.java
Public class regsingletonchild extends regsingleton {
Public regsingletonchild () {
Super ();
}
}
Client Demo Demo.java
Public class demo {
Public static void main (String [] args) {
System.out.println (New Regsingleton ());
// system.out.println (new regsingletonchild ());
System.out.println (New Regsingleton ());
}
}
Result analysis
The result of DEMO output is as follows
Class No. 1
Aopsingleton.regsingleton@6e1408
Aopsingleton.regsingleton@6e1408
Indicates that class regsingleton only creates once, and two constructor calls return the same instance, which meets the characteristics of single-class classes. Note If the regsingletonchild that is commented in Demo is created and compiled, you will get the following error.
1 Error
"D: /javadev/JBuilder/aopsingleton/src/demo.java":
Sub Class Can't Access The Singleton Instance At Line 15, Column 0
This overcomes the disadvantages of creating other instances in Java implementation of the registering single case class subclass.
Readers can easily change this example to non-registered singletons. Just join the following statement in RegsingletonProtocol
PRIVATE SINGLETON Instance = NULL;
Then modify the logic in Around as follows
Singleton arround (): Call ((Singleton ). New (..)) {
IF (instance == null) {
Try {
Instance = (Singleton) proceed ();
} catch (exception e) {
Throw new runtimeException ("construction failed");
}
}
Return Instance;
}
Disclaimer This article is reserved by Starchu1981, if you need to pay, please write the author and source.