使用工厂方法模式实现各种不同分润规则

2024-04-05 00:48

本文主要是介绍使用工厂方法模式实现各种不同分润规则,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

本人邮箱: kco1989@qq.com
欢迎转载,转载请注明网址 http://blog.csdn.net/tianshi_kco
github: https://github.com/kco1989/kco
代码已经全部托管github有需要的同学自行下载

引言

在上一篇文章中使用简单工厂编写不同的分润规则遗留着一个问题,那就是如果要新增分润规则,则需要修改原来的类.也就是代码没有完全解耦.
因此在这一篇中,我将分润规则的设计改为抽象工厂模式来编写.以解决上次遗留的问题.

改写示例

分润规则接口类

该类与上一篇是一样的.

public interface ProfitRole {double getProfit(double money);
}

分润规则抽象工厂类

public abstract class ProfitRoleFactory {public static ProfitRole createProfitRole(String profitTypeName, String expression){ProfitType profitType = ProfitType.getProfitType(profitTypeName);Matcher matcher = profitType.getPattern().matcher(expression);if (!matcher.matches()){throw new RuntimeException("分润表示时不符合" + profitType.getName() + "的规则.");}return profitType.getFactory().newProfitRole(profitType, matcher, expression);}protected abstract ProfitRole newProfitRole(ProfitType profitType, Matcher matcher, String expression);}

该类主要提供一个统一的接口ProfitRoleFactory.createProfitRole创建分润规则实现类
每一个分润规则实现类对应一个分润规则工厂类,由分润规则工厂类来创建出分润规则对象
再由分润规则对象实现具体的分润细节

分润规则类型

上一篇使用到的枚举类型.但是java不支持动态增加枚举成员.所以如果这一篇还是使用枚举类型的话,则在增加新的分润规则时难免需要修改该枚举类型.
因此,这一篇不是枚举类型.

public class ProfitType {// 非捕捉匹配正实数public static final String number = "(?:(?:[1-9]+\\d*)|(?:\\d))(?:\\.\\d+)?";// 捕捉匹配正实数public static final String realNumber = "(" + number + ")";// 捕捉匹配百分比public static final String rateNumber = realNumber + "%";// 分润规则mapprivate static final Map<String, ProfitType> profitTypeMap = new HashMap<>();static {ProfitType.registerProfitRole("FIXED_RATE","^"+ ProfitType.rateNumber +"$",new FixedRateRoleFactory(),"每笔收益率为0.1%则填写代理商收益0.1%;");ProfitType.registerProfitRole("FIXED_INCOME","^" + ProfitType.realNumber + "$",new FixedIncomeRoleFactory(),"每笔固定收益1元,则填写代理商收益1.00");ProfitType.registerProfitRole("FIXED_RATE_AND_FIXED_INCOME","^"+ ProfitType.rateNumber  + "\\+" + ProfitType.realNumber + "$",new FixedRateAndFixedIncomeRoleFactory(),"每笔收益率为0.1%加上固定收益1元,则填写代理商收益0.1%+1.00");ProfitType.registerProfitRole("FIXED_RATE_AND_UPPER_LIMIT","^"+ ProfitType.realNumber + "~" + ProfitType.rateNumber + "~" + ProfitType.realNumber  + "$",new FixedRateAndUpperLimitRoleFactory(),"每笔收益率为0.1%,封顶3元,保底1元则填写代理商收益1.00~0.1%~3.00;");ProfitType.registerProfitRole("GRADIENT_RATE","^"+ ProfitType.rateNumber+"(<"+ ProfitType.realNumber+"<"+ ProfitType.rateNumber+")+$",new GradientRateRoleFactory(),"梯度分润 例如 0.1%<10000<0.2%<20000<0.3%<30000<0.5%");}private String name;private String expression;private String description;private ProfitRoleFactory factory;public ProfitType(String name, String expression, ProfitRoleFactory factory, String description) {this.name = name;this.expression = expression;this.factory = factory;this.description = description;}public static Pattern getNumberPattern() {return Pattern.compile(number);}public String getName() {return name;}public Pattern getPattern(){return Pattern.compile(this.expression);}/*** 注册分润规则类型*/public static void registerProfitRole(String name, String profitRoleExpression,ProfitRoleFactory factory, String description){if (profitTypeMap.containsKey(name)){throw new RuntimeException("该"+name+"分润规则已经存在");}profitTypeMap.put(name, new ProfitType(name, profitRoleExpression, factory, description));}public ProfitRoleFactory getFactory() {return factory;}public String getDescription() {return description;}/*** 根据分润规则名字获取分润规则类型*/public static ProfitType getProfitType(String name){return profitTypeMap.get(name);}public static String getProfitTypeInfo(){StringBuilder sb = new StringBuilder();for (Map.Entry<String, ProfitType> entry : profitTypeMap.entrySet()){sb.append(entry.getKey() + " --> " + entry.getValue().getDescription() + "\n");}return sb.toString();}
}

