Java rule Mode

Source: Internet
Author: User

Definition:Define a set of algorithms, encapsulate each algorithm, and make them interchangeable.

Type:Behavior mode

Class diagram:

Policy mode encapsulates algorithms and encapsulates a series of algorithms into corresponding classes. These classes implement the same interfaces and can be replaced with each other. In the behavior pattern mentioned above, there is also a pattern that focuses on the encapsulation of algorithms-the template method pattern. You can see it in the comparison class diagram, the difference between the policy mode and the template method mode is simply a separate encapsulation class context. The difference between the policy mode and the template method mode is: in the template method mode, the subject that calls an algorithm is in the abstract parent class, while in the Policy mode, the subject that calls the algorithm is encapsulated into the encapsulation class context. The abstract policy strategy is generally an interface, the purpose is to define a specification, which generally does not contain logic. In fact, this is only a general implementation, but in actual programming, because each specific policy implementation class inevitably has some same logic, in order to avoid repeated code, abstract classes are often used to assume the role of strategy and encapsulate public code. Therefore, in many application scenarios, the shadow of the template method mode is usually seen in the Policy mode.

 

Structure of Rule Mode

  • Encapsulation class:It is also called context. It is used to encapsulate policies twice to avoid direct calls from high-level modules to policies.
  • Abstract policy:An interface is usually used. When repeated logic exists in each implementation class, abstract classes are used to encapsulate this part of public code, the rule mode looks more like the template method mode.
  • Specific policy:A specific policy role is usually assumed by a group of classes that encapsulate algorithms. These classes can be freely replaced as needed.

 

Advantages and disadvantages of Rule Mode

The rule mode has the following advantages:

  • The policy classes can be switched freely. Because the policy classes are implemented from the same abstraction, they can be switched freely.
  • It is easy to expand. adding a new policy is very easy for the policy mode. It can be expanded without changing the original code.
  • Avoid using multiple conditions. If no policy mode is used, all algorithms must be connected using conditional statements to determine which algorithm to use, as we mentioned in the previous article, it is very difficult to maintain multiple conditions.

There are two main disadvantages of the Rule mode:

  • Maintenance of various strategy classes will bring additional costs to development, and everyone may have experience in this regard: Generally, there are more than five strategy classes, which is a headache.
  • All policy classes must be exposed to the client (caller), because which policy is determined by the client, the client should know what policies there are and the differences between various policies, otherwise, the consequences are very serious. For example, there is a Sort Algorithm policy mode that provides three algorithms: Fast sort, bubble sort, and select sort. Before using these algorithms, the client, do you need to understand the applicability of these three algorithms first? For another example, if the client wants to use a container that implements linked lists and arrays, does the client need to understand the differences between linked lists and arrays? In this regard, it is against the dimit law.

 

Applicable scenarios

When designing an object-oriented model, you must be familiar with the policy mode, because it is essentially an inheritance and polymorphism in the object-oriented model. After reading the general code of the Policy mode, I think, even if I have never heard of the Rule mode before, I must have used it during development? In either of the following situations, you can consider using the Policy mode,

  • The main logic of several classes is the same, but the algorithm and behavior of some logics are slightly different.
  • There are several similar behaviors, or algorithms. The client needs to dynamically decide which one to use. You can use the policy mode to encapsulate these algorithms for the client to call.

Policy mode is a simple and common mode. We often use it intentionally or unintentionally during development. Generally, policy mode is not used independently, there are many mixed use cases with the template method mode and factory mode.

 

Use Cases:

Liu Bei is about to marry his wife in Jiangdong. Before that, Zhuge Liang gave Zhao Yun (Best Man) three tips, saying that the split by Tianji can solve the tough problem. Hey, let alone it solves the big problem, at the end, Zhou Yu accompanied his wife and gave up again. Let's first look at what this scene looks like.

Let's first talk about the elements in this scenario: three tips, one tip, and one Zhao Yun. The tips are given by Liang GE. They are put in the tips, which are commonly known as tips, then Zhao Yun is a working person, taking out tips from the tip, executing, and winning. How can we use Java programs to demonstrate this?

 

Package com. yangguangfu. Strategy;/***** @ author trygf521@126.com: AFU * First defines a policy interface, which is the three interfaces Zhuge Liang gave Zhao cloud. */Public interface istrategy {// each tip is an executable algorithm. Public void operate ();}

Then write three more implementation classes. There are three tips:

Tip 1: Wu:

Package com. yangguangfu. Strategy;/*** @ author trygf521@126.com: a fu * look for Joe Old help, so that Sun Quan can not kill Liu Bei. */Public class backdoor implements istrategy {@ override public void operate () {system. out. println ("Ask Qiao guolao for help, And let Wu Guotai put pressure on Sun Quan so that Sun Quan cannot kill Liu Bei... ");}}

 

Tip 2: ask Wu Guotai to give a green light and let go:

Package com. yangguangfu. Strategy;/***** @ author trygf521@126.com: AFU * ask Wu Guotai to open a green light. */Public class givengreenlight implements istrategy {@ override public void operate () {system. Out. println ("ask Wu Guotai to open a green light and let it go! ");}}

Tip 3: after Mrs sun is disconnected, the defender will be blocked:

Package com. yangguangfu. Strategy;/***** @ author trygf521@126.com: after Mrs. AFO sun is disconnected, the defender is blocked. */Public class blackenemy implements istrategy {@ override public void operate () {system. Out. println ("when Mrs sun is disconnected, block the pursuit of troops ...");}}

Well, let's take a look. There are three tips, so we need to put some tips in the tip:

Package COM. yangguangfu. strategy;/***** @ author trygf521@126.com: AFU */public class context {private istrategy strategy; // constructor, which of the following measures should you use public context (istrategy strategy) {This. strategy = strategy;} public void operate () {This. strategy. operate ();}}

Then Zhao yunxiongmiao carried three tips, pulled into the ranks of the elderly, but also want to marry innocent girls, Satomi Liu Bei old man to go to the school, hi, don't say, the three tips of Liang Ge are really good. Look:

Package COM. yangguangfu. strategy; public class zhaoyun {/*** Zhao Yun is playing. Based on what Zhuge Liang gave him, he split the magic plot */public static void main (string [] ARGs) {context; // The first system was opened when Wu arrived. out. println ("---------- the first ---------------"); Context = new context (new backdoor (); context. operate (); // disassemble and execute system. out. println ("\ n"); // when Liu Bei is not thinking about it, split the second system. out. println ("---------- Liu Bei is happy, but the second one is removed -- ------------- "); Context = new context (New givengreenlight (); context. operate (); // disassemble and execute system. out. println ("\ n"); // Sun Quan's small pursuit, what should I do? Split the third tip system. Out. println ("---------- Sun Quan's small pursuit, what should I do? Split the third tip --------------- "); Context = new context (New blackenemy (); context. operate (); // disassemble and execute system. out. println ("\ n ");}}

Afterwards: with these three moves, Zhou Lang is "Lost and defeated! This is the policy mode, and the features of high cohesion and low coupling are also shown. Another is the scalability, that is, the OCP principle. The policy class can be added to the class, but the context can be modified. java is enough. I will not talk about this much. Let's understand it.

Conversion from http://yangguangfu.iteye.com/blog/815107 and http://blog.csdn.net/zhengzhb/article/details/7609670

 

 

 

Related Article

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.