當(dāng)前位置:
首頁(yè) >
前端技术
> javascript
>内容正文
javascript
【设计模式实战】SpringBoot模板+策略设计模式实现抽奖流程
生活随笔
收集整理的這篇文章主要介紹了
【设计模式实战】SpringBoot模板+策略设计模式实现抽奖流程
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一、需求分析
1.?抽獎(jiǎng)余額有兩種,1種【貨幣1】,1種【貨幣2】,對(duì)應(yīng)扣除兩種余額
2.?不變的部分余額和扣除余額,變化的部分是余額類型與扣除方式,所以可以使用策略模式
二、設(shè)計(jì)模式思想
1. 定義獲得余額和扣除余額抽象方法
2. 策略方法分為【貨幣1】策略和【貨幣2】策略
3. 調(diào)用放Context根據(jù)傳入策略執(zhí)行查詢和扣除方法
4. 可擴(kuò)展,新增【貨幣3】抽獎(jiǎng),則只需要實(shí)現(xiàn)LotteryContext接口
二、代碼實(shí)現(xiàn)
- LotteryStrategy
- /*** 抽獎(jiǎng)策略模式* 需求分析:抽獎(jiǎng)余額有兩種,1種金幣,1種獎(jiǎng)券;不變的部分余額和扣除余額,變化的部分是余額類型與扣除方式,所以可以使用策略模式* 1. 定義獲得余額和扣除余額抽象方法* 2. 策略方法分為金幣策略和獎(jiǎng)券策略* 3. 調(diào)用放Context根據(jù)傳入策略執(zhí)行查詢和扣除方法* 4. 可擴(kuò)展,新增鉆石抽獎(jiǎng),則只需要實(shí)現(xiàn)LotteryContext接口* @author Marion* @date 2021/7/15 09:59*/ public interface LotteryStrategy {/*** 獲得抽獎(jiǎng)貨幣*/long amount(long uid);/*** 扣除抽獎(jiǎng)貨幣*/boolean draw(long uid, long amoun);}
- CoinLotteryStrategy
- /*** 金幣抽獎(jiǎng)模式* @author Marion* @date 2021/7/15 10:07*/ @Component public class CoinLotteryStrategy implements LotteryStrategy {/*** 獲得抽獎(jiǎng)貨幣*/@Overridepublic long amount(long uid) {return 0;}/*** 扣除抽獎(jiǎng)貨幣* @param uid* @param amount*/@Overridepublic boolean draw(long uid, long amount) {return true;} }
- CouponLotteryStrategy
- /*** 獎(jiǎng)券抽獎(jiǎng)模式* @author Marion* @date 2021/7/15 10:07,*/ @Component public class CouponLotteryStrategy implements LotteryStrategy {/*** 獲得抽獎(jiǎng)貨幣*/@Overridepublic long amount(long uid) {return 0;}/*** 扣除抽獎(jiǎng)貨幣* @param uid* @param amount*/@Overridepublic AssetTransaction draw(long uid, long amount) {return true}}
- LotteryContext
- /*** 抽獎(jiǎng)策略模式* @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);}}
三、運(yùn)行結(jié)果
總結(jié)
以上是生活随笔為你收集整理的【设计模式实战】SpringBoot模板+策略设计模式实现抽奖流程的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 应用运维工程师(技术二面)面试笔记
- 下一篇: 全景视频播放器中OpenGL的相关记录