ProfitType.profitTypeMap 存放分润类型名称和分润类型的key-value值
ProfitType.registerProfitRole 提供一个注册分润类型的借口
ProfitType static域 默认增加上一篇提到的5种分润规则

分润规则工厂类和分润规则实现类

目前提供五种分润规则:
1. 每笔固定收益1元,则填写代理商收益1.00
2. 每笔收益率为0.1%则填写代理商收益0.1%
3. 每笔收益率为0.1%加上固定收益1元,则填写代理商收益0.1%+1.00
4. 每笔收益率为0.1%,封顶3元,保底1元则填写代理商收益1.00~0.1%~3.00
5. 梯度分润 例如 0.1%<10000<0.2%<20000<0.3%<30000<0.5%
- 少于10000 按照 0.1% 分润
- 少于20000 按照 0.2% 分润
- 少于30000 按照 0.3% 分润
- 多于30000 按照 0.5% 分润

分别对应于分润规则工厂类 -> 分润规则实现类:

  1. FixedIncomeRoleFactory -> FixedIncomeRole
  2. FixedRateRoleFactory -> FixedRateRole
  3. FixedRateAndFixedIncomeRoleFactory -> FixedRateAndFixedIncomeRole
  4. FixedRateAndUpperLimitRoleFactory -> FixedRateAndUpperLimitRole
  5. GradientRateRoleFactory -> GradientRateRole

实现新的分润规则

如果需要实现新的分润规则,则分别编写一个分润规则工厂类和分润规则实现类,使其实分别继承(/实现)ProfitRoleFactoryProfitRole,然后再调用ProfitType.registerProfitRole注册一下新的分润规则就万事大吉了.不需要修改原有的代码.也就是实现了完全解耦.

测试类

public class TestProfitRole2 {private static final List<Double> testDate = Arrays.asList(100.0,200.0,300.0,400.0,700.0,1000.0,2000.0,3000.0,7000.0,10000.0, 20000.0, 30000.0, 70000.0);@Testpublic void test(){String profitTypeInfo = ProfitType.getProfitTypeInfo();System.out.println(profitTypeInfo);}@Testpublic void testFixedIncome(){for (double data : testDate){ProfitRole fixedIncome = ProfitRoleFactory.createProfitRole("FIXED_INCOME", "1.00");double profit = fixedIncome.getProfit(data);Assert.assertEquals(1.00, profit, 0.00001);}}@Testpublic void testFixedRate(){for (double data : testDate){ProfitRole fixedRate = ProfitRoleFactory.createProfitRole("FIXED_RATE", "0.1%");double profit = fixedRate.getProfit(data);Assert.assertEquals(data * 0.1 * 0.01, profit, 0.00001);}}@Testpublic void testFixedRateAndFixedIncome(){for (double data : testDate){ProfitRole profitRole = ProfitRoleFactory.createProfitRole("FIXED_RATE_AND_FIXED_INCOME", "0.63%+3.00");double profit = profitRole.getProfit(data);Assert.assertEquals(data * 0.63 * 0.01 + 3.0, profit, 0.00001);}}@Testpublic void testFixedRateAndUpperLimit(){for (double data : testDate){ProfitRole profitRole = ProfitRoleFactory.createProfitRole("FIXED_RATE_AND_UPPER_LIMIT", "1.00~0.1%~3.00");double profit = profitRole.getProfit(data);double actual = data * 0.1 * 0.01;if (actual < 1.0){actual = 1.0;}if (actual > 3.0){actual = 3.0;}Assert.assertEquals(actual, profit, 0.00001);}}@Testpublic void testGradientRate(){for (double data : testDate){ProfitRole profitRole = ProfitRoleFactory.createProfitRole("GRADIENT_RATE", "0.1%<1000<0.2%<5000<0.3%<15000<0.5%");double profit = profitRole.getProfit(data);if (data < 1000){Assert.assertEquals(data * 0.01 * 0.1, profit, 0.00001);}else if (data < 5000){Assert.assertEquals(data * 0.01 * 0.2, profit, 0.00001);}else if(data < 15000){Assert.assertEquals(data * 0.01 * 0.3, profit, 0.00001);}else{Assert.assertEquals(data * 0.01 * 0.5, profit, 0.00001);}}}
}

