From 32e24a60a5e014064f79193c171dfb580a3d6dfb Mon Sep 17 00:00:00 2001 From: niushuai233 Date: Fri, 5 Jan 2024 18:01:05 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20:100:=20=E6=A1=A5=E6=8E=A5=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../constructmodel/bridge/pay/AliPay.java | 48 +++++++++++++++ .../constructmodel/bridge/pay/PayWay.java | 39 ++++++++++++ .../constructmodel/bridge/pay/WxPay.java | 48 +++++++++++++++ .../bridge/security/FaceSecurity.java | 35 +++++++++++ .../bridge/security/FingerprintSecurity.java | 37 +++++++++++ .../bridge/security/PasswordSecurity.java | 41 +++++++++++++ .../bridge/security/Security.java | 29 +++++++++ .../constructmodel/adapter/PayTest.java | 61 +++++++++++++++++++ 8 files changed, 338 insertions(+) create mode 100644 src/main/java/cc/niushuai/demo/designpattern/constructmodel/bridge/pay/AliPay.java create mode 100644 src/main/java/cc/niushuai/demo/designpattern/constructmodel/bridge/pay/PayWay.java create mode 100644 src/main/java/cc/niushuai/demo/designpattern/constructmodel/bridge/pay/WxPay.java create mode 100644 src/main/java/cc/niushuai/demo/designpattern/constructmodel/bridge/security/FaceSecurity.java create mode 100644 src/main/java/cc/niushuai/demo/designpattern/constructmodel/bridge/security/FingerprintSecurity.java create mode 100644 src/main/java/cc/niushuai/demo/designpattern/constructmodel/bridge/security/PasswordSecurity.java create mode 100644 src/main/java/cc/niushuai/demo/designpattern/constructmodel/bridge/security/Security.java create mode 100644 src/test/java/cc/niushuai/demo/designpattern/constructmodel/adapter/PayTest.java diff --git a/src/main/java/cc/niushuai/demo/designpattern/constructmodel/bridge/pay/AliPay.java b/src/main/java/cc/niushuai/demo/designpattern/constructmodel/bridge/pay/AliPay.java new file mode 100644 index 0000000..2802c62 --- /dev/null +++ b/src/main/java/cc/niushuai/demo/designpattern/constructmodel/bridge/pay/AliPay.java @@ -0,0 +1,48 @@ +/* + * 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.constructmodel.bridge.pay; + +import cc.niushuai.demo.designpattern.constructmodel.bridge.security.Security; +import lombok.extern.slf4j.Slf4j; + +import java.math.BigDecimal; + +/** + * ali pay + * + * @author niushuai233 + * @date 2024/1/5 17:13 + * @since 0.0.1 + */ +@Slf4j +public class AliPay extends PayWay { + + public AliPay(Security security) { + super(security); + } + + @Override + public Integer transfer(String fromUid, String toUid, BigDecimal amount) { + log.info("支付宝转账: 从账户{}转账到{}, 金额: {}", fromUid, toUid, amount); + if (security.verify(fromUid)) { + log.info("支付宝转账: 账户[{}]安全校验通过, 转账金额: {}, 转账成功", fromUid, amount); + return 0; + } + log.warn("支付宝转账: 账户[{}]安全校验失败", fromUid); + return -1; + } +} diff --git a/src/main/java/cc/niushuai/demo/designpattern/constructmodel/bridge/pay/PayWay.java b/src/main/java/cc/niushuai/demo/designpattern/constructmodel/bridge/pay/PayWay.java new file mode 100644 index 0000000..50e0bd9 --- /dev/null +++ b/src/main/java/cc/niushuai/demo/designpattern/constructmodel/bridge/pay/PayWay.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.constructmodel.bridge.pay; + +import cc.niushuai.demo.designpattern.constructmodel.bridge.security.Security; + +import java.math.BigDecimal; + +/** + * pay interface + * + * @author niushuai233 + * @date 2024/1/5 17:11 + * @since 0.0.1 + */ +public abstract class PayWay { + + protected Security security; + + public PayWay(Security security) { + this.security = security; + } + + public abstract Integer transfer(String fromUid, String toUid, BigDecimal amount); +} diff --git a/src/main/java/cc/niushuai/demo/designpattern/constructmodel/bridge/pay/WxPay.java b/src/main/java/cc/niushuai/demo/designpattern/constructmodel/bridge/pay/WxPay.java new file mode 100644 index 0000000..222e01e --- /dev/null +++ b/src/main/java/cc/niushuai/demo/designpattern/constructmodel/bridge/pay/WxPay.java @@ -0,0 +1,48 @@ +/* + * 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.constructmodel.bridge.pay; + +import cc.niushuai.demo.designpattern.constructmodel.bridge.security.Security; +import lombok.extern.slf4j.Slf4j; + +import java.math.BigDecimal; + +/** + * WeChat pay + * + * @author niushuai233 + * @date 2024/1/5 17:12 + * @since 0.0.1 + */ +@Slf4j +public class WxPay extends PayWay { + + public WxPay(Security security) { + super(security); + } + + @Override + public Integer transfer(String fromUid, String toUid, BigDecimal amount) { + log.info("微信转账: 从账户{}转账到{}, 金额: {}", fromUid, toUid, amount); + if (security.verify(fromUid)) { + log.info("微信转账: 账户[{}]安全校验通过, 转账金额: {}, 转账成功", fromUid, amount); + return 0; + } + log.warn("微信转账: 账户[{}]安全校验失败", fromUid); + return -1; + } +} diff --git a/src/main/java/cc/niushuai/demo/designpattern/constructmodel/bridge/security/FaceSecurity.java b/src/main/java/cc/niushuai/demo/designpattern/constructmodel/bridge/security/FaceSecurity.java new file mode 100644 index 0000000..2688a22 --- /dev/null +++ b/src/main/java/cc/niushuai/demo/designpattern/constructmodel/bridge/security/FaceSecurity.java @@ -0,0 +1,35 @@ +/* + * 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.constructmodel.bridge.security; + +import lombok.extern.slf4j.Slf4j; + +/** + * 人脸识别 + * + * @author niushuai233 + * @date 2024/1/5 17:15 + * @since 0.0.1 + */ +@Slf4j +public class FaceSecurity implements Security { + @Override + public boolean verify(String uid) { + log.info("面部识别...{} ", uid); + return true; + } +} diff --git a/src/main/java/cc/niushuai/demo/designpattern/constructmodel/bridge/security/FingerprintSecurity.java b/src/main/java/cc/niushuai/demo/designpattern/constructmodel/bridge/security/FingerprintSecurity.java new file mode 100644 index 0000000..2665a66 --- /dev/null +++ b/src/main/java/cc/niushuai/demo/designpattern/constructmodel/bridge/security/FingerprintSecurity.java @@ -0,0 +1,37 @@ +/* + * 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.constructmodel.bridge.security; + +import lombok.extern.slf4j.Slf4j; + +/** + * 指纹 + * + * @author niushuai233 + * @date 2024/1/5 17:14 + * @since 0.0.1 + */ +@Slf4j +public class FingerprintSecurity implements Security { + + + @Override + public boolean verify(String uid) { + log.info("指纹识别...{} ", uid); + return true; + } +} diff --git a/src/main/java/cc/niushuai/demo/designpattern/constructmodel/bridge/security/PasswordSecurity.java b/src/main/java/cc/niushuai/demo/designpattern/constructmodel/bridge/security/PasswordSecurity.java new file mode 100644 index 0000000..e3f208e --- /dev/null +++ b/src/main/java/cc/niushuai/demo/designpattern/constructmodel/bridge/security/PasswordSecurity.java @@ -0,0 +1,41 @@ +/* + * 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.constructmodel.bridge.security; + +import lombok.extern.slf4j.Slf4j; + +/** + * 密码 + * + * @author niushuai233 + * @date 2024/1/5 17:14 + * @since 0.0.1 + */ +@Slf4j +public class PasswordSecurity implements Security { + @Override + public boolean verify(String uid) { + log.info("密码识别...{} ", uid); + long timeMillis = System.currentTimeMillis(); + if ((timeMillis % 3 == 0)) { + log.info("密码[{}]正确", timeMillis); + return true; + } + log.info("密码错误[{}]", timeMillis); + return false; + } +} diff --git a/src/main/java/cc/niushuai/demo/designpattern/constructmodel/bridge/security/Security.java b/src/main/java/cc/niushuai/demo/designpattern/constructmodel/bridge/security/Security.java new file mode 100644 index 0000000..3a95773 --- /dev/null +++ b/src/main/java/cc/niushuai/demo/designpattern/constructmodel/bridge/security/Security.java @@ -0,0 +1,29 @@ +/* + * 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.constructmodel.bridge.security; + +/** + * 安全校验 + * + * @author niushuai233 + * @date 2024/1/5 17:11 + * @since 0.0.1 + */ +public interface Security { + + boolean verify(String uid); +} diff --git a/src/test/java/cc/niushuai/demo/designpattern/constructmodel/adapter/PayTest.java b/src/test/java/cc/niushuai/demo/designpattern/constructmodel/adapter/PayTest.java new file mode 100644 index 0000000..c84f7f1 --- /dev/null +++ b/src/test/java/cc/niushuai/demo/designpattern/constructmodel/adapter/PayTest.java @@ -0,0 +1,61 @@ +/* + * 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.constructmodel.adapter; + +import cc.niushuai.demo.designpattern.constructmodel.bridge.pay.AliPay; +import cc.niushuai.demo.designpattern.constructmodel.bridge.pay.PayWay; +import cc.niushuai.demo.designpattern.constructmodel.bridge.pay.WxPay; +import cc.niushuai.demo.designpattern.constructmodel.bridge.security.FaceSecurity; +import cc.niushuai.demo.designpattern.constructmodel.bridge.security.FingerprintSecurity; +import cc.niushuai.demo.designpattern.constructmodel.bridge.security.PasswordSecurity; +import org.junit.jupiter.api.Test; + +import java.math.BigDecimal; + +/** + * pay test + * + * @author niushuai233 + * @date 2024/1/5 17:57 + * @since 0.0.1 + */ +public class PayTest { + + @Test + public void testPay() { + + PayWay pay_a = new AliPay(new PasswordSecurity()); + pay_a.transfer("001", "002", BigDecimal.valueOf(12)); + + PayWay pay_b = new AliPay(new FaceSecurity()); + pay_b.transfer("001", "002", BigDecimal.valueOf(22)); + + PayWay pay_c = new AliPay(new FingerprintSecurity()); + pay_c.transfer("001", "002", BigDecimal.valueOf(32)); + + + PayWay pay_x = new WxPay(new PasswordSecurity()); + pay_x.transfer("001", "002", BigDecimal.valueOf(11)); + + PayWay pay_y = new WxPay(new FaceSecurity()); + pay_y.transfer("001", "002", BigDecimal.valueOf(21)); + + PayWay pay_z = new WxPay(new FingerprintSecurity()); + pay_z.transfer("001", "002", BigDecimal.valueOf(31)); + + } +}