2.2 rule mode (5.9)

Source: Internet
Author: User

To some extent, the Strategy mode is not worth mentioning, because the Difficulty coefficient of learning and understanding it is 0. Isn't it a class level problem.

Therefore, let's add a point: Method objectization-encapsulate methods as types.

In addition, from the perspective of rebuilding the branch structure, the policy mode and [1.3.2 factory method mode (3.3)] and [3.2 state mode (5.8)] are triplets.

1. Define the interface of the parent class, and implement the sub-class.

When traveling, you can choose from a variety of different travel modes, such as taking a car/train/plane, riding a bicycle or walking. Customs duties are imposed on different commodities at different tax rates ...... The way to do things isPolicy or policy. In the source code, a method body is often called an algorithm. Therefore, a method body that encapsulates the method for doing things is calledA specific policy.

The policy mode defines and encapsulates an algorithm set at a class level: the parent class defines an interface, and the subclass provides an implementation. 【Strategy pattern: defines a series of algorithm classes, encapsulates each algorithm, and allows them to replace each other. The policy pattern allows algorithms to change independently of customers who use it, it is also called a policy ).]

For example, [Introduction to programming · 11 sorting] describes various sorting algorithms: selection, insertion, and exchange sorting. To test various algorithms conveniently, the abstract class algorithm. Sorting. intsort encapsulates the abstract method int [] Sort (INT [] ARR). Various sorting algorithms are designed as subclasses of intsort. Of course, in addition to policies, intsort also provides tool methods, such as public static void swap (INT [] arr, int one, int two)-swap two elements of the array.

From the perspective of encapsulating an algorithm set, the policy mode is to design a class hierarchy. Therefore, to learn and understand it, the difficulty coefficient is 0. The policy mode is simple to use.Polymorphism Technology. Because the policy mode simply uses the polymorphism technology, the policy mode becomes a basic technology commonly used by other models. It can also be said that the policy model has no technical content.

2. Separating policies from the Environment

Take sorting as an example,

Package method. strategy; public class context {public void test (string S, int I, Int J, int [] array) {If (S = "") M1 (I, j); If (S = "insert") m2 (array); If (S = "Switch") m3 (array, I );} private void M1 (int I, Int J) {} private void m2 (INT [] array) {} private void m3 (INT [] array, int I ){}}
If you test various algorithms based on the branch structure, the Code does not follow the OCP when the branch may be added. The obvious solution is to design the abstract method of mystrategy. M (int I, Int J, int [] array)Encapsulate various algorithms M1, M2, and M3. In actual programming, the original M1 (), M2 (), m3 () Different parameters may be required.. Therefore, when designing an abstract method M (), while ensuring that the return value type is determined, M () must be able to ensure that all specific policy classes can obtain all the data they need, therefore, when defining the M () interface, its parameter list is the largest set of data required for a specific policy class. The largest set means that it is preferred to have more than one-some specific policy classes do not use the data provided by the parameter list, but some specific policy classes do not have the required data. Of course, the parameters are simple only for sorting.
Package method. Strategy; public interface mystrategy {public abstract int [] Sort (INT [] ARR);} // subtype of mystrategy, omitted
Note that we have the dependency injection tool import tips. IOC,

Package method. strategy; import tips. IOC; public class context {private mystrategy s; // dependency injection public void setstrategy (mystrategy s) {This. S = s;} public static void test (INT [] ARR) {mystrategy ST = (mystrategy) IOC. getObject ("insert"); int [] array = ST. sort (ARR );}}
.

3.

2.2 rule mode (5.9)

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.