diff --git a/src/main/java/cc/niushuai/demo/designpattern/creatormodel/decorator/LoginService.java b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/decorator/LoginService.java new file mode 100644 index 0000000..41c9d8a --- /dev/null +++ b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/decorator/LoginService.java @@ -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!"); + } +} diff --git a/src/main/java/cc/niushuai/demo/designpattern/creatormodel/decorator/entity/LoginUser.java b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/decorator/entity/LoginUser.java new file mode 100644 index 0000000..31022ba --- /dev/null +++ b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/decorator/entity/LoginUser.java @@ -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; + } +} diff --git a/src/main/java/cc/niushuai/demo/designpattern/creatormodel/decorator/filter/LoginFilter.java b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/decorator/filter/LoginFilter.java new file mode 100644 index 0000000..f28c7dd --- /dev/null +++ b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/decorator/filter/LoginFilter.java @@ -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; + } +} diff --git a/src/main/java/cc/niushuai/demo/designpattern/creatormodel/decorator/filter/LoginFilterDecorator.java b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/decorator/filter/LoginFilterDecorator.java new file mode 100644 index 0000000..87386b9 --- /dev/null +++ b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/decorator/filter/LoginFilterDecorator.java @@ -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); + } +} diff --git a/src/main/java/cc/niushuai/demo/designpattern/creatormodel/decorator/filter/LoginFilterDecorator2.java b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/decorator/filter/LoginFilterDecorator2.java new file mode 100644 index 0000000..35bdce5 --- /dev/null +++ b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/decorator/filter/LoginFilterDecorator2.java @@ -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); + } +} diff --git a/src/test/java/cc/niushuai/demo/designpattern/creatormodel/decorator/_01/LoginTest.java b/src/test/java/cc/niushuai/demo/designpattern/creatormodel/decorator/_01/LoginTest.java new file mode 100644 index 0000000..29d3cfe --- /dev/null +++ b/src/test/java/cc/niushuai/demo/designpattern/creatormodel/decorator/_01/LoginTest.java @@ -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"); + } +}