马府强
2 years ago
21 changed files with 230 additions and 0 deletions
@ -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/ |
@ -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> |
@ -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,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> |
@ -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); |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,10 @@ |
|||
package com.woniu.zerenliandemo; |
|||
|
|||
|
|||
public class BusinessLogicHandler extends Handler { |
|||
|
|||
@Override |
|||
public void doHandler(User user) { |
|||
System.out.println("蜗牛好棒!"); |
|||
} |
|||
} |
@ -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; |
|||
} |
|||
} |
|||
} |
@ -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); |
|||
} |
|||
} |
|||
} |
@ -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); |
|||
} |
|||
|
|||
} |
@ -0,0 +1,14 @@ |
|||
package com.woniu.zerenliandemo; |
|||
|
|||
|
|||
import lombok.Data; |
|||
|
|||
@Data |
|||
public class User { |
|||
|
|||
private String userName; |
|||
private String passWord; |
|||
private String roleName; |
|||
|
|||
|
|||
} |
@ -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); |
|||
} |
|||
} |
|||
} |
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,2 @@ |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
<module type="JAVA_MODULE" version="4" /> |
Loading…
Reference in new issue