In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to understand the strategic pattern of Java design pattern". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to understand the strategic pattern of Java design pattern".
I. what is the strategic model?
The policy pattern defines a series of algorithms and encapsulates each algorithm so that they can replace each other, and the changes in the algorithm will not affect the customers who use the algorithm. Need to design an interface to provide a unified method for a series of implementation classes, multiple implementation classes implement the interface, design an abstract class (optional, belongs to the auxiliary class), and provide auxiliary functions.
The policy pattern defines and encapsulates a series of algorithms, which are interchangeable, that is to say, they have something in common, and their commonness is reflected in the behavior of the policy interface, and in order to achieve the purpose of the last sentence, that is to say, to let the algorithm change independently of the customer who uses it, we need to make the client rely on the policy interface.
A very simple explanation is that in our development process, we often encounter a large number of if...else or switch...case statements. When these statements are only for the purpose of shunting, which has nothing to do with the business logic, then we can consider using the strategy pattern.
Second, the structure of the strategy model
This model involves three roles:
Context (Context) role: holds a reference to Strategy.
Abstract Policy (Strategy) role: this is an abstract role, usually implemented by an interface or abstract class. This role gives the required interfaces for all specific policy classes.
Specific strategy (ConcreteStrategy) role: wraps related algorithms or behaviors
Third, the application scenario of the strategy model
For example, shopping malls offer promotions-20% discount, 50 for 200, and gifts for 1000. This promotion is a strategy.
To give another example, the tactics in dota, the four guarantees of life, the three pseudo-nuclear system, the propulsion system, the big recruitment system, and so on, are all a strategy.
Application scenarios:
1. Multiple classes only show different behaviors. You can use Strategy mode to dynamically select the specific behavior to be executed at run time.
2. Different strategies (algorithms) need to be used in different situations, or the strategy may be implemented in other ways in the future.
3. Hide the implementation details of specific strategies (algorithms) from customers, which are completely independent of each other.
Fourth, the advantages and disadvantages of the strategy model
Advantages:
1. The structure is clear, the policy is separated into separate classes, "replacing the traditional if else" 2, the code coupling is reduced, the security is improved, and the details of each policy are blocked.
Disadvantages:
1. The client must know all the policy classes, otherwise you don't know which policy to use, so the policy pattern is suitable for knowing all policies in advance. 2. The number of policy classes increases (each policy class has little reusability. If you need to add algorithms, you can only add new classes. 5. The similarities and differences between the policy pattern and the simple factory pattern
As mentioned in the previous article, delivery address: in-depth understanding of design patterns (2): simple factory patterns
VI. The realization of the strategy model
The Strategy class, which defines the common interface for all supported algorithms
/ / Strategy class that defines the common interface abstract class Strategy {/ / algorithm method public abstract void AlgorithmInterface ();} for all supported algorithms
OncreteStrategy, which encapsulates specific algorithms or behaviors, inherits from Strategy
/ / algorithm A class ConcreteStrategyA: Strategy {public override void AlgorithmInterface () {Console.WriteLine ("implementation of algorithm A");}} / / algorithm B class ConcreteStrategyB: Strategy {public override void AlgorithmInterface () {Console.WriteLine ("implementation of algorithm B") C class ConcreteStrategyC: Strategy {public override void AlgorithmInterface () {Console.WriteLine ("implementation of algorithm C");}}
Context, configured with a ConcreteStrategy, maintains a reference to the Strategy object
/ / Context, configured with a ConcreteStrategy, maintains a reference to the Strategy object class Context {Strategy Strategy; public Context (Strategy Strategy) {this.Strategy = Strategy;} / / context interface public void ContextInterface () {Strategy.AlgorithmInterface ();}}
Client code
Static void Main (string [] args) {Context Context; Context = new Context (new ConcreteStrategyA ()); Context.ContextInterface (); Context = new Context (new ConcreteStrategyB ()); Context.ContextInterface (); Context = new Context (new ConcreteStrategyC ()); Context.ContextInterface (); Console.Read () 7. The combination of strategic model and simple factory model
Modified Context
Class Context {Strategy Strategy=null; public Context (string type) {switch (type) {case "A": ConcreteStrategyA A = new ConcreteStrategyA (); Strategy= A; break; case "B": ConcreteStrategyB B = new ConcreteStrategyB () Strategy = B; break; case "C": ConcreteStrategyC C = new ConcreteStrategyC (); Strategy = C; break;}} / / context interface public void ContextInterface () {Strategy.AlgorithmInterface () }}
Modified client code
Static void Main (string [] args) {Context Context = new Context ("here is the corresponding algorithm type string"); Context.ContextInterface (); Console.Read ();}
Comparing the differences before and after the transformation, it is not difficult to see that before the transformation, the client needs to know two classes, Context and ConcreteStrategy. With the combination of the policy pattern and the simple factory pattern, the client only needs to know a class Context, which reduces the coupling.
VIII. Implementation of policy enumeration
We can use enumerations to implement all the above functions and three different roles in one class. Let's take a look at how to implement the policy pattern through enumerations.
Public enum Calculator {ADD ("+") {public int exec (int a, int b) {return aquib;}}, SUB ("-") {public int exec (int a, int b) {return amurb;}} Public abstract int exec (int a, int b); / / operator private String value = ""; private Calculator (String value) {this.value = value;} public String getValue () {return value;}}
In the enumeration class, the abstract method defined is like the interface before that time, each enumeration ADD SUB is equivalent to a concrete implementation class (policy role), and the whole enumeration class is the repackaged role of the policy.
Thank you for your reading, the above is the content of "how to understand the strategic pattern of Java design pattern". After the study of this article, I believe you have a deeper understanding of how to understand the strategic pattern of Java design pattern, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.