AspectJ implementation design mode (2) - policy mode

zhaozj2021-02-16  43

This article will continue to introduce the policy mode of using the AspectJ to implement design mode, and the article describes the reference implementation of the policy mode aspectj version in the form of a book discount example.

Strategy mode is divided into behavior and environment. Environmental class is responsible for maintaining and querying behavioral classes, and various algorithms are implemented by specific policy classes. Due to the algorithm and the environment, the increase or decrease of the algorithm does not affect the environment and client. The simple UML legend of the strategy mode is as follows

And the policy mode UML map with aspectj is used, there is a big difference

The Context and Strategy interface are declared by abstract STRATEGYPROTOCOL, while specific policy implementations are responsible for implementing interfaces for specific policy classes and environmental classes, and define PointCut DiscountStrateGy to capture a particular algorithm. This aspect uses Around Advice to achieve calls to different policy classes. Let's introduce the code implemented

StrategyProtocol abstract

public abstract aspect StrategyProtocol {protected interface Context {} protected interface Strategy {} private Strategy Context.strategy = null; public void Context.setStrategy (Strategy strategy) {this.strategy = strategy;} public Strategy Context.getStrategy () {return strategy }}

This aspect uses the Inter-Type declaration defines the Context and Strategy interfaces and declares the management policy class for the Context interface.

Discountstrategyaspect.java

Public aspect descountstrategyaspect extends strategyProtocol {

DECOUNT IMPLEments: // discount is a specific environmental class

// defines three types of discounts algorithm, Strategy interfaces declare parents: NoDiscountStrategy implements Strategy; declare parents: FlatRateStrategy implements Strategy; declare parents: PercentageStrategy implements Strategy; pointcut discountStrategy (Context context): target (context)

& Call (Double Discount.getdiscount ());

// capture algorithm calls junction (JoinPoint) double around (Context context): discountStrategy (context) {Strategy strategy = ((Discount) context) .getStrategy (); // Context abstract methods defined in terms of return getDiscount (strategy) }

// call a specific strategy algorithm private double getDiscount (Strategy strategy) {if (strategy instanceof NoDiscountStrategy) return ((NoDiscountStrategy) strategy) .calculateDiscount (); else if (strategy instanceof FlatRateStrategy) return ((FlatRateStrategy) strategy) .calculateDiscount ( ); else if (strategy instanceof PercentageStrategy) return ((PercentageStrategy) strategy) .calculateDiscount (); else throw new RuntimeException ( "No such strategy defined");}} Discount.java environmental

Public class discount {// does not implement a getDiscount method, implemented public double getDiscount () {return 0;}}

NodiscountStrategy.java policy class

public class NoDiscountStrategy {private double price; private int copies; public NoDiscountStrategy (double price, int copies) {this.price = price; this.copies = copies;} public double calculateDiscount () {return 0;}}

FlatrateStrategy.java Policy class

public class FlatRateStrategy {private double price; private int copies; private double amount; public FlatRateStrategy (double price, int copies, double amount) {this.price = price; this.copies = copies; this.amount = amount;} public double Calculatediscount () {return amount * copies;}}

Percentagestrategy.java Policy class

public class PercentageStrategy {private double price; private int copies; private double percentage; public PercentageStrategy (double price, int copies, double percentage) {this.price = price; this.copies = copies; this.percentage = percentage;} public double Calculatediscount () {return copies * price * percentage;}}

The above three strategies have a CalculateDiscount method, which implements different discount strategies.

Demo.java test code

public class Demo {public static void main (String [] args) {double price = 100; int copies = 10; Discount discount = new Discount (); discount.setStrategy (new NoDiscountStrategy (price, copies)); System.out. println ( "Discount using No Discount Strategy is:" discount.getDiscount ()); discount.setStrategy (new FlatRateStrategy (price, copies, 20)); System.out.println ( "Discount using Flat Rate 20 Discount Strategy is: " discount.getdiscount ()); discount.setstrategy (New Percentagestrategy (Price, Copies, 0.5)); System.out.Println (" Discount Using Percentage 50% Strategy IS: " discount.getdiscount ());}} The test code allows environmental discount to use three policy classes and returns discount values. The result is as follows

Discount Using no Discount Strategy is: 0.0discount Using Flat Rate 20 Discount Strategy IS: 200.0discount Using Percentage 50% Strategy IS: 500.0

At this point, I have fully implemented a discount example of using policy mode design using Aspectj. The next article of this series will introduce how to use AspectJ to implement factory method mode. (Description, this series is assumed to be reader familiar with the design mode and aspectj, so it will not introduce this knowledge, mainly in an example, all examples can run through test, because I published an article for the first time if there is a deficiency And mistakes, please forgive you).

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.

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

New Post(0)