Browse Source

feat: 💯 桥接模式

master
niushuai233 1 year ago
parent
commit
32e24a60a5
  1. 48
      src/main/java/cc/niushuai/demo/designpattern/constructmodel/bridge/pay/AliPay.java
  2. 39
      src/main/java/cc/niushuai/demo/designpattern/constructmodel/bridge/pay/PayWay.java
  3. 48
      src/main/java/cc/niushuai/demo/designpattern/constructmodel/bridge/pay/WxPay.java
  4. 35
      src/main/java/cc/niushuai/demo/designpattern/constructmodel/bridge/security/FaceSecurity.java
  5. 37
      src/main/java/cc/niushuai/demo/designpattern/constructmodel/bridge/security/FingerprintSecurity.java
  6. 41
      src/main/java/cc/niushuai/demo/designpattern/constructmodel/bridge/security/PasswordSecurity.java
  7. 29
      src/main/java/cc/niushuai/demo/designpattern/constructmodel/bridge/security/Security.java
  8. 61
      src/test/java/cc/niushuai/demo/designpattern/constructmodel/adapter/PayTest.java

48
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;
}
}

39
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);
}

48
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;
}
}

35
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;
}
}

37
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;
}
}

41
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;
}
}

29
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);
}

61
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));
}
}
Loading…
Cancel
Save