打赏

如果觉得我的文章写的还过得去的话,有钱就捧个钱场,没钱给我捧个人场(帮我点赞或推荐一下)
微信打赏支付宝打赏

这篇关于使用工厂方法模式实现各种不同分润规则的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/877246

相关文章

中文分词jieba库的使用与实景应用(一)

知识星球:https://articles.zsxq.com/id_fxvgc803qmr2.html 目录 一.定义: 精确模式(默认模式): 全模式: 搜索引擎模式: paddle 模式(基于深度学习的分词模式): 二 自定义词典 三.文本解析   调整词出现的频率 四. 关键词提取 A. 基于TF-IDF算法的关键词提取 B. 基于TextRank算法的关键词提取

使用SecondaryNameNode恢复NameNode的数据

1)需求: NameNode进程挂了并且存储的数据也丢失了,如何恢复NameNode 此种方式恢复的数据可能存在小部分数据的丢失。 2)故障模拟 (1)kill -9 NameNode进程 [lytfly@hadoop102 current]$ kill -9 19886 (2)删除NameNode存储的数据(/opt/module/hadoop-3.1.4/data/tmp/dfs/na

Hadoop数据压缩使用介绍

一、压缩原则 (1)运算密集型的Job,少用压缩 (2)IO密集型的Job,多用压缩 二、压缩算法比较 三、压缩位置选择 四、压缩参数配置 1)为了支持多种压缩/解压缩算法,Hadoop引入了编码/解码器 2)要在Hadoop中启用压缩,可以配置如下参数

Makefile简明使用教程

文章目录 规则makefile文件的基本语法:加在命令前的特殊符号:.PHONY伪目标: Makefilev1 直观写法v2 加上中间过程v3 伪目标v4 变量 make 选项-f-n-C Make 是一种流行的构建工具,常用于将源代码转换成可执行文件或者其他形式的输出文件(如库文件、文档等)。Make 可以自动化地执行编译、链接等一系列操作。 规则 makefile文件

hdu1043(八数码问题,广搜 + hash(实现状态压缩) )

利用康拓展开将一个排列映射成一个自然数,然后就变成了普通的广搜题。 #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#include<stdlib.h>#include<ctype.h>#inclu

使用opencv优化图片(画面变清晰)

文章目录 需求影响照片清晰度的因素 实现降噪测试代码 锐化空间锐化Unsharp Masking频率域锐化对比测试 对比度增强常用算法对比测试 需求 对图像进行优化,使其看起来更清晰,同时保持尺寸不变,通常涉及到图像处理技术如锐化、降噪、对比度增强等 影响照片清晰度的因素 影响照片清晰度的因素有很多,主要可以从以下几个方面来分析 1. 拍摄设备 相机传感器:相机传

2. c#从不同cs的文件调用函数

1.文件目录如下: 2. Program.cs文件的主函数如下 using System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;using System.Windows.Forms;namespace datasAnalysis{internal static

【C++】_list常用方法解析及模拟实现

相信自己的力量,只要对自己始终保持信心,尽自己最大努力去完成任何事,就算事情最终结果是失败了,努力了也不留遗憾。💓💓💓 目录   ✨说在前面 🍋知识点一:什么是list? •🌰1.list的定义 •🌰2.list的基本特性 •🌰3.常用接口介绍 🍋知识点二:list常用接口 •🌰1.默认成员函数 🔥构造函数(⭐) 🔥析构函数 •🌰2.list对象

【Prometheus】PromQL向量匹配实现不同标签的向量数据进行运算

✨✨ 欢迎大家来到景天科技苑✨✨ 🎈🎈 养成好习惯,先赞后看哦~🎈🎈 🏆 作者简介:景天科技苑 🏆《头衔》:大厂架构师,华为云开发者社区专家博主,阿里云开发者社区专家博主,CSDN全栈领域优质创作者,掘金优秀博主,51CTO博客专家等。 🏆《博客》:Python全栈,前后端开发,小程序开发,人工智能,js逆向,App逆向,网络系统安全,数据分析,Django,fastapi

让树莓派智能语音助手实现定时提醒功能

最初的时候是想直接在rasa 的chatbot上实现,因为rasa本身是带有remindschedule模块的。不过经过一番折腾后,忽然发现,chatbot上实现的定时,语音助手不一定会有响应。因为,我目前语音助手的代码设置了长时间无应答会结束对话,这样一来,chatbot定时提醒的触发就不会被语音助手获悉。那怎么让语音助手也具有定时提醒功能呢? 我最后选择的方法是用threading.Time