Java design pattern--------mode of action (4)

Source: Internet
Author: User

[This is my own study notes, welcome reprint, but please specify the source:http://blog.csdn.net/jesson20121020]

  The above 3 sections are implemented from the initial alignment of the array to the last can be all types can be defined according to the need to define their own comparator to sort, the different comparators, the implementation of different algorithms, and separate into a class, in the application, can be flexibly selected according to the actual needs, scalability is strong, In fact, this is the use of strategic mode.

The policy mode strategy is a pattern of object behavior patterns in design mode, mainly defining a series of algorithms that encapsulate these algorithms into separate classes, such as the comparators in the above sections. The strategy mode is widely used, such as the school traffic change chart, there may be two ways to achieve, one is the stitches, one is a pie-shaped representation, as in our above 3 section of the cat type of height comparator, weight comparator. By defining different strategies, we achieve the ability to switch the algorithm freely during the program's operation.

The core part of the actual overall policy pattern is the use of abstract classes or interfaces, and the use of policy patterns can be very small and fast to modify when users need to change.

There is a certain similarity between the strategy mode and the factory model. The tactical weapon is relatively simple and easy to understand, and can be switched freely at run time, while the Factory mode is primarily used to create objects.

  

The strategy mode is suitable for the following situations:

1. Save the file in a different format

2. Compressing files with different algorithms

3. Capturing images with different algorithms

4. Output graphs of the same data in different formats, such as curves, bar, etc.

......

In short, in a word, the strategy model is suitable for the situation of "to achieve the same function with different algorithms, different formats, etc." in actual development. The decision of the policy mode is in the user, the system itself provides the realization of different algorithms, and encapsulates the various algorithms. Therefore, the strategy mode is more used in the algorithm decision system, the external users only need to decide which algorithm to use.

Java design pattern--------mode of action (4)

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.