15 changed files with 314 additions and 0 deletions
@ -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/ |
@ -0,0 +1,18 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<project version="4"> |
||||
|
<component name="CompilerConfiguration"> |
||||
|
<annotationProcessing> |
||||
|
<profile name="Maven default annotation processors profile" enabled="true"> |
||||
|
<sourceOutputDir name="target/generated-sources/annotations" /> |
||||
|
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" /> |
||||
|
<outputRelativeToContentRoot value="true" /> |
||||
|
<module name="celuemapmoshi" /> |
||||
|
</profile> |
||||
|
</annotationProcessing> |
||||
|
</component> |
||||
|
<component name="JavacSettings"> |
||||
|
<option name="ADDITIONAL_OPTIONS_OVERRIDE"> |
||||
|
<module name="celuemapmoshi" options="-parameters" /> |
||||
|
</option> |
||||
|
</component> |
||||
|
</project> |
@ -0,0 +1,6 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<project version="4"> |
||||
|
<component name="Encoding"> |
||||
|
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" /> |
||||
|
</component> |
||||
|
</project> |
@ -0,0 +1,20 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<project version="4"> |
||||
|
<component name="RemoteRepositoriesConfiguration"> |
||||
|
<remote-repository> |
||||
|
<option name="id" value="central" /> |
||||
|
<option name="name" value="Central Repository" /> |
||||
|
<option name="url" value="https://repo.maven.apache.org/maven2" /> |
||||
|
</remote-repository> |
||||
|
<remote-repository> |
||||
|
<option name="id" value="central" /> |
||||
|
<option name="name" value="Maven Central repository" /> |
||||
|
<option name="url" value="https://repo1.maven.org/maven2" /> |
||||
|
</remote-repository> |
||||
|
<remote-repository> |
||||
|
<option name="id" value="jboss.community" /> |
||||
|
<option name="name" value="JBoss Community repository" /> |
||||
|
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" /> |
||||
|
</remote-repository> |
||||
|
</component> |
||||
|
</project> |
@ -0,0 +1,14 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<project version="4"> |
||||
|
<component name="ExternalStorageConfigurationManager" enabled="true" /> |
||||
|
<component name="MavenProjectsManager"> |
||||
|
<option name="originalFiles"> |
||||
|
<list> |
||||
|
<option value="$PROJECT_DIR$/pom.xml" /> |
||||
|
</list> |
||||
|
</option> |
||||
|
</component> |
||||
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK"> |
||||
|
<output url="file://$PROJECT_DIR$/out" /> |
||||
|
</component> |
||||
|
</project> |
@ -0,0 +1,2 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<module type="JAVA_MODULE" version="4" /> |
@ -0,0 +1,67 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" |
||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
||||
|
<modelVersion>4.0.0</modelVersion> |
||||
|
|
||||
|
<groupId>com.celuemoshi</groupId> |
||||
|
<artifactId>celuemapmoshi</artifactId> |
||||
|
<version>1.0-SNAPSHOT</version> |
||||
|
|
||||
|
<parent> |
||||
|
<groupId>org.springframework.boot</groupId> |
||||
|
<artifactId>spring-boot-starter-parent</artifactId> |
||||
|
<version>2.3.10.RELEASE</version> |
||||
|
<relativePath/> <!-- lookup parent from repository --> |
||||
|
</parent> |
||||
|
<description>Demo project for Spring Boot</description> |
||||
|
<properties> |
||||
|
<java.version>1.8</java.version> |
||||
|
</properties> |
||||
|
<dependencies> |
||||
|
<dependency> |
||||
|
<groupId>com.baomidou</groupId> |
||||
|
<artifactId>mybatis-plus-boot-starter</artifactId> |
||||
|
<version>3.4.2</version> |
||||
|
</dependency> |
||||
|
<dependency> |
||||
|
<groupId>org.springframework.boot</groupId> |
||||
|
<artifactId>spring-boot-starter-web</artifactId> |
||||
|
</dependency> |
||||
|
|
||||
|
<dependency> |
||||
|
<groupId>org.projectlombok</groupId> |
||||
|
<artifactId>lombok</artifactId> |
||||
|
<optional>true</optional> |
||||
|
</dependency> |
||||
|
<dependency> |
||||
|
<groupId>org.springframework.boot</groupId> |
||||
|
<artifactId>spring-boot-starter-test</artifactId> |
||||
|
<scope>test</scope> |
||||
|
<exclusions> |
||||
|
<exclusion> |
||||
|
<groupId>org.junit.vintage</groupId> |
||||
|
<artifactId>junit-vintage-engine</artifactId> |
||||
|
</exclusion> |
||||
|
</exclusions> |
||||
|
</dependency> |
||||
|
</dependencies> |
||||
|
|
||||
|
<build> |
||||
|
<plugins> |
||||
|
<plugin> |
||||
|
<groupId>org.springframework.boot</groupId> |
||||
|
<artifactId>spring-boot-maven-plugin</artifactId> |
||||
|
<configuration> |
||||
|
<excludes> |
||||
|
<exclude> |
||||
|
<groupId>org.projectlombok</groupId> |
||||
|
<artifactId>lombok</artifactId> |
||||
|
</exclude> |
||||
|
</excludes> |
||||
|
</configuration> |
||||
|
</plugin> |
||||
|
</plugins> |
||||
|
</build> |
||||
|
|
||||
|
</project> |
@ -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; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
||||
|
} |
@ -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); |
||||
|
} |
||||
|
|
||||
|
} |
@ -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); |
||||
|
} |
||||
|
} |
@ -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点开始秒杀"; |
||||
|
} |
||||
|
} |
@ -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<String, Function<String, String>> 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<String, String> result = grantTypeMap.get(resourceType); |
||||
|
if (result != null) { |
||||
|
//传入resourceId 执行这段表达式获得String型的grantType
|
||||
|
return result.apply(resourceId); |
||||
|
} |
||||
|
return "查询不到该优惠券的发放方式"; |
||||
|
} |
||||
|
} |
||||
|
|
@ -0,0 +1,13 @@ |
|||||
|
package com.celuemoshi.celuemapmoshi; |
||||
|
|
||||
|
/** |
||||
|
* 查询红包的派发方式 |
||||
|
*/ |
||||
|
public class RedPaperStrategy implements Strategy { |
||||
|
|
||||
|
|
||||
|
@Override |
||||
|
public String query(String resourceId) { |
||||
|
return "每周末9点发放"; |
||||
|
} |
||||
|
} |
@ -0,0 +1,13 @@ |
|||||
|
package com.celuemoshi.celuemapmoshi; |
||||
|
|
||||
|
/** |
||||
|
* 查询购物卷的派发方式 |
||||
|
*/ |
||||
|
public class ShoppingStrategy implements Strategy { |
||||
|
|
||||
|
|
||||
|
@Override |
||||
|
public String query(String resourceId) { |
||||
|
return "每周三20点发放"; |
||||
|
} |
||||
|
} |
@ -0,0 +1,10 @@ |
|||||
|
package com.celuemoshi.celuemapmoshi; |
||||
|
|
||||
|
/** |
||||
|
* @className: Strategy |
||||
|
* @author: 蜗牛 |
||||
|
* @date: 2023/1/30 |
||||
|
**/ |
||||
|
public interface Strategy { |
||||
|
String query(String resourceId); |
||||
|
} |
Loading…
Reference in new issue