Java design mode, java design mode pdf
[This article is my own learning notes. You are welcome to repost it, but please note the Source: http://blog.csdn.net/jesson20121020]
The above three sections enable sorting of all types from the initial sorting of integer arrays to the final sorting of all types by defining their own comparator as needed, different algorithms are implemented, and they are individually divided into a class. You can choose based on your actual needs and have high scalability. In fact, the policy mode is used here.
Strategy pattern Strategy is an object behavior pattern in design patterns. It mainly defines a series of algorithms and encapsulates these algorithms into separate classes one by one, such as the comparator in the preceding three sections. The rule mode is widely used. For example, the school traffic change chart may have two implementation methods: Split the line, and pie-like representation. For example, the Cat-type height comparator, weight comparator, in the preceding three sections. By defining different policies, we can freely switch the algorithm during the program running.
The core part of the actual policy mode is the use of abstract classes or interfaces. When you need to change the policy mode, the amount of code modified is small and fast.
The policy mode is similar to the factory mode. The policy mechanism is relatively simple and easy to understand, and can be switched freely at runtime, while the factory mode is mainly used to create objects.
The rule mode is applicable to the following scenarios:
1. save files in different formats
2. compress files with different algorithms
3. capture images using different algorithms
4. output images of the same data in different formats, such as curves and bars.
......
In a word, the policy mode is suitable for the situation that "the same function must be implemented using different algorithms and formats" encountered in actual development. The decision of policy mode lies in the user. The system itself provides implementation of different algorithms and encapsulates various algorithms. Therefore, policy modes are generally used in algorithm decision making systems. external users only need to decide which algorithm to use.