The 8th chapter of the design pattern-strategy mode (Java implementation)
"Year ago Big bargain, Ah, now Barber rushed 500 to get 300, rushed 1000 to send 500." Fish brother hurriedly charge Ah, haircut this thing basic one months a back, very affordable AH. However, the boss of the barber shop is so silly, rushed 1000 to send 500, than two times 500, so you can send 600. "It only means that you are not very stupid, but also not smart." "Ah?" Did I think wrong? "This is a strategy, strategy, understand?" If he is 1000 to send 700, you are not a big possibility to punch 500? And not 1000, but if so, in the "smart", yes, yes, that's what you do, two times 500 means that the boss of the IQ of contempt, then, you will rush 1000, so, you understand? "Well, I am still a strategic model, this little bashi to deceive me, embarrassing lost." "(Editor's note: Boy, you still have the pattern Tucson broken ah.) )
Self-introduction of strategy mode
Batting practice, is a relatively simple pattern, defined as follows:
Define a family of algorithms, encapsulate each one, and make them interchangeable. To translate is to say: Define a set of algorithms, or encapsulate each algorithm, and make them interchangeable. The following is a concrete class diagram:
Because the introduction of the diagram is already very detailed, so I will not repeat, I use object-oriented inheritance and polymorphism mechanism, it is quite easy to understand ~
Self-analysis of the strategy model
When it comes to the pros and cons, well, first of all, say the disadvantages:
- If the customer wants to choose a suitable strategy, then it is important to understand all the strategies and the differences between the various policies, this time will have to burst the policy specific implementation. However, the magic of a tall foot (as if there is a bit wrong 、、、 no matter, continue), this shortcoming can be combined with the previous factory method model to compensate.
- The communication overhead between the strategy and the context, because the strategy algorithm is not necessarily all used to get, so will bring a certain waste.
- Increased the number of objects.
Advantage:
- The algorithm can be switched freely.
- Avoid using multiple judgment statements to eliminate some conditional statements.
- Scalability is good.
- Different strategies can be selected according to different time/space requirements.
Implementation of the Strategy model
In other words, the mule is horse-drawn out for a walk, in that case, the batting practice code out, please wear sunglasses. The following code is based on the barber shop recharge activities to achieve. The first is the abstract policy interface:
1 Public Interface strategy{2 // Each recharge method is an algorithm 3 Public void Recharge (); 4 }
Then there is the strategy of rushing 500 to get 300:
1 Public class Implements strategy{2 Public Viod Recharge () {3 System.out.println ("Punch 500 to send"); 4 }5 }
Then there is the specific implementation of the strategy of rushing 1000 to 500:
1 Public class Implements strategy{2 Public Viod Recharge () {3 System.out.println ("Punch 1000 to send"); 4 }5 }
Next, you need a wrapper class to put these policies, easy to use, that is, the context wrapper class in the class diagram, the code is as follows:
1 Public classcontext{2 //constructor, which preferential policy do you want to start with?3 Privatestrategy strategy;4 PublicContext (Strategy strategy)5 {6 This. Strategy =strategy;7 }8 9 //Start Preferential policyTen Public voidRecharge () { One This. Strategy.recharge (); A } -}
View Code
Pass the preferential policy through the constructor function, then use the Recharge method to execute the related strategy method. What, you don't understand? All right, okay, I got you, look batting practice. Write a Test class:
1 Public classy{2 3 Public Static voidMain (string[] args) {4 context context;5 //start with the first offer6Context =NewContext (NewSt1 ());7 Context.recharge ();8 9 //start with a second offerTenContext =NewContext (NewSt2 ()); One Context.recharge (); A } -}
View Code
Well, that's the end of the implementation. Here's a look at the application scenario.
The application scenario of the Strategy pattern
You might consider using the policy mode when the following conditions exist:
- Many of the related classes are simply behavior-specific.
- You need to use different variants of an algorithm.
- A class defines a variety of behaviors.
- You need to mask the algorithm rules.
PS: This blog welcome forwarding, but please specify the blog address and author ~
Blog Address: http://www.cnblogs.com/voidy/
Blog: http://voidy.gitcafe.com
<. ))) ≦
The 8th chapter of the design pattern-strategy mode (Java implementation)