马府强
2 years ago
48 changed files with 553 additions and 0 deletions
@ -0,0 +1,15 @@ |
|||||
|
package com.woniu.celuejj.celue; |
||||
|
|
||||
|
public class ConcreteStrategyA implements Strategy { |
||||
|
|
||||
|
@Override |
||||
|
public String strategy() { |
||||
|
return StrategySelector.strategyA.getStrategy(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void algorithm() { |
||||
|
System.out.println("process with strategyA..."); |
||||
|
} |
||||
|
} |
||||
|
|
@ -0,0 +1,15 @@ |
|||||
|
package com.woniu.celuejj.celue; |
||||
|
|
||||
|
public class ConcreteStrategyB implements Strategy { |
||||
|
|
||||
|
@Override |
||||
|
public String strategy() { |
||||
|
return StrategySelector.strategyB.getStrategy(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void algorithm() { |
||||
|
System.out.println("process with strategyB..."); |
||||
|
} |
||||
|
} |
||||
|
|
@ -0,0 +1,16 @@ |
|||||
|
package com.woniu.celuejj.celue; |
||||
|
|
||||
|
|
||||
|
public class ConcreteStrategyC implements Strategy { |
||||
|
|
||||
|
@Override |
||||
|
public String strategy() { |
||||
|
return StrategySelector.strategyC.getStrategy(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void algorithm() { |
||||
|
System.out.println("process with strategyC..."); |
||||
|
} |
||||
|
} |
||||
|
|
@ -0,0 +1,14 @@ |
|||||
|
package com.woniu.celuejj.celue; |
||||
|
|
||||
|
|
||||
|
public interface Strategy { |
||||
|
/** |
||||
|
* 采用策略 |
||||
|
*/ |
||||
|
String strategy(); |
||||
|
|
||||
|
/** |
||||
|
* 计算方法逻辑 |
||||
|
*/ |
||||
|
void algorithm(); |
||||
|
} |
@ -0,0 +1,5 @@ |
|||||
|
package com.woniu.celuejj.celue; |
||||
|
|
||||
|
public interface StrategyRunner { |
||||
|
void execute(String strategy); |
||||
|
} |
@ -0,0 +1,23 @@ |
|||||
|
package com.woniu.celuejj.celue; |
||||
|
|
||||
|
|
||||
|
import java.util.Arrays; |
||||
|
import java.util.HashMap; |
||||
|
import java.util.List; |
||||
|
import java.util.Map; |
||||
|
import java.util.stream.Collectors; |
||||
|
|
||||
|
public class StrategyRunnerImpl implements StrategyRunner { |
||||
|
|
||||
|
private static final List<Strategy> STRATEGIES = Arrays.asList(new ConcreteStrategyA(), new ConcreteStrategyB(), new ConcreteStrategyC()); |
||||
|
private static Map<String, Strategy> STRATEGY_MAP = new HashMap<>(); |
||||
|
|
||||
|
static { |
||||
|
STRATEGY_MAP = STRATEGIES.stream().collect(Collectors.toMap(Strategy::strategy, s -> s)); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void execute(String strategy) { |
||||
|
STRATEGY_MAP.get(strategy).algorithm(); |
||||
|
} |
||||
|
} |
@ -0,0 +1,26 @@ |
|||||
|
package com.woniu.celuejj.celue; |
||||
|
|
||||
|
|
||||
|
public enum StrategySelector { |
||||
|
|
||||
|
strategyA(1, "strategyA"), |
||||
|
strategyB(2, "strategyB"), |
||||
|
strategyC(3, "strategyC"); |
||||
|
|
||||
|
private Integer code; |
||||
|
private String strategy; |
||||
|
|
||||
|
public String getStrategy() { |
||||
|
return strategy; |
||||
|
} |
||||
|
|
||||
|
public Integer getCode() { |
||||
|
return code; |
||||
|
} |
||||
|
|
||||
|
StrategySelector(Integer code, String strategy) { |
||||
|
this.code = code; |
||||
|
this.strategy = strategy; |
||||
|
} |
||||
|
} |
||||
|
|
@ -0,0 +1,19 @@ |
|||||
|
package com.woniu.celuejj.celue; |
||||
|
|
||||
|
/** |
||||
|
* 商场搞活动, |
||||
|
* 根据客户购买商品的金额, |
||||
|
* 收费时给与不同的打折, |
||||
|
* 比如, |
||||
|
* 购买 金额>=2000 的打八折(0.8), |
||||
|
* 金额 500 ~ 1000 的,打九折(0.9), |
||||
|
* 购买金额 0 ~ 500 的九五折(0.95), |
||||
|
* 根据不同的金额走不同计算策略逻辑。 |
||||
|
*/ |
||||
|
public class TeT { |
||||
|
|
||||
|
public static void main(String[] args) { |
||||
|
|
||||
|
|
||||
|
} |
||||
|
} |
@ -0,0 +1,19 @@ |
|||||
|
package com.woniu.celuejj.factory; |
||||
|
|
||||
|
import java.util.HashMap; |
||||
|
import java.util.function.Function; |
||||
|
import java.util.function.Supplier; |
||||
|
|
||||
|
public class DefaultHashMap<K, V> extends HashMap<K, V> { |
||||
|
Function<K, V> function; |
||||
|
|
||||
|
public DefaultHashMap(Supplier<V> supplier) { |
||||
|
this.function = k -> supplier.get(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
@SuppressWarnings("unchecked") |
||||
|
public V get(Object key) { |
||||
|
return super.computeIfAbsent((K) key, this.function); |
||||
|
} |
||||
|
} |
@ -0,0 +1,74 @@ |
|||||
|
package com.woniu.celuejj.factory; |
||||
|
|
||||
|
import java.util.*; |
||||
|
import java.util.stream.Collectors; |
||||
|
//Java8中方便又实用的Map函数
|
||||
|
public class Test { |
||||
|
|
||||
|
static List<User> users = Arrays.asList( |
||||
|
new User(1, 23), |
||||
|
new User(2, 25), |
||||
|
new User(3, 45), |
||||
|
new User(1, 49)); |
||||
|
|
||||
|
public static void main(String[] args) { |
||||
|
|
||||
|
Map<Integer, List<User>> userMap = new HashMap<>(); |
||||
|
for (User user : users) { |
||||
|
if (!userMap.containsKey(user.getPayType())) { |
||||
|
userMap.put(user.getPayType(), new ArrayList<>()); |
||||
|
} |
||||
|
userMap.get(user.getPayType()).add(user); |
||||
|
} |
||||
|
System.out.println(userMap); |
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
Map<Integer, List<User>> collectMap = users. |
||||
|
stream(). |
||||
|
collect(Collectors.groupingBy(item -> item.getPayType())); |
||||
|
System.out.println(collectMap); |
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
Map<Integer, List<User>> userMapNew = new HashMap<>(); |
||||
|
for (User user : users) { |
||||
|
userMapNew.computeIfAbsent(user.getPayType(), k -> new ArrayList<>()). |
||||
|
add(user); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
List<User> userNews = users; |
||||
|
Map<Integer, List<User>> userTypeMap = new DefaultHashMap<>(ArrayList::new); |
||||
|
for (User userNew : userNews) { |
||||
|
userTypeMap.get(userNew.getPayType()) |
||||
|
.add(userNew); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
|
||||
|
} |
||||
|
|
||||
|
} |
@ -0,0 +1,11 @@ |
|||||
|
package com.woniu.celuejj.factory; |
||||
|
|
||||
|
import lombok.AllArgsConstructor; |
||||
|
import lombok.Data; |
||||
|
|
||||
|
@Data |
||||
|
@AllArgsConstructor |
||||
|
public class User { |
||||
|
private Integer payType; |
||||
|
private Integer age; |
||||
|
} |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -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,14 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<project version="4"> |
||||
|
<component name="CompilerConfiguration"> |
||||
|
<annotationProcessing> |
||||
|
<profile default="true" name="Default" enabled="true" /> |
||||
|
<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,21 @@ |
|||||
|
<?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> |
||||
|
|
||||
|
|
||||
|
<dependencies> |
||||
|
<dependency> |
||||
|
<groupId>org.projectlombok</groupId> |
||||
|
<artifactId>lombok</artifactId> |
||||
|
<version>1.18.24</version> |
||||
|
</dependency> |
||||
|
</dependencies> |
||||
|
|
||||
|
|
||||
|
</project> |
@ -0,0 +1,15 @@ |
|||||
|
package com.woniu.celuejj.celue; |
||||
|
|
||||
|
public class ConcreteStrategyA implements Strategy { |
||||
|
|
||||
|
@Override |
||||
|
public String strategy() { |
||||
|
return StrategySelector.strategyA.getStrategy(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void algorithm() { |
||||
|
System.out.println("process with strategyA..."); |
||||
|
} |
||||
|
} |
||||
|
|
@ -0,0 +1,15 @@ |
|||||
|
package com.woniu.celuejj.celue; |
||||
|
|
||||
|
public class ConcreteStrategyB implements Strategy { |
||||
|
|
||||
|
@Override |
||||
|
public String strategy() { |
||||
|
return StrategySelector.strategyB.getStrategy(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void algorithm() { |
||||
|
System.out.println("process with strategyB..."); |
||||
|
} |
||||
|
} |
||||
|
|
@ -0,0 +1,16 @@ |
|||||
|
package com.woniu.celuejj.celue; |
||||
|
|
||||
|
|
||||
|
public class ConcreteStrategyC implements Strategy { |
||||
|
|
||||
|
@Override |
||||
|
public String strategy() { |
||||
|
return StrategySelector.strategyC.getStrategy(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void algorithm() { |
||||
|
System.out.println("process with strategyC..."); |
||||
|
} |
||||
|
} |
||||
|
|
@ -0,0 +1,14 @@ |
|||||
|
package com.woniu.celuejj.celue; |
||||
|
|
||||
|
|
||||
|
public interface Strategy { |
||||
|
/** |
||||
|
* 采用策略 |
||||
|
*/ |
||||
|
String strategy(); |
||||
|
|
||||
|
/** |
||||
|
* 计算方法逻辑 |
||||
|
*/ |
||||
|
void algorithm(); |
||||
|
} |
@ -0,0 +1,5 @@ |
|||||
|
package com.woniu.celuejj.celue; |
||||
|
|
||||
|
public interface StrategyRunner { |
||||
|
void execute(String strategy); |
||||
|
} |
@ -0,0 +1,23 @@ |
|||||
|
package com.woniu.celuejj.celue; |
||||
|
|
||||
|
|
||||
|
import java.util.Arrays; |
||||
|
import java.util.HashMap; |
||||
|
import java.util.List; |
||||
|
import java.util.Map; |
||||
|
import java.util.stream.Collectors; |
||||
|
|
||||
|
public class StrategyRunnerImpl implements StrategyRunner { |
||||
|
|
||||
|
private static final List<Strategy> STRATEGIES = Arrays.asList(new ConcreteStrategyA(), new ConcreteStrategyB(), new ConcreteStrategyC()); |
||||
|
private static Map<String, Strategy> STRATEGY_MAP = new HashMap<>(); |
||||
|
|
||||
|
static { |
||||
|
STRATEGY_MAP = STRATEGIES.stream().collect(Collectors.toMap(Strategy::strategy, s -> s)); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void execute(String strategy) { |
||||
|
STRATEGY_MAP.get(strategy).algorithm(); |
||||
|
} |
||||
|
} |
@ -0,0 +1,26 @@ |
|||||
|
package com.woniu.celuejj.celue; |
||||
|
|
||||
|
|
||||
|
public enum StrategySelector { |
||||
|
|
||||
|
strategyA(1, "strategyA"), |
||||
|
strategyB(2, "strategyB"), |
||||
|
strategyC(3, "strategyC"); |
||||
|
|
||||
|
private Integer code; |
||||
|
private String strategy; |
||||
|
|
||||
|
public String getStrategy() { |
||||
|
return strategy; |
||||
|
} |
||||
|
|
||||
|
public Integer getCode() { |
||||
|
return code; |
||||
|
} |
||||
|
|
||||
|
StrategySelector(Integer code, String strategy) { |
||||
|
this.code = code; |
||||
|
this.strategy = strategy; |
||||
|
} |
||||
|
} |
||||
|
|
@ -0,0 +1,19 @@ |
|||||
|
package com.woniu.celuejj.celue; |
||||
|
|
||||
|
/** |
||||
|
* 商场搞活动, |
||||
|
* 根据客户购买商品的金额, |
||||
|
* 收费时给与不同的打折, |
||||
|
* 比如, |
||||
|
* 购买 金额>=2000 的打八折(0.8), |
||||
|
* 金额 500 ~ 1000 的,打九折(0.9), |
||||
|
* 购买金额 0 ~ 500 的九五折(0.95), |
||||
|
* 根据不同的金额走不同计算策略逻辑。 |
||||
|
*/ |
||||
|
public class TeT { |
||||
|
|
||||
|
public static void main(String[] args) { |
||||
|
|
||||
|
|
||||
|
} |
||||
|
} |
@ -0,0 +1,19 @@ |
|||||
|
package com.woniu.celuejj.factory; |
||||
|
|
||||
|
import java.util.HashMap; |
||||
|
import java.util.function.Function; |
||||
|
import java.util.function.Supplier; |
||||
|
|
||||
|
public class DefaultHashMap<K, V> extends HashMap<K, V> { |
||||
|
Function<K, V> function; |
||||
|
|
||||
|
public DefaultHashMap(Supplier<V> supplier) { |
||||
|
this.function = k -> supplier.get(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
@SuppressWarnings("unchecked") |
||||
|
public V get(Object key) { |
||||
|
return super.computeIfAbsent((K) key, this.function); |
||||
|
} |
||||
|
} |
@ -0,0 +1,74 @@ |
|||||
|
package com.woniu.celuejj.factory; |
||||
|
|
||||
|
import java.util.*; |
||||
|
import java.util.stream.Collectors; |
||||
|
//Java8中方便又实用的Map函数
|
||||
|
public class Test { |
||||
|
|
||||
|
static List<User> users = Arrays.asList( |
||||
|
new User(1, 23), |
||||
|
new User(2, 25), |
||||
|
new User(3, 45), |
||||
|
new User(1, 49)); |
||||
|
|
||||
|
public static void main(String[] args) { |
||||
|
|
||||
|
Map<Integer, List<User>> userMap = new HashMap<>(); |
||||
|
for (User user : users) { |
||||
|
if (!userMap.containsKey(user.getPayType())) { |
||||
|
userMap.put(user.getPayType(), new ArrayList<>()); |
||||
|
} |
||||
|
userMap.get(user.getPayType()).add(user); |
||||
|
} |
||||
|
System.out.println(userMap); |
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
Map<Integer, List<User>> collectMap = users. |
||||
|
stream(). |
||||
|
collect(Collectors.groupingBy(item -> item.getPayType())); |
||||
|
System.out.println(collectMap); |
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
Map<Integer, List<User>> userMapNew = new HashMap<>(); |
||||
|
for (User user : users) { |
||||
|
userMapNew.computeIfAbsent(user.getPayType(), k -> new ArrayList<>()). |
||||
|
add(user); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
List<User> userNews = users; |
||||
|
Map<Integer, List<User>> userTypeMap = new DefaultHashMap<>(ArrayList::new); |
||||
|
for (User userNew : userNews) { |
||||
|
userTypeMap.get(userNew.getPayType()) |
||||
|
.add(userNew); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
|
||||
|
} |
||||
|
|
||||
|
} |
@ -0,0 +1,11 @@ |
|||||
|
package com.woniu.celuejj.factory; |
||||
|
|
||||
|
import lombok.AllArgsConstructor; |
||||
|
import lombok.Data; |
||||
|
|
||||
|
@Data |
||||
|
@AllArgsConstructor |
||||
|
public class User { |
||||
|
private Integer payType; |
||||
|
private Integer age; |
||||
|
} |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in new issue