In our life, we can go to the supermarket shopping can choose different promotional prices of goods, travel can choose to take a car, can take a train, can take a plane.

1. What is the strategic pattern?

A policy pattern, where different families of algorithms are defined and interchangeable, allows the algorithm to change independently of the client using the algorithm.

2. What are the benefits of the strategic model?

The advantage of the policy pattern is that you can change the behavior of an object dynamically.

3. Design principles

The design principle is to take the parts of a class that change frequently or may change in the future, make them an interface (virtual classes can be used in C ++), and then include an instance of the object in the class, so that the instance of the class can, at run time, invoke the behavior of the class that implements the interface. [References]

A policy pattern is an object behavior pattern that focuses on a set of algorithms, encapsulating each into a separate class with a common interface so that they can be interchangeable. The policy pattern allows algorithms to change without affecting the client. In general, the policy pattern is used when an application needs to implement a particular service or function, and the program can be implemented in multiple ways.

Policy patterns: Define a set of algorithms, encapsulate them, and make them interchangeable

Policy pattern specific role implementation

  • Abstract strategy (Strategy) class: This is an abstract role, usually implemented by an interface or abstract class. This role gives all the interfaces required for the concrete policy classes.
  • Specific strategies (Concrete Strategy) class: An interface that implements an abstract policy definition and provides a concrete algorithm implementation or behavior.
  • Environment (Context) class: Holds a reference to a policy class that is ultimately invoked by the client.

1. Create an interface

public interface Strategy {
    void show();
}

2. Create an entity class that implements the interface

Public Class Strategya implements Strategy{@Override public void show() {System.out.println(" full 300-40"); public Class Strategya implements Strategy{@Override public void show() {System.out.println(" full 300-40"); }}// join the Java development exchange: 593142328 together blow water chat
Public Class StrategyB implements Strategy{@Override public void show() {System.out.println(" 500 % off "); public Class StrategyB implements Strategy{@Override public void show() {System.out.println(" 500 % off "); }}
Public Class StrategyC implements Strategy{@Override public void show() {System.out.println(" Buy one, get one free "); public Class StrategyC implements Strategy{@Override public void show() {System.out.println(" Buy one, get one free "); }}

3. Create Context class

public class Context { private Strategy strategy; public Context(Strategy strategy) { this.strategy=strategy; } public void ContextShow() { strategy.show(); }} // join the Java development exchange: 593142328 together blow water chat

Use the Context to see how the behavior changes when it changes the Strategy

public static void main(String[] args) {
 Context context=new Context(new StrategyA());
 context.ContextShow();
}

Policy pattern implementation

Taking Taobao 618 promotion as an example, the strategy mode is used to realize the promotional discount scene

Goods over 300 to 40, 20% off, normal charges

1. Declare the charging interface

public interface Cash{
    double Calculation(double money);
}

2. Declare the context class

public class Context { private Cash cash; public Context(Cash cash) { this.cash=cash; } public double ContextShow(double money) { return cash.Calculation(money); }}

3. Declare normal charge subclasses

public class CashNormal implements Cash{ @Override public double Calculation(double money) { double total=0; total=money; System.out.println(" normal charges, amount "+total); return total; }}

4. Declare the discount category

public class CashDiscount implements Cash{ private double disCount; public CashDiscount(double disCount) { this.disCount=disCount; } @Override public double Calculation(double money) { double total=0; total=money*disCount; System.out.println(" to participate in making a disCount "+disCount+" activity, amount "+total); return total; }}

5. Declare full subtraction classes

Public class CashFullReduction implements Cash{// private Double MoneyReturn; Private Double MoneyConditation; public CashFullreduction(double moneyReturn,double moneyConditation) { this.moneyConditation=moneyConditation; this.moneyReturn=moneyReturn; } @Override public double Calculation(double money) { double total=0; If (money<300){// if(money<300); } else {total= money-(Math. Floor (Money/MoneyConditation)*moneyReturn); } System.out.println(" payment "+money+", full "+moneyConditation+" minus "+moneyReturn+", the final amount is "+total "); return total; }}

6. The client

Public static void main(String[] args) {Context Context =new Context(new CashFullReduction (40,300)); / / 300-40 context. ContextShow (400); The Context context2 = new Context (new CashDiscount (0.8)); // 20% off context2.ContextShow(400); Context context3=new Context(new CashNormal()); // normal charge context3.ContextShow(400); }

conclusion

1. The advantages

  • Policy classes can switch freely between them

    Because the policy classes all implement the same interface, they can switch freely between them.

  • extensible

    To add a new policy, you only need to add a specific policy class, and basically do not need to change the original code, in accordance with the “Open Closed Principle”.

  • Avoid the use of multiple conditional selection statements (if else), fully reflect the concept of object-oriented design. [Information of Baixiao]

2. Disadvantages:

  • The client must know all the policy classes and decide which one to use.
  • The policy pattern will result in many policy classes, and the number of objects can be reduced to some extent by using the Endowment pattern.

Usage scenario:

1. If there are many classes in a system that differ only in their behavior, then using the policy pattern can dynamically make an object choose one behavior among many behaviors

2. A system needs to dynamically choose one of several algorithms

3. If an object has many behaviors, these behaviors will have to be implemented using multiple conditional selection statements without proper patterns

Finally, I wish you success as soon as possible, get satisfiedoffer, fastA promotion and pay increaseOn the top of life.

Can words please give me a 3 connect support once I yo?????? [Information of Baixiao]