diff --git a/celuemapmoshi/.idea/.gitignore b/celuemapmoshi/.idea/.gitignore new file mode 100644 index 0000000..c386304 --- /dev/null +++ b/celuemapmoshi/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/../../../../:\java project\celuemapmoshi\.idea/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/celuemapmoshi/.idea/compiler.xml b/celuemapmoshi/.idea/compiler.xml new file mode 100644 index 0000000..3432491 --- /dev/null +++ b/celuemapmoshi/.idea/compiler.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/celuemapmoshi/.idea/encodings.xml b/celuemapmoshi/.idea/encodings.xml new file mode 100644 index 0000000..63e9001 --- /dev/null +++ b/celuemapmoshi/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/celuemapmoshi/.idea/jarRepositories.xml b/celuemapmoshi/.idea/jarRepositories.xml new file mode 100644 index 0000000..712ab9d --- /dev/null +++ b/celuemapmoshi/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/celuemapmoshi/.idea/misc.xml b/celuemapmoshi/.idea/misc.xml new file mode 100644 index 0000000..4b661a5 --- /dev/null +++ b/celuemapmoshi/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/celuemapmoshi/celuemapmoshi.iml b/celuemapmoshi/celuemapmoshi.iml new file mode 100644 index 0000000..78b2cc5 --- /dev/null +++ b/celuemapmoshi/celuemapmoshi.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/celuemapmoshi/pom.xml b/celuemapmoshi/pom.xml new file mode 100644 index 0000000..45f0cc2 --- /dev/null +++ b/celuemapmoshi/pom.xml @@ -0,0 +1,67 @@ + + + 4.0.0 + + com.celuemoshi + celuemapmoshi + 1.0-SNAPSHOT + + + org.springframework.boot + spring-boot-starter-parent + 2.3.10.RELEASE + + + Demo project for Spring Boot + + 1.8 + + + + com.baomidou + mybatis-plus-boot-starter + 3.4.2 + + + org.springframework.boot + spring-boot-starter-web + + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.projectlombok + lombok + + + + + + + + \ No newline at end of file diff --git a/celuemapmoshi/src/main/java/com/celuemoshi/celuemapmoshi/CeLueTest.java b/celuemapmoshi/src/main/java/com/celuemoshi/celuemapmoshi/CeLueTest.java new file mode 100644 index 0000000..0459b63 --- /dev/null +++ b/celuemapmoshi/src/main/java/com/celuemoshi/celuemapmoshi/CeLueTest.java @@ -0,0 +1,50 @@ +package com.celuemoshi.celuemapmoshi; + +/** + * 还在用策略模式解决 if-else? + * Map + 函数式接口来帮你搞定! + * + * 根据优惠券类型resourceType -> 确定查询哪个数据表 + * 根据编码resourceId -> 到对应的数据表里边查询优惠券的派发方式 + * + * 优惠券有多种类型,分别对应了不同的数据库表: + * 红包 —— 红包发放规则表 + * 购物券 —— 购物券表 + * ... + */ +public class CeLueTest { + + public static void main(String[] args) { + String resourceType = ""; + String resourceId = ""; + switch (resourceType) { + case "红包": +// 查询红包的派发方式 + break; + case "购物券": +// 查询购物券的派发方式 + break; + default: + System.out.println("查找不到该优惠券类型resourceType以及对应的派发方式"); + break; + } + +//策略模式优化之后 + String grantType = ""; + switch (resourceType) { + case "红包": +// 查询红包的派发方式 + grantType = new Content(new RedPaperStrategy()).contentStrategy(resourceId); + break; + case "购物券": +// 查询购物券的派发方式 + grantType = new Content(new ShoppingStrategy()).contentStrategy(resourceId); + break; + default: + System.out.println("查找不到该优惠券类型resourceType以及对应的派发方式"); + break; + } + + + } +} diff --git a/celuemapmoshi/src/main/java/com/celuemoshi/celuemapmoshi/Content.java b/celuemapmoshi/src/main/java/com/celuemoshi/celuemapmoshi/Content.java new file mode 100644 index 0000000..cbdaa44 --- /dev/null +++ b/celuemapmoshi/src/main/java/com/celuemoshi/celuemapmoshi/Content.java @@ -0,0 +1,14 @@ +package com.celuemoshi.celuemapmoshi; + +public class Content { + Strategy strategy; + + public Content(Strategy strategy) { + this.strategy = strategy; + } + + public String contentStrategy(String resourceId) { + return strategy.query(resourceId); + } + +} diff --git a/celuemapmoshi/src/main/java/com/celuemoshi/celuemapmoshi/GrantTypeController.java b/celuemapmoshi/src/main/java/com/celuemoshi/celuemapmoshi/GrantTypeController.java new file mode 100644 index 0000000..3c09e83 --- /dev/null +++ b/celuemapmoshi/src/main/java/com/celuemoshi/celuemapmoshi/GrantTypeController.java @@ -0,0 +1,15 @@ +package com.celuemoshi.celuemapmoshi; + + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PostMapping; + +public class GrantTypeController { + @Autowired + private QueryGrantTypeService queryGrantTypeService; + + @PostMapping("/grantType") + public String test(String resourceName,String resourceId) { + return queryGrantTypeService.getResult(resourceName,resourceId); + } +} diff --git a/celuemapmoshi/src/main/java/com/celuemoshi/celuemapmoshi/GrantTypeSerive.java b/celuemapmoshi/src/main/java/com/celuemoshi/celuemapmoshi/GrantTypeSerive.java new file mode 100644 index 0000000..1e0595e --- /dev/null +++ b/celuemapmoshi/src/main/java/com/celuemoshi/celuemapmoshi/GrantTypeSerive.java @@ -0,0 +1,22 @@ +package com.celuemoshi.celuemapmoshi; + +import org.springframework.stereotype.Service; + +@Service +public class GrantTypeSerive { + + public String redPaper(String resourceId) { + //红包的发放方式 + return "每周末9点发放"; + } + + public String shopping(String resourceId) { + //购物券的发放方式 + return "每周三9点发放"; + } + + public String QQVip(String resourceId) { + //qq会员的发放方式 + return "每周一0点开始秒杀"; + } +} diff --git a/celuemapmoshi/src/main/java/com/celuemoshi/celuemapmoshi/QueryGrantTypeService.java b/celuemapmoshi/src/main/java/com/celuemoshi/celuemapmoshi/QueryGrantTypeService.java new file mode 100644 index 0000000..e53e5ab --- /dev/null +++ b/celuemapmoshi/src/main/java/com/celuemoshi/celuemapmoshi/QueryGrantTypeService.java @@ -0,0 +1,42 @@ +package com.celuemoshi.celuemapmoshi; + + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import javax.annotation.PostConstruct; +import java.util.HashMap; +import java.util.Map; +import java.util.function.Function; + +@Service +public class QueryGrantTypeService { + + @Autowired + private GrantTypeSerive grantTypeSerive; + + private Map> grantTypeMap = new HashMap<>(); + + /** + * 初始化业务分派逻辑,代替了if-else部分 + * key: 优惠券类型 + * value: lambda表达式,最终会获得该优惠券的发放方式 + */ + @PostConstruct + public void dispatcherInit() { + grantTypeMap.put("红包", resourceId -> grantTypeSerive.redPaper(resourceId)); + grantTypeMap.put("购物券", resourceId -> grantTypeSerive.shopping(resourceId)); + grantTypeMap.put("qq会员", resourceId -> grantTypeSerive.QQVip(resourceId)); + } + + public String getResult(String resourceType, String resourceId) { + //Controller根据 优惠券类型resourceType、编码resourceId 去查询 发放方式grantType + Function result = grantTypeMap.get(resourceType); + if (result != null) { + //传入resourceId 执行这段表达式获得String型的grantType + return result.apply(resourceId); + } + return "查询不到该优惠券的发放方式"; + } +} + diff --git a/celuemapmoshi/src/main/java/com/celuemoshi/celuemapmoshi/RedPaperStrategy.java b/celuemapmoshi/src/main/java/com/celuemoshi/celuemapmoshi/RedPaperStrategy.java new file mode 100644 index 0000000..4b215c1 --- /dev/null +++ b/celuemapmoshi/src/main/java/com/celuemoshi/celuemapmoshi/RedPaperStrategy.java @@ -0,0 +1,13 @@ +package com.celuemoshi.celuemapmoshi; + +/** + * 查询红包的派发方式 + */ +public class RedPaperStrategy implements Strategy { + + + @Override + public String query(String resourceId) { + return "每周末9点发放"; + } +} diff --git a/celuemapmoshi/src/main/java/com/celuemoshi/celuemapmoshi/ShoppingStrategy.java b/celuemapmoshi/src/main/java/com/celuemoshi/celuemapmoshi/ShoppingStrategy.java new file mode 100644 index 0000000..b1c7880 --- /dev/null +++ b/celuemapmoshi/src/main/java/com/celuemoshi/celuemapmoshi/ShoppingStrategy.java @@ -0,0 +1,13 @@ +package com.celuemoshi.celuemapmoshi; + +/** + * 查询购物卷的派发方式 + */ +public class ShoppingStrategy implements Strategy { + + + @Override + public String query(String resourceId) { + return "每周三20点发放"; + } +} diff --git a/celuemapmoshi/src/main/java/com/celuemoshi/celuemapmoshi/Strategy.java b/celuemapmoshi/src/main/java/com/celuemoshi/celuemapmoshi/Strategy.java new file mode 100644 index 0000000..d35d311 --- /dev/null +++ b/celuemapmoshi/src/main/java/com/celuemoshi/celuemapmoshi/Strategy.java @@ -0,0 +1,10 @@ +package com.celuemoshi.celuemapmoshi; + +/** + * @className: Strategy + * @author: 蜗牛 + * @date: 2023/1/30 + **/ +public interface Strategy { + String query(String resourceId); +}