當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
【设计模式实战】SpringBoot模板+策略设计模式实现抽奖流程
生活随笔
收集整理的這篇文章主要介紹了
【设计模式实战】SpringBoot模板+策略设计模式实现抽奖流程
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、需求分析
1.?抽獎余額有兩種,1種【貨幣1】,1種【貨幣2】,對應扣除兩種余額
2.?不變的部分余額和扣除余額,變化的部分是余額類型與扣除方式,所以可以使用策略模式
二、設計模式思想
1. 定義獲得余額和扣除余額抽象方法
2. 策略方法分為【貨幣1】策略和【貨幣2】策略
3. 調用放Context根據傳入策略執行查詢和扣除方法
4. 可擴展,新增【貨幣3】抽獎,則只需要實現LotteryContext接口
二、代碼實現
- LotteryStrategy
- /*** 抽獎策略模式* 需求分析:抽獎余額有兩種,1種金幣,1種獎券;不變的部分余額和扣除余額,變化的部分是余額類型與扣除方式,所以可以使用策略模式* 1. 定義獲得余額和扣除余額抽象方法* 2. 策略方法分為金幣策略和獎券策略* 3. 調用放Context根據傳入策略執行查詢和扣除方法* 4. 可擴展,新增鉆石抽獎,則只需要實現LotteryContext接口* @author Marion* @date 2021/7/15 09:59*/ public interface LotteryStrategy {/*** 獲得抽獎貨幣*/long amount(long uid);/*** 扣除抽獎貨幣*/boolean draw(long uid, long amoun);}
- CoinLotteryStrategy
- /*** 金幣抽獎模式* @author Marion* @date 2021/7/15 10:07*/ @Component public class CoinLotteryStrategy implements LotteryStrategy {/*** 獲得抽獎貨幣*/@Overridepublic long amount(long uid) {return 0;}/*** 扣除抽獎貨幣* @param uid* @param amount*/@Overridepublic boolean draw(long uid, long amount) {return true;} }
- CouponLotteryStrategy
- /*** 獎券抽獎模式* @author Marion* @date 2021/7/15 10:07,*/ @Component public class CouponLotteryStrategy implements LotteryStrategy {/*** 獲得抽獎貨幣*/@Overridepublic long amount(long uid) {return 0;}/*** 扣除抽獎貨幣* @param uid* @param amount*/@Overridepublic AssetTransaction draw(long uid, long amount) {return true}}
- LotteryContext
- /*** 抽獎策略模式* @author Marion* @date 2021/7/15 10:04*/ public class LotteryContext {private LotteryStrategy lotteryStrategy;public LotteryContext(LotteryStrategy lotteryStrategy) {this.lotteryStrategy = lotteryStrategy;}public long getAmount(long uid) {return this.lotteryStrategy.amount(uid);}public boolean draw(long uid, long amount) {return this.lotteryStrategy.draw(uid, amount);}}
三、運行結果
總結
以上是生活随笔為你收集整理的【设计模式实战】SpringBoot模板+策略设计模式实现抽奖流程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 应用运维工程师(技术二面)面试笔记
- 下一篇: [附源码]JSP+ssm计算机毕业设计创