Summary of strategic mode learning

1, Concept and class diagram

In the policy mode, a series of algorithms are defined, and each algorithm is encapsulated so that they can be replaced with each other. The policy mode makes the algorithm run independently of its customers, which is an object behavior mode.

Strategy is an abstract strategy class, which defines the public methods of the algorithm.
Concretestrategya and B are concrete algorithm classes, which encapsulate the concrete implementation of the algorithm.
Context represents the environment class, which is responsible for maintaining an instance of Strategy.

2, Example of movie ticket discount

//Discount class: Abstract policy class
public interface Discount {
    public double calculate(double price);
}
//Child discount: specific strategy
public class ChildrenDiscount implements Discount{

    @Override
    public double calculate(double price) {
        return price-10;
    }

}

//Discount for students: specific strategies
public class StudentDiscount implements Discount{

    @Override
    public double calculate(double price) {
        return price*0.8;
    }


}
//Member discount: specific strategy
public class VipDiscount implements Discount{


    @Override
    public double calculate(double price) {
        System.out.println("Plus integral");
        return price*0.5;
    }


}
//Movie tickets: Environment
public class MovieTicket {
    private double price;
    private Discount discount;
    public MovieTicket(double price){
        this.price=price;
    }


    public double getPrice(){
        return discount.calculate(this.price);
    }

    public void setDiscount(Discount discount){
        this.discount=discount;
    }


}
//Client class
public class Client {
    public static void main(String[] args) {
        MovieTicket movieTicket=new MovieTicket(50);
        double currentPrice;
        Discount discount;
        discount=new StudentDiscount();
        movieTicket.setDiscount(discount);
        currentPrice=movieTicket.getPrice();
        System.out.println("Student ticket is:"+currentPrice);
        System.out.println("------------------------");
        discount=new VipDiscount();
        movieTicket.setDiscount(discount);
        currentPrice=movieTicket.getPrice();
        System.out.println("Membership ticket is:"+currentPrice);

    }

}

3, Summary

1. Different algorithms can be encapsulated through the use of policy patterns, making the algorithm independent of the customers who use it.
2. In the basic policy mode, the responsibility of selecting the policy used is the client, so the client must know all the policy classes and decide which one to use, that is to say, the client needs to know the difference of algorithm.
3. In order to reduce the responsibility of the client to select specific policies, we can combine the simple factory mode and put the specific implementation into the Context environment class, so as to minimize the responsibility of the client.

People can't do something by themselves´╝îthen they will tell you that you can not do it.

Posted on Sun, 03 May 2020 13:58:48 -0700 by strangebeer