Design Pattern Learning 1-strategy pattern (Strategy)

Source: Internet
Author: User

Hey hey, you don't know how to implement this strategy. It seems that it is not very difficult to learn the design model.

Rule Mode

Official explanation: strategy is an object behavior pattern in design pattern, mainly defining a seriesAlgorithmAnd encapsulate these algorithms into separate classes.

We have achieved the goal of freely switching algorithms during operation.

In reality, the core part of strategy is the use of abstract classes. The strategy mode can be used with a small amount of modifications and fast modification when users need to change.

There are some similarities between strategy and factory. Strategy is relatively simple and easy to understand, and can switch freely at runtime. Factory focuses on creating objects.

Strategy is applicable to the following scenarios:

1. save files in different formats;

2. Use different algorithms to compress files;

3. capture images using different algorithms;

4. output images of the same data in different formats, such as curves or Block Bar

My personal understanding: It's so handsome ...... In fact, it seems that it is not very complicated, that is, to abstract the behavior (algorithm) of the object, and then assemble it with a class when other objects are used, so that specific objects can use their favorite methods to make love.

Finally, let's go to the previous example, "duck story", first:

Click to download source code

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.