Browse Source

优化策略模式

pull/1/head
马府强 2 years ago
parent
commit
ac44d08ced
  1. 8
      celuemoshijingjian/.idea/.gitignore
  2. 13
      celuemoshijingjian/.idea/compiler.xml
  3. 20
      celuemoshijingjian/.idea/jarRepositories.xml
  4. 14
      celuemoshijingjian/.idea/misc.xml
  5. 2
      celuemoshijingjian/celuemoshijingjian.iml
  6. 12
      celuemoshijingjian/pom.xml
  7. 28
      celuemoshijingjian/src/main/java/com/woniu/celuejj/FunctionStrategy.java
  8. 56
      celuemoshijingjian/src/main/java/com/woniu/celuejj/ReturnHomeStrategy.java
  9. 29
      celuemoshijingjian/src/main/java/com/woniu/celuejj/TestMain.java
  10. 14
      celuemoshijingjian/src/main/java/com/woniu/celuejj/celueyuanxing/FunctionStrategy.java
  11. 10
      celuemoshijingjian/src/main/java/com/woniu/celuejj/celueyuanxing/HighRiskFunctionStrategy.java
  12. 10
      celuemoshijingjian/src/main/java/com/woniu/celuejj/celueyuanxing/LowRiskStrategy.java
  13. 9
      celuemoshijingjian/src/main/java/com/woniu/celuejj/celueyuanxing/MiddleRiskStrategy.java
  14. BIN
      celuemoshijingjian/target/classes/com/woniu/celuejj/FunctionStrategy.class
  15. BIN
      celuemoshijingjian/target/classes/com/woniu/celuejj/ReturnHomeStrategy.class

8
celuemoshijingjian/.idea/.gitignore

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Datasource local storage ignored files
/../../../../:\java project\celuemoshijingjian\.idea/dataSources/
/dataSources.local.xml
# Editor-based HTTP Client requests
/httpRequests/

13
celuemoshijingjian/.idea/compiler.xml

@ -0,0 +1,13 @@
<?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="celuemoshijingjian" />
</profile>
</annotationProcessing>
</component>
</project>

20
celuemoshijingjian/.idea/jarRepositories.xml

@ -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>

14
celuemoshijingjian/.idea/misc.xml

@ -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>

2
celuemoshijingjian/celuemoshijingjian.iml

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4" />

12
celuemoshijingjian/pom.xml

@ -0,0 +1,12 @@
<?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.woniu</groupId>
<artifactId>celuemoshijingjian</artifactId>
<version>1.0-SNAPSHOT</version>
</project>

28
celuemoshijingjian/src/main/java/com/woniu/celuejj/FunctionStrategy.java

@ -0,0 +1,28 @@
package com.woniu.celuejj;
import java.util.function.Function;
import java.util.function.Predicate;
/**
* 用java 8的函数式接口和泛型 优化策略模式简化写法
* 进而优化if else逻辑
* @className: FunctionStrategy
* @author: woniu
* @date: 2023/2/11
**/
public interface FunctionStrategy<P, T, R> {
/**
* 暴露当前策略的钩子
*
* @return 判断钩子
*/
Predicate<P> predicate();
/**
* 暴露当前策略的生产逻辑
*
* @return 消费逻辑
*/
Function<T, R> function();
}

56
celuemoshijingjian/src/main/java/com/woniu/celuejj/ReturnHomeStrategy.java

@ -0,0 +1,56 @@
package com.woniu.celuejj;
import java.util.function.Function;
import java.util.function.Predicate;
/**
* 策略模式简化写法 来优化if else逻辑
* String 代表来自区域风险系数
* Integer 打工人
* String 村里开具证明
*/
enum ReturnHomeStrategy implements FunctionStrategy<String, Integer, String> {
/**
* 高风险
*/
HIGH_RISK(from -> "HIGH_RISK".equals(from), i -> i+"在高风险回来了,get out!"),
/**
* 中风险
*/
MIDDLE_RISK(from -> "MIDDLE_RISK".equals(from), i -> i+"在中风险回来了,get out!"),
/**
* 低风险
*/
LOW_RISK(from -> "LOW_RISK".equals(from), i -> i+"在低风险回来了,拉走先做个核酸检测!");
private final Predicate<String> predicate;
private final Function<Integer, String> function;
public Predicate<String> getPredicate() {
return predicate;
}
public Function<Integer, String> getFunction() {
return function;
}
ReturnHomeStrategy(Predicate<String> predicate, Function<Integer, String> function) {
this.predicate = predicate;
this.function = function;
}
@Override
public Predicate<String> predicate() {
return this.predicate;
}
@Override
public Function<Integer, String> function() {
return this.function;
}
}

29
celuemoshijingjian/src/main/java/com/woniu/celuejj/TestMain.java

@ -0,0 +1,29 @@
package com.woniu.celuejj;
/**
* 策略模式简化写法 来优化if else逻辑
*/
public class TestMain {
public static void main(String[] args) {
String low_risk = returnHome("LOW_RISK", 1);
System.out.println(low_risk);
}
/**
* 某人回村
* @param from 代表区域风险系数
* @param id 打工人
* @return 要证明
*/
public static String returnHome(String from, Integer id) {
for (ReturnHomeStrategy value : ReturnHomeStrategy.values()) {
if (value.predicate().test(from)) {
return value.function().apply(id);
}
}
throw new RuntimeException("外星人,抓起来放进动物园卖门票!");
}
}

14
celuemoshijingjian/src/main/java/com/woniu/celuejj/celueyuanxing/FunctionStrategy.java

@ -0,0 +1,14 @@
package com.woniu.celuejj.celueyuanxing;
/**
* 用java 8的函数式接口和泛型 优化策略模式简化写法
* 进而优化if else逻辑
* 疫情期间 某人回村为例
* @className: FunctionStrategy
* @author: woniu
* @date: 2023/2/11
**/
public interface FunctionStrategy {
void huicun();
}

10
celuemoshijingjian/src/main/java/com/woniu/celuejj/celueyuanxing/HighRiskFunctionStrategy.java

@ -0,0 +1,10 @@
package com.woniu.celuejj.celueyuanxing;
public class HighRiskFunctionStrategy implements FunctionStrategy {
@Override
public void huicun() {
System.out.println("在高风险回来了,get out!");
}
}

10
celuemoshijingjian/src/main/java/com/woniu/celuejj/celueyuanxing/LowRiskStrategy.java

@ -0,0 +1,10 @@
package com.woniu.celuejj.celueyuanxing;
public class LowRiskStrategy implements FunctionStrategy {
@Override
public void huicun() {
System.out.println("在低风险回来了,先做个核酸吧!");
}
}

9
celuemoshijingjian/src/main/java/com/woniu/celuejj/celueyuanxing/MiddleRiskStrategy.java

@ -0,0 +1,9 @@
package com.woniu.celuejj.celueyuanxing;
public class MiddleRiskStrategy implements FunctionStrategy {
@Override
public void huicun() {
System.out.println("在中风险回来了,get out!");
}
}

BIN
celuemoshijingjian/target/classes/com/woniu/celuejj/FunctionStrategy.class

Binary file not shown.

BIN
celuemoshijingjian/target/classes/com/woniu/celuejj/ReturnHomeStrategy.class

Binary file not shown.
Loading…
Cancel
Save