I. Overview of the pattern
The Strategy mode (strategy pattern) is similar in shape to the state mode, but somewhat different in intent. The intention is to make these algorithms interchangeable and provide a way to select the most appropriate algorithm.
In my application of OOP design process Evolution (iii) This article applies to the Strategy model, in the book's rent calculation on a variety of situations, each type of book rental is not the same, and from the point of view of the user, the different types of users rent charge is not the same, see the following analysis:
Computer Books: Members of the rental play 50 percent, average customer rental hit 60 percent.
Novel books: Members on loan 60 percent, average customer rental hit 80 percent.
Life-Class Books: Members of the rental play 90 percent, average customer rental hit 90 percent.
From the above analysis, it is very complicated to calculate the rent, because of such a complex discount algorithm, so that the price calculation problem needs to be solved systematically.
Bookstore operating for a period of time found that the book rental of Life class is very small, perhaps because of the rent, very few people to rent, so the boss decided to lower rents, the life of books in accordance with the rent of the computer class charge. What about this time? Is there already existing computer books rent calculation strategy, at this time we just need to live to book rental calculation strategy for computer books rent calculation strategy will be OK. With this analysis, the policy pattern (strategy) is the pattern for solving the problem, which is defined as "preparing a set of algorithms and encapsulating each one so that they can be interchanged." "
Second, the pattern definition
Definition: "Prepare a set of algorithms and encapsulate each algorithm so that they can be interchanged." "
Iii. Patterns of Intent
The intent of the policy pattern is to encapsulate each algorithm in a separate class with a common interface for a set of algorithms so that they can be replaced by each other.