Browse Source

feat: 👍 装饰器模式

master
niushuai233 1 year ago
parent
commit
8acb639113
  1. 47
      src/main/java/cc/niushuai/demo/designpattern/creatormodel/decorator/LoginService.java
  2. 50
      src/main/java/cc/niushuai/demo/designpattern/creatormodel/decorator/entity/LoginUser.java
  3. 39
      src/main/java/cc/niushuai/demo/designpattern/creatormodel/decorator/filter/LoginFilter.java
  4. 40
      src/main/java/cc/niushuai/demo/designpattern/creatormodel/decorator/filter/LoginFilterDecorator.java
  5. 45
      src/main/java/cc/niushuai/demo/designpattern/creatormodel/decorator/filter/LoginFilterDecorator2.java
  6. 46
      src/test/java/cc/niushuai/demo/designpattern/creatormodel/decorator/_01/LoginTest.java

47
src/main/java/cc/niushuai/demo/designpattern/creatormodel/decorator/LoginService.java

@ -0,0 +1,47 @@ @@ -0,0 +1,47 @@
/*
* Copyright (C) 2023 niushuai233 niushuai.cc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cc.niushuai.demo.designpattern.creatormodel.decorator;
import cc.niushuai.demo.designpattern.creatormodel.decorator.entity.LoginUser;
import cc.niushuai.demo.designpattern.creatormodel.decorator.filter.LoginFilter;
import cc.niushuai.demo.designpattern.creatormodel.decorator.filter.LoginFilterDecorator;
/**
* 登陆校验逻辑
*
* @author niushuai233
* @date 2024/1/29 10:40
* @since 0.0.1
*/
public class LoginService {
public void login(String username, String password) {
login(username, password, null);
}
public void login(String username, String password, String code) {
LoginUser loginUser = new LoginUser(username, password, code);
LoginFilterDecorator filter = new LoginFilterDecorator();
if (filter.filter(loginUser)) {
System.out.println(username + " login success!");
return;
}
System.err.println(username + " login failed!");
}
}

50
src/main/java/cc/niushuai/demo/designpattern/creatormodel/decorator/entity/LoginUser.java

@ -0,0 +1,50 @@ @@ -0,0 +1,50 @@
/*
* Copyright (C) 2023 niushuai233 niushuai.cc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cc.niushuai.demo.designpattern.creatormodel.decorator.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* 登陆用户
*
* @author niushuai233
* @date 2024/1/10 16:56
* @since 0.0.1
*/
@Data
public class LoginUser {
private String username;
private String password;
private String code;
public LoginUser() {
}
public LoginUser(String username, String password) {
this.username = username;
this.password = password;
}
public LoginUser(String username, String password, String code) {
this.username = username;
this.password = password;
this.code = code;
}
}

39
src/main/java/cc/niushuai/demo/designpattern/creatormodel/decorator/filter/LoginFilter.java

@ -0,0 +1,39 @@ @@ -0,0 +1,39 @@
/*
* Copyright (C) 2023 niushuai233 niushuai.cc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cc.niushuai.demo.designpattern.creatormodel.decorator.filter;
import cc.niushuai.demo.designpattern.creatormodel.decorator.entity.LoginUser;
/**
* 登陆过滤器
*
* @author niushuai233
* @date 2024/1/29 10:40
* @since 0.0.1
*/
public class LoginFilter {
public boolean filter(LoginUser user) {
if ("zh".equals(user.getUsername()) && "pass".equals(user.getPassword())) {
return true;
}
System.err.println("==> username or password is wrong");
return false;
}
}

40
src/main/java/cc/niushuai/demo/designpattern/creatormodel/decorator/filter/LoginFilterDecorator.java

@ -0,0 +1,40 @@ @@ -0,0 +1,40 @@
/*
* Copyright (C) 2023 niushuai233 niushuai.cc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cc.niushuai.demo.designpattern.creatormodel.decorator.filter;
import cc.niushuai.demo.designpattern.creatormodel.decorator.entity.LoginUser;
/**
* login filter decorator
*
* @author niushuai233
* @date 2024/1/29 11:04
* @since 0.0.1
*/
public class LoginFilterDecorator extends LoginFilter {
@Override
public boolean filter(LoginUser user) {
if (!"9527".equals(user.getCode())) {
System.err.println("code is wrong!");
return false;
}
return super.filter(user);
}
}

45
src/main/java/cc/niushuai/demo/designpattern/creatormodel/decorator/filter/LoginFilterDecorator2.java

@ -0,0 +1,45 @@ @@ -0,0 +1,45 @@
/*
* Copyright (C) 2023 niushuai233 niushuai.cc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cc.niushuai.demo.designpattern.creatormodel.decorator.filter;
import cc.niushuai.demo.designpattern.creatormodel.decorator.entity.LoginUser;
/**
* 构造装饰
*
* @author niushuai233
* @date 2024/1/29 11:09
* @since 0.0.1
*/
public class LoginFilterDecorator2 {
private LoginFilter loginFilter;
public LoginFilterDecorator2(LoginFilter loginFilter) {
this.loginFilter = loginFilter;
}
public boolean filter(LoginUser user) {
if (!"9527".equals(user.getCode())) {
System.err.println("code is wrong!");
return false;
}
return loginFilter.filter(user);
}
}

46
src/test/java/cc/niushuai/demo/designpattern/creatormodel/decorator/_01/LoginTest.java

@ -0,0 +1,46 @@ @@ -0,0 +1,46 @@
/*
* Copyright (C) 2023 niushuai233 niushuai.cc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cc.niushuai.demo.designpattern.creatormodel.decorator._01;
import cc.niushuai.demo.designpattern.creatormodel.decorator.LoginService;
import org.junit.jupiter.api.Test;
/**
* 测试
*
* @author niushuai233
* @date 2024/1/29 10:49
* @since 0.0.1
*/
public class LoginTest {
@Test
public void test_login() {
LoginService loginService = new LoginService();
loginService.login("zhangsan", "123");
loginService.login("zh", "pass");
System.out.println();
loginService.login("zhangsan", "123", "9527");
loginService.login("zh", "pass", "9527w");
System.out.println();
loginService.login("zh", "pass", "9527");
}
}
Loading…
Cancel
Save