JAVA and mode 16th-Rule Mode

Source: Internet
Author: User

The rule mode is the behavior mode of the object. It is intended to encapsulate an algorithm into an independent class with a common interface for mutual replacement. The policy mode allows the algorithm to change without affecting the client.
________________________________________
Structure of Rule Mode
Policy mode encapsulates algorithms and separates the responsibility for using algorithms from the algorithms themselves and delegates them to different objects for management. Rule mode typically packs a series of algorithms into a series of policy classes and serves as a subclass of an abstract policy class. In one sentence, it is: "prepare a group of algorithms and encapsulate each algorithm to make them interchangeable ". The following describes the structure of the Policy mode instance with a schematic implementation.
 
This mode involves three roles:
● Environment (Context) Role: Hold a reference of Strategy.
● Strategy Role: This is an abstract role, which is usually implemented by an interface or abstract class. This role provides all the interfaces required for specific policy classes.
● A ConcreteStrategy role encapsulates related algorithms or behaviors.
Use Cases
Suppose we want to design a shopping cart system for e-commerce websites selling books. The simplest case is to multiply the unit price of all goods by the quantity, but the actual situation must be more complicated than that. For example, this website may offer a 20% discount to all senior members, a 10% discount to intermediate members, and no discount to junior members.
According to the description, the discount is based on one of the following algorithms:
Algorithm 1: There is no discount for junior members.
Algorithm 2: provides a 10% discount to intermediate members.
Algorithm 3: provides a 20% discount to senior members.
The structure chart implemented using the Policy mode is as follows:
 

 
Source code
Abstract discount
[Java]
1. package com. bankht. Strategy;
2.
3 ./**
4. * @ author: special troops-AK47
5. * @ Creation Time: 10:16:58
6 .*
7. * @ Class description: Abstract discount class
8 .*/
9. public interface MemberStrategy {
10 ./**
11. * calculate the book price
12 .*
13. * @ param booksPrice
14. * original price of books
15. * @ return calculates the discount price
16 .*/
17. public double calcPrice (double booksPrice );
18 .}
 

Discount for junior members
[Java]
1. package com. bankht. Strategy;
2.
3 ./**
4. * @ author: special troops-AK47
5. * @ Creation Time: 10:17:28
6 .*
7. * @ Class description: discount for junior members
8 .*/
9. public class PrimaryMemberStrategy implements MemberStrategy {
10.
11. @ Override
12. public double calcPrice (double booksPrice ){
13.
14. System. out. println ("No discounts for junior members ");
15. return booksPrice;
16 .}
17.
18 .}
 

 
Intermediate member discount
[Java]
1. package com. bankht. Strategy;
2.
3 ./**
4. * @ author: special troops-AK47
5. * @ Creation Time: 10:18:15
6 .*
7. * @ Class description: intermediate member discount class
8 .*/
9. public class IntermediateMemberStrategy implements MemberStrategy {
10.
11. @ Override
12. public double calcPrice (double booksPrice ){
13.
14. System. out. println ("the discount for intermediate members is 10% ");
15. return booksprint * 0.9;
16 .}
17.
18 .}
 

 
Discount for senior members
[Java]
1. package com. bankht. Strategy;
2.
3 ./**
4. * @ author: special troops-AK47
5. * @ Creation Time: 10:18:59
6 .*
7. * @ Class description: discounts for senior members
8 .*/
9. public class AdvancedMemberStrategy implements MemberStrategy {
10.
11. @ Override
12. public double calcPrice (double booksPrice ){
13.
14. System. out. println ("the discount for senior members is 20% ");
15. return booksprint * 0.8;
16 .}
17 .}
 

 
Price
[Java]
1. package com. bankht. Strategy;
2.
3 ./**
4. * @ author: special troops-AK47
5. * @ Creation Time: 10:19:22
6 .*
7. * @ Class description: price class
8 .*/
9. public class Price {
10. // hold a specific policy object
11. private MemberStrategy strategy;
12.
13 ./**
14. * constructor to pass in a specific policy object
15 .*
16. * @ param strategy
17. * specific policy object
18 .*/
19. public Price (MemberStrategy strategy ){
20. this. strategy = strategy;
21 .}
22.
23 ./**
24. * calculate the book price
25 .*
26. * @ param booksPrice
27. * original price of books
28. * @ return calculates the discount price
29 .*/
30. public double quote (double booksPrice ){
31. return this. strategy. calcPrice (booksPrice );
32 .}
33 .}
 

 
Client
[Java]
1. package com. bankht. Strategy;
2.
3 ./**
4. * @ author: special troops-AK47
5. * @ Creation Time: 10:19:48
6 .*
7. * @ Class description: Client
8 .*/
9. public class Client {
10.
11. public static void main (String [] args ){
12. // select and create the policy object to be used
13. MemberStrategy strategy = new AdvancedMemberStrategy ();
14.
15. // create an environment
16. Price price = new Price (strategy );
17. // calculate the price
18. double quote = price. quote (300 );
19. System. out. println ("the final price of the book is:" + quote );
20.
21 .}
22.
23 .}
 

 
From the above example, we can see that the policy mode only encapsulates algorithms, and provides new algorithms to be inserted into existing systems, as well as the old algorithms to "retire" from the system, the policy mode does not determine when to use the algorithm. Under what circumstances is the algorithm used determined by the client.
Recognition policy Mode
Center of gravity of policy Mode
The focus of the policy mode is not how to implement algorithms, but how to organize and call these algorithms to make the program structure more flexible, with better maintainability and scalability.
Equality of Algorithms
A major feature of the policy model is the equality of each policy algorithm. For a series of specific policy algorithms, everyone has the same status. Because of this equality, algorithms can be replaced with each other. All policy algorithms are independent of each other and are independent from each other.
So we can describe this series of policy algorithms as follows: Policy algorithms are different implementations of the same behavior.
Uniqueness of runtime policies
During running, the policy mode can only use one specific policy to implement objects at a time. Although you can dynamically switch between different policy implementations, you can only use one.
Public Behavior
It is often seen that all specific policy classes have some public behaviors. At this time, these public behaviors should be put into the Strategy class of the common abstract policy role. Of course, at this time, the abstract policy role must be implemented using a Java Abstract class, rather than an interface.
In fact, this is also a typical standard practice of inheriting code to the top of the hierarchy.
 
 

 

Advantages of Rule Mode
(1) The policy mode provides methods for managing related algorithm families. The hierarchical structure of a policy class defines an algorithm or behavior family. Proper use of inheritance allows you to move public code to the parent class to avoid code duplication.
(2) using policy mode can avoid using multi-condition (if-else) statements. Multi-condition statements are not easy to maintain. they mix the logic of which algorithm or behavior is adopted with the logic of the algorithm or behavior and are all listed in a multi-Condition Statement, it is more primitive and backward than the inheritance method.
Disadvantages of Rule Mode
(1) The client must know all the policy classes and decide which one to use. This means that the client must understand the differences between these algorithms so that appropriate algorithm classes can be selected in a timely manner. In other words, the policy mode is only applicable when the client knows the algorithm or behavior.
(2) because the policy mode encapsulates each specific policy implementation into a class separately, if there are many alternative policies, the number of objects will be considerable.
Author: m13666425773
 

 


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.