AspectJ implementation design mode (5) - iterative score

zhaozj2021-02-16  46

This article describes the iterative sector mode using AspectJ to implement design mode. The article implements the inner arms of the AspectJ version with an example of purchasing goods.

Due to the extensive application of the iterative score, the article does not repeat the specific content of the mode, I use the specific example to explain how to use AspectJ to complete the function described. This example refers to the example of the iterative sub-mode in << Java and mode >>.

Figure 1 - Example System UML Map

Example The system first defines an abstract shopping basket class Purchase to give the behavior of the specific shopping basket and methods that need to be realized. Shopping baskets PurchaseOfcoPA and PurchaseOfcopb inherited from Purchase, which could create a forward iterative ForwardIterator and a backward iterative backawrditerator used for traversal. The system contains an abstract IteratoralASpect, which declares how the Purchase class is used to create an iteration and adapter class PURCHASEITERATOR, which implements interface java.util.uterator, and also defines an abstract PointCut Iterator Supply Lion use. Subwood ForwardITERATORASPECT and BackwardITERASPECT define the CreateETETETETERMATOR method of the respective Iterator PointCut to capture the corresponding Purchase subclass, and provides the Advice Around implementation the Createrator method to return to the corresponding category iteration. The most important thing is the internal FORWARDITERATOR and BACKWARDITERATOR in these two sub-asks that achieve the logic of forward iterations and backward iterations.

Abstract Purchase.java

Import java.util. *;

Public Abstract Class Purchase {

Private collection elements = new arraylist ();

Public void append (Object obj) {// Take home element

Elements.Add (obj);

}

Public void remove (Object obj) {// Remove element

Elements.Remove (OBJ);

}

Public Object CurrentItem (int index) {// Current element

Return ((arraylist) Elements .get (Index);

}

Public int count () {// element total

Return Elements.size ();

}

}

Specific class PurchaseOfcoPA and PurchaseOfcopb

Public Class PurchaseOfcoPA {}

Public Class PurchaseOfcopb {}

Adapter Purchaseiterator.java

Public Class PurchaseIterator {}

Abstract territory IteratoralASpect.java

Import java.util.iterator;

Public Abstract aspect iteratoralaspect {

Public iterator purchase.createiterator () {return null;}

Abstract POINTCUT ITERATOR (PURCHASE PURCHASE);

// purchaseiterator implements Iterator, used as an adapter class, supplies subscription

Declare Parents: Purchaseiterator IMPLEments ITERATOR;

INT PURCHASEITERATOR.CURRENT = 0;

Purchase purchaseiterator.purchase;

// Iterator interface method's default implementation

Public boolean purchaseiterator.hasnext () {return false;} public object purchaseiterator.next () {return null;}

Public void purchaseiterator.remove () {}

Public void purchaseiterator.Append (Object obj) {}

}

Specific aspect ForwardItemspect.java

Import java.util.iterator;

Public aspect forwardIteratoralASpect extends iteratoralaspect {

Declare Parents: PurchaseOfcopa Extends Purchase;

POINTCUT ITERATOR (Purchase Purchase): Target (Purchase)

&& call (Iterator CreateTeiterator ())

&& if (purchase instanceof purchaseofcopa; // Capture the creation method of forward iteration

Iterator Around (Purchase Purchase): Iterator (Purchase) {// creation method executes logic, and constructively iterator

Return New ForwardITERATOR (PURCHASE);

}

Private static class forwardIrtor Extends PurchaseIterator {// Before Iertone Internal Class

Public ForwardItem (Purchase Purchase) {

Super ();

this.purchase = purchase;

Current = 0; // Current cursor is the first element

}

Public Boolean Hasnext () {

Return (Current <= purchase.count () - 1);

}

Public Object next () {

IF (! Hasnext ()) {

Throw new arrayindexoutofboundsexception ("iTerator out of bounds");

}

Return Purchase.currentItem (CURRENT );

}

}

}

Specific aspect BackwardItemratoralASpect.java

Import java.util.iterator;

Public Aspect BackwardITORASPECT EXTENDS ITERATORASPECT {

DechaseOfcopb Extends Purchase;

POINTCUT ITERATOR (Purchase Purchase): Target (Purchase)

&& call (Iterator CreateTeiterator ())

&&ness (Purchase InstanceOf PurchaseOfcopb); // Capture the creation method of iterative

Iterator Around (Purchase Purchase): Iterator (Purchase) {// creation method performs logic, and the iterator

Return New BackwardITERATORTOR (PURCHASE);

}

Private static class backardItemrator Extends PurchaseIterator {// Runior iterative internal class

Public BackwardITITERATOR (Purchase Purchase) {

Super ();

this.purchase = purchase;

Current = purchase.count () - 1; // Current cursor is the last element}

Public Boolean Hasnext () {

Return (Current> = 0);

}

Public Object next () {

IF (! Hasnext ()) {

Throw new arrayindexoutofboundsexception ("iTerator out of bounds");

} else {

Return Purchase.currentItem (Current -);

}

}

}

}

Sample code DEMO.JAVA

Public class demo {

Public static void main (string [] args) throws exception {

Purchase purchasea = new purchaseofcopa ();

Purchase purchaseb = new purchaseofcopb ();

Purchasea.Append ("Dish Washer");

Purchasea.Append ("Hair Dresser");

Purchasea.append ("microwave");

System.out.Println ("Creating Forward Iterator for Purchase A);

PrintItems (Purchasea);

Purchaseb.Append ("Hair Dresser");

Purchaseb.append ("diskman");

Purchaseb.append ("DIGITAL CAMERA");

Purchaseb.Append ("PC");

Purchaseb.Append ("Dish Washer");

System.out.println ("Creating Backward Iterator for Purchase B);

PrintItems (Purchaseb);

}

Private Static Void PrintItems (Purchase Purchase) {// Format Output Element

INT INDEX = 1;

IF (IsBackforward (Purchase)) {

INDEX = Purchase.count ();

}

Iterator it = purchase.createiterator ();

While (it.hasnext ()) {

System.out.println ("Item: no" index ":" it.next (). TOSTRING ());

IF (IsBackforward (Purchase)) {

Index -;

}

Else {

INDEX ;

}

}

}

PRIVATE Static Boolean IsBackForward (Purchase Purchase) {// Is it an iteration?

IF (Purchase InstanceOf PurchaseOfcopb) {

Return True;

}

Return False;

}

}

The output is as follows

Creating Forward Iterator for Purchase A

Item no.1: Dish Washer

Item No.2: Hair Dresser

Item No.3: Microwave

Creating Backward Iterator for Purchase B

Item No.5: Dish Washer

Item No.4: PC

Item No.3: Digital Camera

Item No.2: Discmanitem No.1: Hair Dresser

At this point, I have implemented an example of using an in-arrayed child, and an interested reader can try to implement the same example of using an externalized iteration. Also because of the next month I have to participate in an important exam, the rest of this series may be slow, please also ask the readers to forgive. At the same time, because many readers don't know the AspectJ grammar, I will introduce aspectj's grammar, configuration, and use examples in another series, hoping to increase your interest in AspectJ and AOP aspects.

statement

This article retains copyright by STARCHU1981, and if you need to pay, please write the author and source.

转载请注明原文地址:https://www.9cbs.com/read-26879.html

New Post(0)