One: definition of the policy model
---> is a relatively simple model, also called the policy model
---> Define a set of algorithms that encapsulate each algorithm and make them interchangeable.
II: Three roles of the strategy model
Context wrapper role
---> It is also called context role, which plays a role in connecting the package, shielding the high-level module from the direct access to the strategy, the algorithm, and encapsulates the possible changes.
Strategy Abstract policy roles
---> Strategy, an abstraction of the algorithm family, typically an interface that defines the methods and properties that each policy or algorithm must have
Concretestrategy Specific policy roles
---> Implement operations in an abstract policy that contains specific algorithms.
Three: Advantages and disadvantages of the strategy model
Advantages
The algorithm is free to switch
---> This is defined by the policy model itself, as long as the implementation of the abstract strategy, it becomes a member of the policy family, encapsulated by the role of encapsulation, to ensure that the external provide a "free to switch" policy.
Avoid using multiple criteria to judge
---> If there is no policy model, what would it look like? A strategy family has 5 strategy algorithms, one will use a strategy, one will use B strategy, how to design it? Multiple conditional statements are not easy to maintain, and the probability of error is greatly enhanced. After using the policy mode, it is possible for other modules to decide what strategy to adopt, and the access interface provided by the policy family is the encapsulation class, which simplifies the operation and avoids the conditional statement judgment.
Good extensibility
---> It doesn't even have to be the merit of it, because it's too obvious. Adding a strategy to an existing system is too easy, as long as the interface is implemented, the other is not modified, similar to a reusable plug-in, which greatly conforms to the OCP principle
Disadvantages
Increased number of policy classes
---> Each strategy is a class that is less likely to be reused and has an increased number of classes.
All policy classes require external exposure.
---> The upper module must know what strategies to use, which is contrary to the Dimitri rule, and I just want to use a strategy, so why should I understand this strategy? What's the point of your encapsulation class? This is a disadvantage of the original strategy model, fortunately, We can use other modes to fix this flaw, such as factory method mode, proxy mode, or the enjoy meta mode.
Three: The usage scenario of the policy mode
Multiple classes have only a slightly different scenario than the algorithm or behavior.
The algorithm requires a free-switching scenario.
---> For example, the choice of the algorithm is determined by the user, or the algorithm is always evolving, especially in some industries that stand at the forefront of technology, and even business experts cannot guarantee you how long such a system rule can exist, in which case the strategy mode is your best helper.
scenarios where the algorithm rules need to be masked.
---> Now the technology is developing quickly, the memory of the human brain is limited (for the time being limited), too many algorithms you just need to know a name to be able to pass the relevant numbers come in, feedback an operation result, everything.
Four: Considerations for the Strategy model
---> If a policy family in the system has more than 4 specific policies, consider using mixed mode (such as the wrapper to get a map, the user to pass an identity to identify the corresponding policy), to solve the problem of policy expansion and external exposure, or the future system maintenance will become a hot potato , nobody wants to answer.
---> Strategy mode is a very simple pattern. It is used very much in the project, but it is less used alone because it has a fatal flaw: All strategies need to be exposed to make it easier for the client to decide which policy to use. For example, in the example of Zhao Yun, actually do not know which strategy to use, he only know to dismantle the first bag, and do not know is backdoor this ingenious scheme. Yes, Zhuge Liang has stipulated in the appropriate scenario to open the designated balloon, our strategy model is only to achieve the management of the brocade sac, but we do not strictly define "appropriate scenario" to open "appropriate", in the actual project, we generally through the factory method pattern to implement the policy class declaration, Readers can refer to the mixed mode.
V: Example of a strategy model
"1" Policy abstract class
1 PackageCom.yeepay.sxf.template13;2 /**3 * Interface for the operation of the policy4 * @authorSXF5 *6 */7 Public InterfaceStrategy {8 //algorithms for strategy mode9 Public voiddosomething ();Ten}
View Code
"2" policy 1
1 PackageCom.yeepay.sxf.template13;2 /**3 * Strategy algorithm 14 * @authorSXF5 *6 */7 Public classConcreteStrategy1Implementsstrategy{8 9 @OverrideTen Public voiddosomething () { OneSystem.out.println ("Concretestrategy1.dosomething (Strategy algorithm 1)")); A } - - the}
View Code
"3" Policy 2
1 PackageCom.yeepay.sxf.template13;2 /**3 * Strategy algorithm 24 * @authorSXF5 *6 */7 Public classConcreteStrategy2Implementsstrategy{8 9 @OverrideTen Public voiddosomething () { OneSystem.out.println ("Concretestrategy2.dosomething (Strategy algorithm 2)")); A } - - the}
View Code
"4" policy wrapper
1 PackageCom.yeepay.sxf.template13;2 /**3 * Policy Wrapper4 * High-level module invocation is very simple,5 * know which strategy to use, produce its object, and then put it in the wrapper role to complete the task.6 * @authorSXF7 *8 */9 Public classContext {Ten One PrivateStrategy strategy=NULL; A - PublicContext (Strategy strategy) { - This. strategy=strategy; the } - //Kam SAC Performers - Public voiddoanything () { - strategy.dosomething (); + } -}
View Code
"5" Client test
1 PackageCom.yeepay.sxf.template13;2 /**3 * Client4 * @authorSXF5 *6 */7 Public classClient {8 9 Public Static voidMain (string[] args) {Ten //to declare a specific policy OneStrategy strategy=NewConcreteStrategy1 (); A //encapsulate this policy inside the wrapper -Context context=NewContext (strategy); - //Wrapper execution Policy error the context.doanything (); - //The wrapper hides a group of policies that execute different policies based on different conditions. - } -}
View Code
Design pattern of Zen design pattern-Strategy mode