当前位置:网站首页>【无标题】
【无标题】
2022-07-23 04:43:00 【看表该更新博客了】
设计模式——策略模式(代码简述)
1.先定义功能—优惠券接口及其方法
/** * 优惠券接口 */
// 泛型,不同类型的接口传递不同的类型参数
public interface ICouponDiscount<T> {
/** * * @param couponInfo 优惠券折扣信息 * @param skuPrice SKU 金额 * @return 优惠后价格 */
BigDecimal discountAmount(T couponInfo,BigDecimal skuPrice);
}
2.不同优惠券不同玩法
-—满减优惠券
/** * 满减优惠券 */
public class MJCouponDiscount implements ICouponDiscount<Map<String,BigDecimal>>{
@Override
public BigDecimal discountAmount(Map<String, BigDecimal> couponInfo, BigDecimal skuPrice) {
// map 满 x 减 n
BigDecimal x = couponInfo.get("x");
BigDecimal n = couponInfo.get("n");
// 销售价小于x,返回销售价
if (skuPrice.compareTo(x)<0) {
return skuPrice;
}
return skuPrice.subtract(n);
}
—折扣商品
/** * 折扣商品 */
public class ZKCoupon implements ICoupon<Double>{
/** * * @param zkCouponInfo 折扣 * @param skuPrice 商品原价 * @return */
@Override
public BigDecimal handlePrice(Double zkCouponInfo, BigDecimal skuPrice){
return skuPrice.multiply(new BigDecimal(zkCouponInfo)).setScale(2,BigDecimal.ROUND_HALF_UP);
}
}
3.统一对象
public class Context<T> {
// 属性为接口对象
private ICoupon iCoupon;
public BigDecimal handle(T t, BigDecimal skuPrice) {
return iCoupon.handlePrice(t, skuPrice);
}
// 构造,参数传实现了ICoupon接口的实现类对象
public Context(ICoupon iCoupon) {
this.iCoupon = iCoupon;
}
}
4.测试类
public class Test {
public static void main(String[] args) {
// 商品原价
BigDecimal skuPrice = BigDecimal.valueOf(100);
// 折扣商品计算
// 6折
Double zk = Double.valueOf(0.6);
Context<Double> zkContext = new Context(new ZKCoupon()); // 构造函数
BigDecimal zkPrice = zkContext.handle(zk, skuPrice);
System.out.println("折扣后价格:"+zkPrice);
// 满减商品计算
Map<String,BigDecimal> map = new HashMap<>();
map.put("x",BigDecimal.valueOf(50));
map.put("n",BigDecimal.valueOf(20));
Context<Map<String, BigDecimal>> mjContext = new Context<>(new MJCoupon());
BigDecimal mjPrice = mjContext.handle(map, skuPrice);
System.out.println("满减后价格:"+mjPrice);
}
}
context的设计(类后泛型,构造函数)最为重要
边栏推荐
- C# EventHandler观察者模式
- CV (3)- CNNs
- HBV parameter extraction and fitting [draft]
- 【Delphi】制作控件面板安装图标的简单方法(译)
- 300 题 第六讲 二次型
- MapReduce进阶
- Meituan's 8-year experience on how to improve test engineers (automation, performance, test development)
- 20.有效的括号
- Flutter 运行flutter pub get 报错“客户端没有所需特权“
- CLion + MinGW64配置C语言开发环境 Visual Studio安装
猜你喜欢

Chapter 4: runtime data area - shared space

Kubernetes技术与架构(六)

Flask learning notes

【Unity】AVPro使用踩坑,编辑器模式使用视频播放正常,打包后视频无法播放的问题

What is file management software? Why do you need it?

网络安全等级保护2.0标准解析

数据湖:Apache Iceberg介绍

中国经济网:“元宇宙”炙手可热

How to protect the copyright of NFT digital collections?

The wave of the meta universe is shocking. Seize the opportunity and work together
随机推荐
NFT数字藏品版权如何保护?
Network data leakage events occur frequently, how to protect personal privacy information?
软件测试基础篇—测试用例的设计方法
Figure 8 sequence of crystal head connection of network cable
No routines, no traps, no advertisements | are you sure you don't need this free instant messaging software?
写驱动程序的时候warning LNK4210报错
2022/7/20
Important knowledge of application layer (interview, reexamination, term end)
C语言基础知识梳理(一)
Unityc realizes the conversion of Chinese characters to Pinyin - using Microsoft chspinyinconv Library
Chapter2 Standard Output
How Alibaba cloud resolves a domain name to another domain name
[warning] recognizing corrupt image/label during yolov5 training: [errno 2]...... it is impossible to complete the training data set. I will take you to solve it quickly
MySql 数据库表名命名规则---方便自动转换工具转换
MySQL query optimization - detailed explanation
编译构建工具-bazel
Common neural network parameters and calculations
Naming rules of MySQL database table names -- convenient for automatic conversion tools
hbv参数提取和拟合[草稿]
Chapter2 Standard Output