Interpreting design Patterns: Strategic patterns (Strategy pattern)

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.