马府强
2 years ago
15 changed files with 225 additions and 0 deletions
@ -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/ |
@ -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> |
@ -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,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> |
@ -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(); |
|||
} |
@ -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; |
|||
} |
|||
|
|||
|
|||
|
|||
} |
@ -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("外星人,抓起来放进动物园卖门票!"); |
|||
} |
|||
} |
@ -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(); |
|||
} |
@ -0,0 +1,10 @@ |
|||
package com.woniu.celuejj.celueyuanxing; |
|||
|
|||
|
|||
public class HighRiskFunctionStrategy implements FunctionStrategy { |
|||
|
|||
@Override |
|||
public void huicun() { |
|||
System.out.println("在高风险回来了,get out!"); |
|||
} |
|||
} |
@ -0,0 +1,10 @@ |
|||
package com.woniu.celuejj.celueyuanxing; |
|||
|
|||
public class LowRiskStrategy implements FunctionStrategy { |
|||
|
|||
|
|||
@Override |
|||
public void huicun() { |
|||
System.out.println("在低风险回来了,先做个核酸吧!"); |
|||
} |
|||
} |
@ -0,0 +1,9 @@ |
|||
package com.woniu.celuejj.celueyuanxing; |
|||
|
|||
|
|||
public class MiddleRiskStrategy implements FunctionStrategy { |
|||
@Override |
|||
public void huicun() { |
|||
System.out.println("在中风险回来了,get out!"); |
|||
} |
|||
} |
Binary file not shown.
Binary file not shown.
Loading…
Reference in new issue