Browse Source

责任链模式

pull/1/head
马府强 2 years ago
parent
commit
c0551db167
  1. 8
      zerenliandemo/.idea/.gitignore
  2. 14
      zerenliandemo/.idea/compiler.xml
  3. 20
      zerenliandemo/.idea/jarRepositories.xml
  4. 14
      zerenliandemo/.idea/misc.xml
  5. 41
      zerenliandemo/pom.xml
  6. 15
      zerenliandemo/src/main/java/com/woniu/zerenliandemo/AuthHandler.java
  7. 10
      zerenliandemo/src/main/java/com/woniu/zerenliandemo/BusinessLogicHandler.java
  8. 31
      zerenliandemo/src/main/java/com/woniu/zerenliandemo/Handler.java
  9. 16
      zerenliandemo/src/main/java/com/woniu/zerenliandemo/LoginHandler.java
  10. 28
      zerenliandemo/src/main/java/com/woniu/zerenliandemo/Test.java
  11. 14
      zerenliandemo/src/main/java/com/woniu/zerenliandemo/User.java
  12. 17
      zerenliandemo/src/main/java/com/woniu/zerenliandemo/ValidateHandler.java
  13. BIN
      zerenliandemo/target/classes/com/woniu/zerenliandemo/AuthHandler.class
  14. BIN
      zerenliandemo/target/classes/com/woniu/zerenliandemo/BusinessLogicHandler.class
  15. BIN
      zerenliandemo/target/classes/com/woniu/zerenliandemo/Handler$Builder.class
  16. BIN
      zerenliandemo/target/classes/com/woniu/zerenliandemo/Handler.class
  17. BIN
      zerenliandemo/target/classes/com/woniu/zerenliandemo/LoginHandler.class
  18. BIN
      zerenliandemo/target/classes/com/woniu/zerenliandemo/Test.class
  19. BIN
      zerenliandemo/target/classes/com/woniu/zerenliandemo/User.class
  20. BIN
      zerenliandemo/target/classes/com/woniu/zerenliandemo/ValidateHandler.class
  21. 2
      zerenliandemo/zerenliandemo.iml

8
zerenliandemo/.idea/.gitignore

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

14
zerenliandemo/.idea/compiler.xml

@ -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="zerenliandemo" />
</profile>
</annotationProcessing>
</component>
</project>

20
zerenliandemo/.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
zerenliandemo/.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>

41
zerenliandemo/pom.xml

@ -0,0 +1,41 @@
<?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>zerenliandemo</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>

15
zerenliandemo/src/main/java/com/woniu/zerenliandemo/AuthHandler.java

@ -0,0 +1,15 @@
package com.woniu.zerenliandemo;
public class AuthHandler extends Handler {
@Override
public void doHandler(User user) {
if (!"管理员".equals(user.getRoleName())) {
System.out.println("您不是管理员,没有操作权限");
return;
}
if (null != next) {
next.doHandler(user);
}
}
}

10
zerenliandemo/src/main/java/com/woniu/zerenliandemo/BusinessLogicHandler.java

@ -0,0 +1,10 @@
package com.woniu.zerenliandemo;
public class BusinessLogicHandler extends Handler {
@Override
public void doHandler(User user) {
System.out.println("蜗牛好棒!");
}
}

31
zerenliandemo/src/main/java/com/woniu/zerenliandemo/Handler.java

@ -0,0 +1,31 @@
package com.woniu.zerenliandemo;
public abstract class Handler<T> {
protected Handler next;
private void next(Handler next) {
this.next = next;
}
public abstract void doHandler(User user);
public static class Builder<T> {
private Handler<T> head;
private Handler<T> tail;
public Builder<T> addHandler(Handler handler) {
if (this.head == null) {
this.head = this.tail = handler;
return this;
}
this.tail.next(handler);
this.tail = handler;
return this;
}
public Handler<T> build() {
return this.head;
}
}
}

16
zerenliandemo/src/main/java/com/woniu/zerenliandemo/LoginHandler.java

@ -0,0 +1,16 @@
package com.woniu.zerenliandemo;
public class LoginHandler extends Handler {
@Override
public void doHandler(User user) {
if (!"woniu".equals(user.getUserName()) || !"666".equals(user.getPassWord())) {
System.out.println("用户名密码不正确");
return;
}
if (null != next) {
next.doHandler(user);
}
}
}

28
zerenliandemo/src/main/java/com/woniu/zerenliandemo/Test.java

@ -0,0 +1,28 @@
package com.woniu.zerenliandemo;
/**
* 责任链模式来优化 参数多重校验非常优雅
*
* 之前在做需求写一个方法先在前面做验证
* if不满足A条件则return
* if不满足B条件则return...
* 一共写了5个验证等验证通过以后才执行下面的逻辑
* 过了一阵产品提了需求跟这个方法类似
* 我又把这个方法copy了一份只不过验证条件稍微有点不一样变成6个验证了
*/
public class Test {
public static void main(String[] args) {
Handler.Builder builder = new Handler.Builder();
//链式编程,谁在前谁在后看的清清楚楚
builder.addHandler(new ValidateHandler())
.addHandler(new LoginHandler())
.addHandler(new AuthHandler())
.addHandler(new BusinessLogicHandler());
User user = new User();
user.setUserName("woniu");
user.setPassWord("666");
builder.build().doHandler(user);
}
}

14
zerenliandemo/src/main/java/com/woniu/zerenliandemo/User.java

@ -0,0 +1,14 @@
package com.woniu.zerenliandemo;
import lombok.Data;
@Data
public class User {
private String userName;
private String passWord;
private String roleName;
}

17
zerenliandemo/src/main/java/com/woniu/zerenliandemo/ValidateHandler.java

@ -0,0 +1,17 @@
package com.woniu.zerenliandemo;
import org.apache.commons.lang.StringUtils;
public class ValidateHandler extends Handler {
@Override
public void doHandler(User user) {
if (StringUtils.isEmpty(user.getUserName()) ||
StringUtils.isEmpty(user.getPassWord())) {
System.out.println("用户名和密码不能为空");
return;
}
if (null != next) {
next.doHandler(user);
}
}
}

BIN
zerenliandemo/target/classes/com/woniu/zerenliandemo/AuthHandler.class

Binary file not shown.

BIN
zerenliandemo/target/classes/com/woniu/zerenliandemo/BusinessLogicHandler.class

Binary file not shown.

BIN
zerenliandemo/target/classes/com/woniu/zerenliandemo/Handler$Builder.class

Binary file not shown.

BIN
zerenliandemo/target/classes/com/woniu/zerenliandemo/Handler.class

Binary file not shown.

BIN
zerenliandemo/target/classes/com/woniu/zerenliandemo/LoginHandler.class

Binary file not shown.

BIN
zerenliandemo/target/classes/com/woniu/zerenliandemo/Test.class

Binary file not shown.

BIN
zerenliandemo/target/classes/com/woniu/zerenliandemo/User.class

Binary file not shown.

BIN
zerenliandemo/target/classes/com/woniu/zerenliandemo/ValidateHandler.class

Binary file not shown.

2
zerenliandemo/zerenliandemo.iml

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