Java design pattern--policy mode

Source: Internet
Author: User

650) this.width=650; "title=" Image1.png "src=" https://s5.51cto.com/wyfs02/M00/99/2B/ Wkiol1le1pki-znaaadxag5lmeq574.png "alt=" Wkiol1le1pki-znaaadxag5lmeq574.png "/>


The strategy pattern is a method of defining a series of algorithms, conceptually, all of which are done the same work, only implemented differently, it can call all the algorithms in the same way, reducing the coupling between the various algorithm classes and the use of the algorithm class


The strategy pattern is used to encapsulate the algorithm, but in practice we find that it can be used to encapsulate almost any type of rule, as long as the analysis process to hear the need to apply different business rules at different times, you can consider the possibility of using the policy model to deal with this change


Object-oriented programming is not the more class the better, the class is divided for encapsulation, but the basis of classification is abstract, and the abstract collection of objects with the same properties is considered a class.

650) this.width=650; "title=" Image.png "src=" https://s5.51cto.com/wyfs02/M00/99/2B/ Wkiol1le1q-cpebsaafdf3sppuk163.png "alt=" Wkiol1le1q-cpebsaafdf3sppuk163.png "/>

Abstract policy Strategy Interface: Defines the public interface for all supported algorithms, an abstract role that is typically implemented by an interface or abstract class. This role gives the interfaces required for all the specific policy classes

Policy interface

Public interface Strategy

{

/**

* Strategy method

* /

public void Strategyinterface();

}

Specific strategy Concretestrategy class: Encapsulates a specific algorithm or behavior to implement the Strategy interface

Specific strategy A

public class Concretestrategya implements strategy{

@Override

public void Strategyinterface () {

Related business

}

}

Specific Strategy B

public class Concretestrategyb implements strategy{

@Override

public void Strategyinterface () {

Related business

}

}

Specific Strategy C

public class CONCRETESTRATEGYC implements strategy{

@Override

public void Strategyinterface () {

Related business

}

}

Environment Role Class: holds a reference to a strategy.

public class context{

An object that holds a specific policy

Private strategy strategy;

/**

* constructor to pass in a specific policy object

*/

Public Context (strategy strategy) {

This.strategy=strategy;

}

/**

* Strategy method

*/

public void Contextinterface () {

Strategy.strategyinterface ();

}

}

Client:

public class client{

public static void Main (string[] args) {

Select and create the policy object you want to use

Strategy Strategy=new Concretestrategya ();

Create an environment

Context Context=new context (strategy);

Business results

Context.contextinterface ();

}

}

Advantages of the Strategy mode:

(1) The strategy class hierarchy of the strategy mode defines a series of reusable algorithms or behaviors for the context, and inheritance helps to extract the public functions from these algorithms.

(2) simplifies unit testing because each algorithm has its own class and can be tested individually through its own interface.

(3) When different behaviors are stacked in a class, it is difficult to avoid using conditional statements to choose the appropriate behavior. Use the policy mode to avoid using multiple condition (if-else) statements. Encapsulating these behaviors in separate strategy classes allows you to eliminate conditional statements in classes that use these behaviors.

Disadvantages of the policy model:

(1) The client must know all of the policy classes and decide for itself which policy class to use. This means that the client must understand the differences between these algorithms in order to select the appropriate algorithm classes at the right time. In other words, the policy pattern applies only to situations where the client knows the algorithm or behavior.

(2) because the policy model encapsulates each specific policy implementation as a class, the number of objects can be significant if there are many alternative strategies .



Reference Link: http://www.cnblogs.com/java-my-life/archive/2012/05/10/2491891.html

Reference book: "Big Talk design mode"

I am a beginner, please Daniel more advice!

Java design pattern--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.