How do I learn the algorithm strategy
In daily development we often have to use different processing logics for different business units in the same context. To take a simple example, when we make purchases and billing on ecommerce websites, different levels of preference often apply to different levels of our members. From a system implementation point of view, we need to decide whether to use them depending on the different requirements during operation of a particular algorithm strategy. Strategy mode can better solve this problem. This blog explains the strategy mode.
The strategy pattern defines a set of algorithms, encapsulates each algorithm and allows them to be substituted for one another so that the algorithm can change regardless of the customers who use it.
The class diagram of the strategy pattern is as follows:
As can be seen from the figure above, the strategy model consists of three components:
context Keep a strategy reference;
Abstract strategy (strategy) Abstract roles, usually implemented by an interface or abstract class;
Concrete strategy (ConcreteStrategy) Packaged related algorithms or behaviors.
Let's take the e-commerce preference guidelines mentioned in the opening chapter as an example and consider the typical implementation code of the strategy model. First we have to define a strategy interface to calculate the discount amount
And three implementation classes that represent three different billing strategies
We also need to define a consumer business unit that will play the environmental role mentioned above
Finally, let's test the code functionality
The results are as follows
- Who is the stronger Magneto or Ebony Maw
- What Causes Posterior Ankle Impingement Syndrome
- What is the typical Chinese
- Where can I buy a domesticated fox?
- Are Maxwell's equations exact or only approximate?
- Where can I work as an industrial engineer?
- How many tourists visit Armenia each year
- Think apples grow on trees
- How old is the Slenderman legend
- How much can I earn with Champcash
- Why does India not have hostels
- Which is the cheapest flight to Dubai
- What does it mean?
- When was the Thai script created
- How do the Pomeranians smell
- Mineral water is beneficial for thinning hair
- Can a dictatorship be feminist
- What does RRR mean in medical terms
- How can I set my sleep schedule
- Can I die breathing
- Is Jewish a denomination or a nationality
- What do polyglots think of Duolingo
- Who likes tight clothes
- What are some examples of navigation maps