Algorithm encapsulation and switching-policy mode (2)

Source: Internet
Author: User
24.2 policy mode Overview

In policy mode, we can define some independent classes to encapsulate different algorithms,Each class encapsulates a specific AlgorithmEvery class that encapsulates an algorithm can be calledStrategy)To ensure consistency of these policies in use, an abstract policy class is generally provided for rule definition, and each algorithm corresponds to a specific policy class.

The main purpose of the Policy mode is to separate the definition and use of an algorithm, that is, to separate the behavior and environment of an algorithm.The algorithm is defined in a dedicated policy class. Each policy class encapsulates an implementation algorithm. The environment class of the algorithm is used to program the abstract policy class and complies with the "Dependency inversion principle ". When a new algorithm appears, you only need to add a new specific policy class that implements the abstract policy class. The rule mode is defined as follows:

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 ). Policy mode is an object behavior mode.

The rule mode structure is not complex, but we need to understand the role of context in the environment, as shown in Figure 24-1:

The policy mode structure includes the following roles:

Context(Environment ):An environment is a role that uses algorithms. It can adopt multiple policies to solve a problem (that is, to implement a method. Maintain a reference instance for the abstract policy class in the Environment class to define the adopted policy.

Strategy(Abstract policy class ):It declares an abstract method for the supported algorithms. It is the parent class of all policy classes. It can be an abstract class, a specific class, or an interface. The environment class calls the algorithm implemented in a specific policy class at runtime through the methods declared in the abstract policy class.

Concretestrategy(Specific strategy ):It implements the algorithm declared in the abstract policy class. At runtime, the specific policy class will overwrite the abstract policy class object defined in the Environment class, and use a specific algorithm to implement a service.

 

Thoughts

Can an environment context correspond to multiple different policy levels? How to design?

Policy mode is a design mode that is easy to understand and use. It encapsulates an algorithm. It separates the responsibility of an algorithm from the algorithm itself and delegates it to different objects for management. Rule mode encapsulates a series of algorithms into a series of specific policy classes and serves as a subclass of the abstract policy class. In the policy mode, it is very important to understand the environment and abstract strategies. The environment class is the class that needs to use algorithms. There can be multiple environmental classes in a system, and they may need to reuse some of the same algorithms.

When using the Policy mode, We need to extract the algorithm from the context class. First, we should create an abstract policy class. The typical code is as follows:

Abstract class abstractstrategy {public abstract void algorithm (); // declare an abstract algorithm}

Then, the class that encapsulates each specific algorithm is used as a subclass of the abstract policy class, as shown in the following code:

Class concretestrategya extends abstractstrategy {// specific implementation of the algorithm public void algorithm () {// algorithm }}

Similar to other specific policy classes, the context class establishes an association between it and the abstract policy class. The typical code is as follows:

Class context {private abstractstrategy strategy; // maintain a reference to the abstract policy class public void setstrategy (abstractstrategy Strategy) {This. strategy = strategy;} // call the Public void algorithm () {strategy. algorithm ();}}

Define a strategy object of the abstractstrategy type in the context class, and input a specific policy object in the client through injection. The client code snippets are as follows:

...... Context context = new context (); abstractstrategy strategy; Strategy = new concretestrategya (); // you can specify the type of context at runtime. setstrategy (Strategy); context. algorithm ();......

You only need to inject a specific policy object into the client code. You can store the specific policy class name in the configuration file and dynamically create a specific policy object through reflection, in this way, you can flexibly change the specific policy categories and add new ones.The policy mode provides a plug-in (Pluggable) Algorithm Implementation solution..

[Author: Liu Wei http://blog.csdn.net/lovelion]

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.