《研磨设计模式》chap17 策略模式(1) 简介
生活随笔
收集整理的這篇文章主要介紹了
《研磨设计模式》chap17 策略模式(1) 简介
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
客戶報價,價格有多種情況。
1. 正常思路解決:分成多個函數
public class Price {//報價,對不同類型的,計算不同的價格 public double quote(double goodsPrice,String customerType){if("普通客戶".equals(customerType)){return this.calcPriceForNormal(goodsPrice);}else if("老客戶".equals(customerType)){return this.calcPriceForOld(goodsPrice);}else if("大客戶".equals(customerType)){return this.calcPriceForLarge(goodsPrice); }//其余人員都是報原價return goodsPrice;}//為新客戶或者是普通客戶計算應報的價格 private double calcPriceForNormal(double goodsPrice){System.out.println("對于新客戶或者是普通客戶,沒有折扣");return goodsPrice;}//為老客戶計算應報的價格 private double calcPriceForOld(double goodsPrice){System.out.println("對于老客戶,統一折扣5%");return goodsPrice*(1-0.05);}2. 策略模式簡介
3. 應用策略模式到場景
總結
以上是生活随笔為你收集整理的《研磨设计模式》chap17 策略模式(1) 简介的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《研磨设计模式》chap19 备忘录模式
- 下一篇: 《研磨设计模式》chap17 策略模式(