[Design mode] policy Mode

Source: Internet
Author: User

• Strategy pattern embodies two very basic object-oriented design principles
-Encapsulate the concept of change
-Use interfaces in programming instead of implementing Interfaces
• Interface-Oriented Programming


Rule mode definition
-Define a set of algorithms, encapsulate each algorithm, and make them interchangeable.
-The policy mode enables these algorithms to change without affecting each other when the client calls them.

• Significance of the Policy Model
-The policy mode enables developers to develop software consisting of many replaceable parts, and each part has a weak connection relationship.
-The weak connection feature makes the software more scalable and easy to maintain. More importantly, it greatly improves the reusability of the software.

• Composition of policy Modes
-Abstract policy role: a policy class, usually implemented by an interface or abstract class.
-Specific policy roles: encapsulate related algorithms and Behaviors
-Environment role: it holds a reference to the policy class and is finally called by the client.

• Implementation of policy Modes
-The purpose of the Policy mode is to encapsulate each algorithm into an independent class with a common interface for mutual replacement.
-The policy mode allows the algorithm to change without affecting the client. The policy mode can be used to separate the behavior from the environment.
-The Environment class is responsible for maintaining and querying behavior classes, and various algorithms are provided in specific policies. Because the algorithm and environment are independent, modifications to the algorithm do not affect the environment and the client.

• Policy mode compiling steps
-1. Define a public interface for the policy object.
-2. Write a policy class that implements the above public interface
-3. Save a reference to the policy object in the class using the Policy object.
-4. In the class using policy objects, set and get methods (injection) of policy objects or assign values using constructor methods are implemented.

• See the source code of the JDK collections class.

• Implement your own policy model

Strategy. Java

1 package cn.itcast.strategy;2 3 public interface Strategy{4     public int calculate(int a, int b);5 }

Addstrategy. Java

1 package cn.itcast.strategy;2 3 public class AddStrategy implements Strategy{4     public int calculate(int a, int b){5         return a + b;6     }7 }

Subtractstrategy. Java

1 package cn.itcast.strategy;2 3 public class SubtractStrategy implements Strategy{4     public int calculate(int a, int b){5         return a - b;6     }7 }

Multiplystrategy. Java

1 package cn.itcast.strategy;2 3 public class MultiplyStrategy implements Strategy{4     public int calculate(int a, int b){5         return a * b;6     }7 }

Dividestrategy. Java

1 package cn.itcast.strategy;2 3 public class DivideStrategy implements Strategy{4     public int calculate(int a, int b){5         return a / b;6     }7 }

Environment. Java

1 package CN. itcast. strategy; 2 3 Public class environment {4 private strategy Strategy; 5 Public Environment (strategy Strategy) {6 this. strategy = strategy; 7} 8 9 // provides a method to change the strategy. Change the value of strategy to 10 public void setstrategy (strategy Strategy) {11 this. strategy = strategy; 12} 13 // get the value of Strategy 14 public strategy getstrategy () {15 return this. strategy; 16} 17 18 // the environment itself has no comparison function. It is based on the method provided by strategy that 19 // map does not know what policy classes are passed in. 20 public int calculate (int A, int B) {21 // you can specify the policy class as required. 22 return strategy. calculate (a, B); 23} 24}

Client. Java

1 package CN. itcast. strategy; 2 3 Import Java. lang. character. subset; 4 5 public class client {6 public static void main (string [] ARGs) {7 // addition 8 addstrategy = new addstrategy (); 9 // when the policy role is passed to the Environment role, the Environment role is used to complete computation 10 // a reference to a specific policy class is required 11 environment = new environment (addstrategy ); 12 system. out. println (environment. calculate (3, 4); 13 // subtract 14 subtractstrategy = new subtractstrategy (); 15 environment. setstrategy (subtractstrategy); 16 system. out. println (environment. calculate (3, 4); 17 // multiplication 18 multiplystrategy = new multiplystrategy (); 19 environment. setstrategy (multiplystrategy); 20 system. out. println (environment. calculate (3, 4); 21 // Division 22 dividestrategy = new dividestrategy (); 23 environment. setstrategy (dividestrategy); 24 system. out. println (environment. calculate (3, 4); 25 26} 27}

• Disadvantages of policy Mode
-1. The client must know all the policy classes and decide which policy class to use.
-2. There are many strategies.

• Solution

-Factory method adopted

 

[Design mode] policy Mode

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.