What is a policy model?
Define the algorithm families, encapsulate each other, so that they can be replaced with each other, this pattern allows the algorithm to change independently of the customers using the algorithm.
What are the benefits of the strategy model?
You can dynamically change the behavior of an object.
Design Principles
1. Package changes (identify where changes may be needed in the application, separate them out, and do not mix with code that does not need to change) 2. Multi-use combination, less inheritance 3. Programming for interfaces, not for implementation
The policy pattern belongs to the object behavior pattern, which is mainly for a set of algorithms, encapsulating each algorithm in a separate class with a common interface, so that they can be replaced with each other. The policy pattern allows the algorithm to change without affecting the client. Typically, a policy pattern is used when an application needs to implement a particular service or feature, and the program is implemented in a variety of ways.
There are three objects in the policy mode
1. Environment object: This class implements a reference to an interface defined in an abstract policy or an abstract class 2. Abstract policy object: it can be implemented by an interface or an abstract class 3. Specific policy object: it encapsulates different algorithms for implementing the same function
Using the policy model to build the application, you can choose different algorithms to implement the functions of the application according to the user configuration and other content. The specific selection has the environment object to complete. This approach avoids the code clutter caused by the use of conditional statements, improving application flexibility and rationality.
Example Analysis
Design an action adventure game in which there are different characters and different weapons, each weapon attack style and lethality is not the same, each character has combat skills, in the game can change weapons
Depending on the scenario, you can create a base class role, which contains the combat fight () method, and also contains a method for setting the behavior of the weapon Setweapon (); (Environment object) creates a weapon behavior interface that includes the use of weapon methods, different weapons to implement the interface ; (abstract policy object) different roles set different weapon behavior subclass object (Specific policy object)
1.Role class
Package Com.pattern.strategy.role;import Com.pattern.strategy.weapon.iweaponbehavior;public abstract class Role { Iweaponbehavior Mweaponbehavior; public void Fight () { if (mweaponbehavior! = null) { mweaponbehavior.useweapon (); } } public void Setweaponbehavior (Iweaponbehavior weaponbehavior) { mweaponbehavior = Weaponbehavior; } public abstract void Move ();
2. Specific role class--king,queen.
Package Com.pattern.strategy.role;import Com.pattern.strategy.util.tools;public class King extends role { @ Override public void Move () { tools.print ("King move");} }
Package Com.pattern.strategy.role;import Com.pattern.strategy.util.tools;public class Queen extends role { @ Override public void Move () { tools.print ("Queen move");} }
3. Abstract policy Object Interface-iweaponbehavior
Package Com.pattern.strategy.weapon;public interface Iweaponbehavior {public void useweapon ();
4. Specific policy object class, Axebehavior (using axe), Knifebehavior (using dagger), Swordbehavior (using sword)
Package Com.pattern.strategy.weapon;import Com.pattern.strategy.util.tools;public class Axebehavior implements Iweaponbehavior { @Override public void Useweapon () { tools.print ("Axebehavior");} }
Package Com.pattern.strategy.weapon;import Com.pattern.strategy.util.tools;public class Knifebehavior implements Iweaponbehavior { @Override public void Useweapon () { tools.print ("Knifebehavior");} }
Package Com.pattern.strategy.weapon;import Com.pattern.strategy.util.tools;public class Swordbehavior implements Iweaponbehavior { @Override public void Useweapon () { tools.print ("Swordbehavior");} }
5. Tool tools and test class Strategymode
Package Com.pattern.strategy.util;public class Tools {public static void print (String info) { SYSTEM.OUT.PRINTLN (info);} }
Package Com.pattern.strategy;import Com.pattern.strategy.role.king;import Com.pattern.strategy.role.queen;import Com.pattern.strategy.role.role;import Com.pattern.strategy.weapon.axebehavior;import Com.pattern.strategy.weapon.knifebehavior;import Com.pattern.strategy.weapon.swordbehavior;public Class Strategymode {public static void Main (string[] args) { role role = new King (); Role.setweaponbehavior (New Axebehavior ()); Role.fight (); Role.move (); Role.setweaponbehavior (New Swordbehavior ()); Role.fight (); role = new Queen (); Role.setweaponbehavior (New Knifebehavior ()); Role.fight (); Role.move (); }}
6. Running Results
Axebehavior
King move
Swordbehavior
Knifebehavior
Queen move
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The strategy mode of Java design pattern