java商场满减活动_Java使用策略模式解决商场促销商品问题示例
本文實例講述了Java使用策略模式解決商場促銷商品問題。分享給大家供大家參考,具體如下:
一 模式定義
策略模式:定義一系列的算法,將每一種算法封裝起來并可以相互替換使用,策略模式讓算法獨立于使用它的客戶應用而獨立變化。
二 模式舉例
1 模式分析
我們借用商場促銷商品來說明這一模式。
2 策略模式靜態類圖
3 代碼示例
3.1 創建策略接口一IStrategy
package com.demo.strategy;
/**
* 策略接口
*
* @author
*
*/
public interface IStrategy {
/**
* 計算實際價格方法
*
* @param consumePrice
* 消費金額
* @return
*/
public double realPrice(double consumePrice);
}
3.2 八折促銷策略一RebateStrategy
package com.demo.strategy;
/**
* 打八折商品促銷策略
*
* @author
*
*/
public class RebateStrategy implements IStrategy {
private final double rate;
/**
* 構造方法設置打折率
*/
public RebateStrategy() {
this.rate = 0.8;
}
/**
* 計算實際價格方法
*
* @param consumePrice
* 消費金額
* @return
*/
public double realPrice(double consumePrice) {
return consumePrice * this.rate;
}
}
3.3 滿1000減200促銷策略一ReduceStrategy
package com.demo.strategy;
/**
* 滿1000減200 商品促銷策略
*
* @author
*
*/
public class ReduceStrategy implements IStrategy {
/**
* 計算實際價格方法
*
* @param consumePrice
* 消費金額
* @return
*/
public double realPrice(double consumePrice) {
if (consumePrice >= 1000) {
return consumePrice - 200;
} else {
return consumePrice;
}
}
}
3.4 200以上部分打8折促銷策略一PromotionalStrategy
package com.demo.strategy;
/**
* 滿200,高于200部分打八折 商品促銷策略
*
* @author
*
*/
public class PromotionalStrategy implements IStrategy {
/**
* 計算實際價格方法
*
* @param consumePrice
* 消費金額
* @return
*/
public double realPrice(double consumePrice) {
if (consumePrice > 200) {
return 200 + (consumePrice - 200) * 0.8;
} else {
return consumePrice;
}
}
}
3.5 創建上下文環境一Context
package com.demo.context;
import java.math.BigDecimal;
import com.demo.strategy.IStrategy;
/**
* 上下文環境
*
* @author
*
*/
public class Context {
// 當前策略
private IStrategy strategy;
// 設置當前策略
public void setStrategy(IStrategy strategy) {
this.strategy = strategy;
}
// 使用策略計算價格
public double cul(double consumePrice) {
// 使用具體商品促銷策略獲得實際消費金額
double realPrice = this.strategy.realPrice(consumePrice);
// 格式化保留小數點后1位,即:精確到角
BigDecimal bd = new BigDecimal(realPrice);
bd = bd.setScale(1, BigDecimal.ROUND_DOWN);
return bd.doubleValue();
}
}
3.6 消費者購物消費一Client
package com.demo;
import java.util.Random;
/**
* 客戶端應用程序
*
* @author
*
*/
public class Client {
/**
* @param args
*/
public static void main(String[] args) {
// 創建上下問環境對象實例
// Context context = new Context();
// 隨機數對象
Random random = new Random();
for (int i = 0; i < 10; i++) {
// 產生隨機數的方式判斷使用何種促銷策略
int x = random.nextInt(3);
// 消費價格也是由隨機數產生的(不能為0)
double consumePrice = 0;
while ((consumePrice = random.nextInt(2000)) == 0) {
}
double realPrice = consumePrice;
switch (x) {
case 0:
// 打八折商品
// context.setStrategy(new RebateStrategy());
realPrice = consumePrice * 0.8;
break;
case 1:
// 滿200,高于200部分打八折 商品
// context.setStrategy(new PromotionalStrategy());
if (consumePrice > 200) {
realPrice = 200 + (consumePrice - 200) * 0.8;
}
break;
case 2:
// 滿1000減200 商品
// context.setStrategy(new ReduceStrategy());
if (consumePrice >= 1000) {
realPrice = consumePrice - 200;
}
break;
}
System.out.print("【"
+ (x == 0 ? "打八折" : (x == 1 ? "高于200部分打八折"
: (x == 2 ? "滿1000減200" : ""))) + "】商品:");
System.out.println("原價:" + consumePrice + " - 優惠后價格:" + realPrice);
}
}
}
4 運行結果
【滿1000減200】商品:原價:908.0 - 優惠后價格:908.0
【滿1000減200】商品:原價:1129.0 - 優惠后價格:929.0
【滿1000減200】商品:原價:829.0 - 優惠后價格:829.0
【打八折】商品:原價:518.0 - 優惠后價格:414.40000000000003
【滿1000減200】商品:原價:1230.0 - 優惠后價格:1030.0
【打八折】商品:原價:106.0 - 優惠后價格:84.80000000000001
【滿1000減200】商品:原價:1134.0 - 優惠后價格:934.0
【高于200部分打八折】商品:原價:664.0 - 優惠后價格:571.2
【滿1000減200】商品:原價:564.0 - 優惠后價格:564.0
【滿1000減200】商品:原價:730.0 - 優惠后價格:730.0
三 該模式設計原則
1 "開-閉"原則
2 單一職責原則
四 使用場合
1 當多個類的表現行為不同,需要在運行時刻動態選擇具體執行的行為的時候。
2 需要在不同情況下使用不同策略,或者策略還可能在未來用其它方式實現的時候。
3 需要隱藏具體策略的實現細節,各個具體策略彼此獨立的時候。
4 當一個類中出現了多種行為,而且在一個操作中使用多個條件分支來判斷使用多種行為的時候,可以使用策略模式將各個條件分支的動作植入具體策略中實現。
五 策略模式靜態類圖
希望本文所述對大家java程序設計有所幫助。
總結
以上是生活随笔為你收集整理的java商场满减活动_Java使用策略模式解决商场促销商品问题示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 大话设计模式策略模式_多种方法实现商场促
- 下一篇: 活动、节假日、促销等营销方式的因果效应评