6 changed files with 267 additions and 0 deletions
@ -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!"); |
||||||
|
} |
||||||
|
} |
@ -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; |
||||||
|
} |
||||||
|
} |
@ -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; |
||||||
|
} |
||||||
|
} |
@ -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); |
||||||
|
} |
||||||
|
} |
@ -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); |
||||||
|
} |
||||||
|
} |
@ -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…
Reference in new issue