策略模式StrategyPattern

簡介

Define a family of algorithms,encapsulate each one, and make them interchangeable.
定義一組算法,將每組算法都封裝起來.

UML

這裏寫圖片描述

題目

使用策略模式實現商場收銀程序,滿足商場隨時可能會變化的優惠活動的需求,具體描述如下:
收費模式現有3種:
1、正常收費
2、打折(80%)
3、滿減(滿300100

代碼

GitHub源碼:https://github.com/YEN-GitHub/PatternDemo/tree/master/src/BehavioralPattern1/StrategyPattern

基本寫法

package BehavioralPattern1.StrategyPattern.demo1;

/**
 * Created by yangenneng on 2017-05-31 10:25
 * Abstract:收費接口
 */
public interface ChargeImp {
    //返回總的價格
    public Double totalPrice();
}
package BehavioralPattern1.StrategyPattern.demo1;

/**
 * Created by yangenneng on 2017-05-31 10:28
 * Abstract:正常收費
 */
public class Normal implements ChargeImp {

    private double totalPrice;//總價

    public Normal(Double totalPrice){
        this.totalPrice=totalPrice;
    }
    @Override
    public Double totalPrice() {
        return totalPrice;
    }

}
package BehavioralPattern1.StrategyPattern.demo1;

/**
 * Created by yangenneng on 2017-05-31 10:30
 * Abstract:打折
 */
public class Discount implements ChargeImp {
    private double totalPrice;//總價
    private double ratio;//折扣

    @Override
    public Double totalPrice() {
        return totalPrice * ratio;
    }

    public Discount(double totalPrice,double ratio){
        this.totalPrice=totalPrice;
        this.ratio=ratio;
    }

}
package BehavioralPattern1.StrategyPattern.demo1;

/**
 * Created by yangenneng on 2017-05-31 10:33
 * Abstract:滿減
 */
public class FullSubtract implements ChargeImp {

    private double totalPrice;//總價
    private double maxPrice;//滿多少
    private double subtractPrice;//減多少

    public FullSubtract(double totalPrice,double maxPrice,double subtractPrice){
        this.totalPrice=totalPrice;
        this.maxPrice=maxPrice;
        this.subtractPrice=subtractPrice;
    }

    @Override
    public Double totalPrice() {
        if(totalPrice>=maxPrice){
            totalPrice-=subtractPrice;
        }
        return totalPrice;
    }
}
package BehavioralPattern1.StrategyPattern.demo1;

/**
 * Created by yangenneng on 2017-05-31 10:36
 * Abstract:環境角色1
 */
public class Context1 {
    private ChargeImp chargeImp=null;

    public Context1(ChargeImp chargeImp){
        this.chargeImp=chargeImp;
    }

    //調用策略方法
    public double getTotalPrice(){
         return  chargeImp.totalPrice();
    }

}
package BehavioralPattern1.StrategyPattern.demo1;

import java.util.Scanner;

/**
 * Created by yangenneng on 2017-05-31 10:36
 * Abstract:客戶端測試1
 */
public class Client1 {
    public static void main(String[] args) {
        Context1 context1=null;

        System.out.println("請選擇:【1】正常 【2】打折 【3】滿減");
        Scanner scanner=new Scanner(System.in);
        int type=scanner.nextInt();

        switch (type){
            case 1:context1=new Context1(new Normal(100.0));
                    break;
            case 2:context1=new Context1(new Discount(100.0,0.8));
                    break;
            case 3:context1=new Context1(new FullSubtract(100.0,80,30));
                    break;
        }

        System.out.println(context1.getTotalPrice());

    }
}

改進寫法

package BehavioralPattern1.StrategyPattern.demo1;

/**
 * Created by yangenneng on 2017-05-31 10:36
 * Abstract:環境角色2
 */
public class Context2 {
    private ChargeImp chargeImp=null;

    public Context2(int type){
        switch (type){
            case 1:chargeImp=new Normal(100.0);
                break;
            case 2:chargeImp=new Discount(100.0,0.8);
                break;
            case 3:chargeImp=new FullSubtract(100.0,80,30);
                break;
        }
        this.chargeImp=chargeImp;
    }

    //調用策略方法
    public double getTotalPrice(){
         return  chargeImp.totalPrice();
    }

}
package BehavioralPattern1.StrategyPattern.demo1;

import java.util.Scanner;

/**
 * Created by yangenneng on 2017-05-31 10:46
 * Abstract:客戶端測試2
 */
public class Client2 {
    public static void main(String[] args) {
        System.out.println("請選擇:【1】正常 【2】打折 【3】滿減");
        Scanner scanner=new Scanner(System.in);
        int type=scanner.nextInt();

        Context2 context2=new Context2(type);

        System.out.println(context2.getTotalPrice());

    }
}

運行結果

這裏寫圖片描述

這裏寫圖片描述

這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章