Design pattern of the strategy mode

Source: Internet
Author: User

? design pattern--Strategy mode

1, Definition: Policy mode, also known as the algorithm cluster mode, is defined by different algorithm families, and can be replaced between each other, this mode allows the algorithm to change independently of the customer using the algorithm.

2, "strategy" Understanding: A strategy is a plan, through the implementation of the plan, we give the given input under the specific output. In general, in order to obtain this output, the use of the strategy than the use of algorithms to have a wider choice of space. Therefore, a policy can often be expressed as a set of scenarios that can be substituted (or understood as a set of algorithms) between each other.

The policy pattern allows you to encapsulate the policies that can be replaced with each other within certain classes, which are usually separated from each other, each of which implements a separate completion with the policy class. These classes that show different strategies essentially implement the same operation, so they can be converted to each other, formally due to this mutual substitution feature that allows the program to provide different strategies for different inputs of the user.

3, the application of:

1), a lot of related knowledge in the behavior of the difference;

2) When you need to use a different variant of an algorithm;

3), the client using the algorithm should not know the exact structure of the algorithm;

4), when a class defines a variety of behaviors and these behaviors can occur in the form of multiple conditional statements in the operation of this class.

4, Code:

Main class: Main.class:

public class Main {

Public static void Main (string[] args) {

context context;

context = new context (1);

context. Getdecision ();

context = new context (2);

context. Getdecision ();

context = new context (3);

context. Getdecision ();

}

}

Context class: Context.class:

? ? public class Context {

? ? strategy strategy;

? ? Public Context (int i) {

? ? switch (i) {

? ? Case 1:

? ? strategy = new Concretestrategya ();

? ? Break ;

? ? Case 2:

? ? strategy = new Concretestrategyb ();

? ? Break ;

? ? Case 3:

? ? strategy = new CONCRETESTRATEGYC ();

? ? Break ;

? ? Default:

? ? Break ;

? ? }

? ? }

? ?

? ? Public void Getdecision () {

? ? strategy. Algorithm ();

? ? }

? ?}

? ? Abstract policy Class Code: Strategy.class:

? ? public abstract class Strategy {

? ? Public abstract void algorithm ();

? ?}

? ? Policy class A:concretestrategya.class:

? ? public class Concretestrategya extends strategy{

? ? @Override

? ? Public Void Algorithm () {

? ? System.out.println ("strategy a");

? ? }

? ?}

? ? Policy class B:concretestrategyB. Class:

? ? public class Concretestrategyb extends strategy{

?        ? @Override

?        ? public void algorithm () {

?        ? System.out.println ("Strategy B");

?        ? }

? ?}

? ?

Policy class C:concretestrategyc.class:

public class CONCRETESTRATEGYC extends strategy{

@Override

public void algorithm () {

System.out.println ("Strategy C");

}

}

Operation Result:

? ? Policy A

? ? Policy b

? ? Policy C

Design pattern of the strategy mode

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.