In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "what is the strategy model in java". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the strategy model in java"?
1. Definition of pattern
Suppose you now want to implement a simplified quote management to achieve the following functions:
1. Quote full price for ordinary customers or new customers
two。 For the prices quoted by regular customers, a uniform discount of 5%
3. For the prices quoted by major customers, there is a uniform discount of 10%
Quote different prices for different personnel
How can it be implemented so that the algorithm for calculating quotation in the price class can be easily maintainable, extensible and dynamically switched?
Definition of policy pattern:
Define a series of algorithms, encapsulate them one by one, and make them interchangeable so that the algorithm can be changed independently of the customers who use it
2. UML diagram
Strategy: policy interface, which is used to constrain a series of specific policy algorithms. Context uses this interface to call the algorithms defined by the specific policy implementation.
ConcreteStrategy: specific policy implementation, that is, specific algorithm implementation
Context: context, responsible for interacting with specific policy classes, usually holding a real policy implementation
Code:
3. Grinding design mode
1) the function of the strategy pattern: separate the specific algorithm implementation from the specific business processing, implement them into a separate algorithm class, thus form a series of algorithms, and let these algorithms replace each other.
2) the relationship between Context and Strategy
The policy implementation object can also obtain the required data from the context and pass the context as a parameter to the policy implementation object.
3) Fault-tolerant recovery mechanism
For example, using the policy mode, logging to the database and logging to the file as two logging strategies, and then dynamically switching as needed during the run
4) Strategy pattern combined with template method pattern
It is found that there are common functions in the implementation of a series of algorithms, but they are different in some local steps.
5) the essence of strategy pattern: separation algorithm, choice and implementation.
At this point, I believe you have a deeper understanding of "what is the strategy model in java". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.