The policy pattern defines a series of algorithms, encapsulates each algorithm, and allows them to be replaced with each other, and the policy pattern makes the algorithm independent of the customers who use it. The policy pattern enables these algorithms to change independently when the client calls them, which is a behavioral pattern
There are three objects in the policy mode :
- Environment Object (context): a reference to an interface or abstract class defined in an abstract policy is implemented in this class.
- Abstract Policy Object (strategy): It can be implemented by an interface or an abstract class.
- specific Policy object (concretestrategy): It encapsulates different algorithms for implementing the same functionality
defining abstract Policy Objects
package Com.strategypattern; /** * @author Yyx October 11, 2017 * Policy Interface */ public interface discountstrategy { /** * calculate Price * * @param commodity Price * @return */ public double discountprice ( double Commodityprice);
defining specific policy Objects
PackageCom.strategypattern;/** * @authorYyx October 11, 2017*/ Public classIsmemberdiscountstrategyImplementsDiscountstrategy {@Override Public DoubleDiscountprice (DoubleCommodityprice) {System.out.println ("10% Discount for members"); returnCommodityprice * 0.9; }} PackageCom.strategypattern;/** * @authorYyx October 11, 2017*/ Public classNotmemberdiscountstrategyImplementsDiscountstrategy {@Override Public DoubleDiscountprice (DoubleCommodityprice) {System.out.println ("No discount for non-members"); returnCommodityprice; }}
Defining Environment Objects
PackageCom.strategypattern;/** * @authorYyx October 11, 2017*/ Public classCalculatedprice {PrivateDiscountstrategy Discountstrategy; PublicCalculatedprice (Discountstrategy discountstrategy) {Super(); This. Discountstrategy =Discountstrategy; } Public voidQuoteDoubleCommodityprice) {System.out.println ("Your Discounted price is:" + This. Discountstrategy.discountprice (Commodityprice)); }}
Defining Test Classes
PackageCom.strategypattern;/** * @authorYyx October 11, 2017*/ Public classPatterntest { Public Static voidMain (string[] args) {//Select and create the policy object you want to useDiscountstrategy Discountstrategy =NewIsmemberdiscountstrategy (); //Create an environmentCalculatedprice Calculatedprice =NewCalculatedprice (discountstrategy); //Calculate PriceCalculatedprice.quote (300); }}
Advantages of the policy model
(1) The Strategy mode provides a way to manage the associated algorithm families. The hierarchy structure of a policy class defines an algorithm or a family of behaviors. Proper use of inheritance can move common code into the parent class, thus avoiding code duplication
(2) Use the policy mode to avoid using multiple conditional (IF-ELSE) statements. Multiple conditional statements are difficult to maintain, and it mixes the logic of which algorithm or behavior is taken with the logic of the algorithm or the action, all listed in a multi-conditional statement, more primitive and backward than the method of using inheritance.
Disadvantages of the policy model
(1) The client must know all of the policy classes and decide for itself which policy class to use. This means that the client must understand the differences between these algorithms in order to select the appropriate algorithm classes at the right time. In other words, the policy pattern applies only to situations where the client knows the algorithm or behavior
(2) Since the policy model encapsulates each specific policy implementation as a class, the number of objects will be considerable if there are many alternative strategies.
Java Common design pattern--Strategy mode