Browse Source

shejimoshi

pull/1/head
woniu 2 years ago
parent
commit
757024e14a
  1. 8
      shejimoshidemo/.idea/.gitignore
  2. 13
      shejimoshidemo/.idea/compiler.xml
  3. 25
      shejimoshidemo/.idea/jarRepositories.xml
  4. 14
      shejimoshidemo/.idea/misc.xml
  5. 37
      shejimoshidemo/pom.xml
  6. 2
      shejimoshidemo/shejimoshidemo.iml
  7. 17
      shejimoshidemo/src/main/java/com/woniu/design/AbstractHandler.java
  8. 45
      shejimoshidemo/src/main/java/com/woniu/design/Demo.java
  9. 27
      shejimoshidemo/src/main/java/com/woniu/design/Factory.java
  10. 27
      shejimoshidemo/src/main/java/com/woniu/design/Factory2.java
  11. 12
      shejimoshidemo/src/main/java/com/woniu/design/Handler.java
  12. 17
      shejimoshidemo/src/main/java/com/woniu/design/PiQiuHandler.java
  13. 18
      shejimoshidemo/src/main/java/com/woniu/design/WoNiuHandler.java

8
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/

13
shejimoshidemo/.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="shejimoshidemo" />
</profile>
</annotationProcessing>
</component>
</project>

25
shejimoshidemo/.idea/jarRepositories.xml

@ -0,0 +1,25 @@
<?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="Central Repository" />
<option name="url" value="http://maven.aliyun.com/nexus/content/repositories/central/" />
</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
shejimoshidemo/.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>

37
shejimoshidemo/pom.xml

@ -0,0 +1,37 @@
<?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>shejimoshidemo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>5.3.25</version>
<type>pom</type>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>3.0.2</version>
</dependency>
</dependencies>
</project>

2
shejimoshidemo/shejimoshidemo.iml

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

17
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();
}
}

45
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);
}
}

27
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<String,AbstractHandler> 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);
// }
//
//
//
//}

27
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<String,AbstractHandler> 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);
}
}

12
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);
}

17
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);
}
}

18
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);
}
}
Loading…
Cancel
Save