This article describes the decorative mode of design mode using AspectJ, and the article uses an example of a print invoice to explain how to use AspectJ to implement decorative mode.
Example description
Decorative mode is a model that everyone is familiar with. The most typical example is the Java I / O library, and its design is fully decorated. We usually package a class in the client to use the New operator, such as the following code.
DataOutputStream out = new dataputstream (New FileoutputStream ("1.txt");
It uses DataOutputStream to package FileOutputStream, so you can use the former's method to write file streams. In the decorative mode implemented by Aspectj, we will not see such new operations in the client, but directly to the decorative behavior to the method to increase the additional operation inside the method. Examples of printing invoices in << Java and Mode >> are now changed to AspectJ implementation, and we see how the AspectJ is how to apply additional to the method body. System UML map is as follows
Source code
Abstract aspect orderdecorator.java
Abstract aspect orderdecorator {
Declare PARENTS: SalesOrder Extends Order;
Public void salesOrder.print () {
Super.print ();
}
Protected PointCut Print (Order Order): Target (Order) & Call (Public Void Print ());
}
Abstract aspects are declared to the SalesOrder class with the type of description, and implement method print (). It also defines a cleavage print (ORDER ORDER), which will capture the Order class Print method call.
Specific aspect of HEADERDECorator.java
Public aspect headerdecorator extends ORDERDECorator {
Void Around (Order Order): Print (ORDER) {
Printheader (Order);
Proceed (ORDER);
}
Private Void Printheader (ORDER Order) {
System.out.println ("/ t *** / tinvoice / t ***");
System.out.println ("XYZ Incorporated / NDATE OF SALE:");
System.out.println (ORDER.GETSALESDATE ());
System.out.println ("===============================================");
System.out.println ("Item / T / TUnits / TUnit Price / Tsubtotal");
}
}
HeaderDecorator implementation of the invoice head print function, defines the method call captured by the uninception point print, and then print the invoice head and then use the proceed () method to perform the original logic of the captured method.
FooterDecorator.java
Public aspect footerdecorator extends ORDerDecorator {
Declare precedence: FooterDecorator, HeaderDecorator; // Declaration Priority Void Around (Order ORDER): Print (Order) {
Proceed (ORDER);
Printfooter;
}
Private Void PrintFooter (ORDER Order) {
System.out.println ("======================================");
System.out.println ("Total / T / T / T / T / T" Order.Formatcurrency (Order.getgrandTotal ())));
}
}
FOOTERDECORATOR implements the invoice pins printing function, which defines the method call captured by the intercept cut point print, and then use the proceed () method to perform the original logic of the captured method to print footpin information. There is also the most important thing is its precedence declaration, this statement determines that FOOTERDECORATOR is capturing the same notice as the same notification in the same way as the HeaderDecorator. Declarations in the aspect stated that HeaderDecorator is higher than the priority of FooterDecorator, and its notification is performed prior to execution.
Abstract set of Order.java
Import java.util. *;
Import java.text.numberformat;
Abstract public class order {
Private OrderLine InkorderLine;
protected collection items = new arraylist ();
Protected string customername;
Protected Date Salesdate;
Public void print () {
Iterator it = items.iterator ();
While (it.hasnext ()) {
OrderLine Item = (OrderLine) it.next ();
item.printline ();
}
}
Public string getcustomername () {
Return Customername;
}
Public void setcustomername (String Customername) {
This.customername = Customername;
}
Public Date getsalesdate () {
Return Salesdate;
}
Public void setsalesdate (date salesdate) {
THIS.SALESDATE = SALESDATE
}
Public void additem (OrderLine Item) {
Items.add (item);
}
Public Void RemoveItem (OrderLine Item) {
Items.Remove (item);
}
Public double getgrandtotal () {
Double Amount = 0.0d;
Iterator it = items.iterator ();
While (it.hasnext ()) {
OrderLine Item = (OrderLine) it.next ();
Amount = item.getsubtotal ();
Return Amount;
}
Public String Formatcurrency (double amount) {
Return Numberformat.getcurrencyinstance (). Format (Amount);
}
}
Single item class OrderLine.java
Import java.text.numberformat;
Public class orderline {
Private string itemname;
PRIVATE INT UNITS;
PRIVATE DOUBLE UNITPRICE;
Public string getItemName () {
Return ItemName;
}
Public void setItemName (String ItemName) {
THIS.ItemName = ITEMNAME;
}
Public int getUnits () {
Return Units;
}
Public void setUnits (int units) {
THIS.UNITS = UNITS;
}
Public Double getUnitprice () {
Return Unitprice;
}
Public void setUnitprice (double unitprice) {
THIS.UNITPRICE = UNITPRICE
}
Public void printline () {
System.out.println (itemname "/ t" units "/ t"
FormatCurrency (Unitprice) "/ t"
Formatcurrency (getSubtotal ()));
}
Public double getSubtotal () {
Return Units * Unitprice;
}
PRIVATE STRING FORMATCURRENCY (Double Amount) {
Return Numberformat.getcurrencyinstance (). Format (Amount);
}
}
Specific sales set single SalesOrder.java
Public Class SalesOrder {}
Client Demo Demo.java
Public class demo {
Private static Order ORDER;
Public static void main (String [] args) {
ORDER = New SalesOrder ();
Order.setSalesdate (New Java.util.date ());
Order.SetCustomerName ("starchu1981");
ORDERLINE ITEM1 = New ORDERLINE ();
Item1.SETITEMNAME ("Fire Wheel Tire");
Item1.setUnitprice (154.23);
Item1.setUnits (4);
Order.Additem (item1);
OrderLine Item2 = new orderline ();
Item2.SetItemName ("Front Fender");
Item2.setUnitprice (300.45);
Item2.setUnits (1);
Order.Additem (item2);
ORDER.PRINT (); // This is different from the Java version of the client code, which is explained later.
}
}
Result analysis
The result of DEMO output is as follows
*** Invoice ***
XYZ Incorporated
Date of Sale:
Thu Jul 24 11:45:10 CST 2003
==========================================================================================================================================================
Item Units Unit Price Subtotal
Fire Wheel Tire 4 ¥ 154.23 ¥ 616.92
Front Fender 1 ¥ 300.45 ¥ 300.45
==========================================================================================================================================================
Total ¥ 917.37
Example output is consistent with the result of our expectations. Especially worth noting is the last line of the code, usually there may be the following code in Java.
Order Order = New HeaderDecorator (New FooterDecorator ());
ORDER.PRINT ();
The decorative mode using the Aspectj constructed is eliminated in the client, and can directly use the method without the need to provide a specific decorative behavior of the specific decorative class, which makes the client code more transparent. Customer can do it. Decorative class. Of course, there is also a shortcoming that the client cannot control all decorative behavior itself, for example, if the customer cannot undo the behavior of the printing footprint.
This article retains copyright by STARCHU1981, and if you need to pay, please write the author and source.