diff --git a/shejimoshidemo/.idea/.gitignore b/shejimoshidemo/.idea/.gitignore new file mode 100644 index 0000000..466489f --- /dev/null +++ b/shejimoshidemo/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/../../../:\shejimoshidemo\.idea/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/shejimoshidemo/.idea/compiler.xml b/shejimoshidemo/.idea/compiler.xml new file mode 100644 index 0000000..9cca9a0 --- /dev/null +++ b/shejimoshidemo/.idea/compiler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/shejimoshidemo/.idea/jarRepositories.xml b/shejimoshidemo/.idea/jarRepositories.xml new file mode 100644 index 0000000..40a2eee --- /dev/null +++ b/shejimoshidemo/.idea/jarRepositories.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/shejimoshidemo/.idea/misc.xml b/shejimoshidemo/.idea/misc.xml new file mode 100644 index 0000000..4b661a5 --- /dev/null +++ b/shejimoshidemo/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/shejimoshidemo/pom.xml b/shejimoshidemo/pom.xml new file mode 100644 index 0000000..b7192fe --- /dev/null +++ b/shejimoshidemo/pom.xml @@ -0,0 +1,37 @@ + + + 4.0.0 + + com.woniu + shejimoshidemo + 1.0-SNAPSHOT + + + 8 + 8 + + + + + + + + org.springframework + spring + 5.3.25 + pom + + + + + org.springframework.boot + spring-boot-starter-web + 3.0.2 + + + + + + \ No newline at end of file diff --git a/shejimoshidemo/shejimoshidemo.iml b/shejimoshidemo/shejimoshidemo.iml new file mode 100644 index 0000000..78b2cc5 --- /dev/null +++ b/shejimoshidemo/shejimoshidemo.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/shejimoshidemo/src/main/java/com/woniu/design/AbstractHandler.java b/shejimoshidemo/src/main/java/com/woniu/design/AbstractHandler.java new file mode 100644 index 0000000..fb4461c --- /dev/null +++ b/shejimoshidemo/src/main/java/com/woniu/design/AbstractHandler.java @@ -0,0 +1,17 @@ +package com.woniu.design; + +import org.springframework.beans.factory.InitializingBean; + +public abstract class AbstractHandler implements InitializingBean { + + public void AA(String nikeName) { + throw new UnsupportedOperationException(); + } + + + public void BB(String nikeName) { + throw new UnsupportedOperationException(); + } + + +} diff --git a/shejimoshidemo/src/main/java/com/woniu/design/Demo.java b/shejimoshidemo/src/main/java/com/woniu/design/Demo.java new file mode 100644 index 0000000..b15958a --- /dev/null +++ b/shejimoshidemo/src/main/java/com/woniu/design/Demo.java @@ -0,0 +1,45 @@ +package com.woniu.design; + +/** + * 用策略模式和工厂模式以及模板方法模式优化代码中的if else + */ +public class Demo { + public static void main(String[] args) { + String nickName = "蜗牛"; + if ("皮球".equals(nickName)) { + //业务逻辑 +// System.out.println("我是皮球"); + new PiQiuHandler().AA("皮球"); + }else if ("蜗牛".equals(nickName)){ + //业务逻辑 +// System.out.println("我是蜗牛"); + new WoNiuHandler().AA("蜗牛"); + }else if ("牛蛙".equals(nickName)){ + //业务逻辑 + System.out.println("我是牛蛙"); + System.out.println("呱呱呱"); + } + + String nickName2 = "woniu"; +// Handler invokeStrategy = Factory.getInvokeStrategy(nickName2); +// invokeStrategy.AA(nickName2); + + AbstractHandler invokeStrategy = Factory2.getInvokeStrategy(nickName2); + invokeStrategy.AA(nickName2); + + invokeStrategy.BB(nickName2); + } + + private static void extracted() { + String nickName = "woniu"; + AbstractHandler invokeStrategy = Factory2.getInvokeStrategy(nickName); + + invokeStrategy.AA(nickName); + + invokeStrategy.BB(nickName); + + + } + + +} diff --git a/shejimoshidemo/src/main/java/com/woniu/design/Factory.java b/shejimoshidemo/src/main/java/com/woniu/design/Factory.java new file mode 100644 index 0000000..a00b2ff --- /dev/null +++ b/shejimoshidemo/src/main/java/com/woniu/design/Factory.java @@ -0,0 +1,27 @@ +//package com.woniu.design; +// +//import org.springframework.util.StringUtils; +// +//import java.util.HashMap; +//import java.util.Map; +// +///** +// * 工厂设计模式 +// */ +//public class Factory { +// private static Map strategyMap = new HashMap<>(); +// +// public static AbstractHandler getInvokeStrategy(String str){ +// return strategyMap.get(str); +// } +// +// public static void register(String str ,AbstractHandler handler){ +// if (StringUtils.isEmpty(str)|| null==handler) { +// return; +// } +// strategyMap.put(str,handler); +// } +// +// +// +//} diff --git a/shejimoshidemo/src/main/java/com/woniu/design/Factory2.java b/shejimoshidemo/src/main/java/com/woniu/design/Factory2.java new file mode 100644 index 0000000..6b7afb0 --- /dev/null +++ b/shejimoshidemo/src/main/java/com/woniu/design/Factory2.java @@ -0,0 +1,27 @@ +package com.woniu.design; + +import org.springframework.util.StringUtils; + +import java.util.HashMap; +import java.util.Map; + +/** + * 工厂设计模式 + */ +public class Factory2 { + private static Map strategyMap = new HashMap<>(); + + public static AbstractHandler getInvokeStrategy(String str){ + return strategyMap.get(str); + } + + public static void register(String str ,AbstractHandler handler){ + if (StringUtils.isEmpty(str)|| null==handler) { + return; + } + strategyMap.put(str,handler); + } + + + +} diff --git a/shejimoshidemo/src/main/java/com/woniu/design/Handler.java b/shejimoshidemo/src/main/java/com/woniu/design/Handler.java new file mode 100644 index 0000000..fdb1d99 --- /dev/null +++ b/shejimoshidemo/src/main/java/com/woniu/design/Handler.java @@ -0,0 +1,12 @@ +package com.woniu.design; + +import org.springframework.beans.factory.InitializingBean; + +/** + * 策略设计模式 + */ +public interface Handler extends InitializingBean { + void AA(String nikeName); + + +} diff --git a/shejimoshidemo/src/main/java/com/woniu/design/PiQiuHandler.java b/shejimoshidemo/src/main/java/com/woniu/design/PiQiuHandler.java new file mode 100644 index 0000000..08d575f --- /dev/null +++ b/shejimoshidemo/src/main/java/com/woniu/design/PiQiuHandler.java @@ -0,0 +1,17 @@ +package com.woniu.design; + +import org.springframework.stereotype.Component; + +@Component +public class PiQiuHandler extends AbstractHandler{ + @Override + public void AA(String nikeName) { + //业务逻辑 + System.out.println("我是皮球"); + } + + @Override + public void afterPropertiesSet() throws Exception { + Factory2.register("皮球",this); + } +} diff --git a/shejimoshidemo/src/main/java/com/woniu/design/WoNiuHandler.java b/shejimoshidemo/src/main/java/com/woniu/design/WoNiuHandler.java new file mode 100644 index 0000000..971d417 --- /dev/null +++ b/shejimoshidemo/src/main/java/com/woniu/design/WoNiuHandler.java @@ -0,0 +1,18 @@ +package com.woniu.design; + +import org.springframework.stereotype.Component; + +@Component +public class WoNiuHandler extends AbstractHandler{ + + @Override + public void AA(String nikeName) { + //业务逻辑 + System.out.println("我是蜗牛"); + } + + @Override + public void afterPropertiesSet() throws Exception { + Factory2.register("蜗牛",this); + } +}