Hello everyone, I am Wolf King, a programmer who loves playing ball

This is part 8 of the Design Patterns series, which takes a look at bridging patterns


1, an overview of the

The bridging pattern is a structural design pattern that splits a large class or series of closely related classes into two separate hierarchies of abstraction and implementation that can be used separately at development time.

2. Applicable scenarios

1) If you want to split or recombine a large, complex class with multiple functions, use bridge mode. 2) Use this pattern if you want to extend a class in several independent dimensions. Object properties are implemented by other classes and do not need to do all the work themselves. 3) Use bridge mode if you need to switch implementations at run time. The bridge pattern replaces an implementation object in an abstract part as simple as assigning new values to a member variable.

3, the instance,

There are the following scenarios:

Payment method: Wechat, Alipay payment method: fingerprint, scan faceCopy the code

3.1 Do not use bridge mode

Define two enumerations

/** * Payment method */
public enum PayMethodEnum {

    FACE(0."Sweep face"),
    FINGER(1."Fingerprint");


    PayMethodEnum(int code, String name) {
        this.code = code;
        this.name = name;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getName(int code) {
        PayMethodEnum[] payWaysEnums = values();
        for (PayMethodEnum payMethodEnum : payWaysEnums) {
            if (payMethodEnum.code == code) {
                returnpayMethodEnum.name; }}return null;
    }

    public void setName(String name) {
        this.name = name;
    }

    private int code;

    private String name;

}
Copy the code
/** * Payment method */
public enum PayWaysEnum {

    ZHIFUBAO(0."Alipay"),
    WEIXIN(1."WeChat");


    PayWaysEnum(int code, String name) {
        this.code = code;
        this.name = name;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    private int code;


    public void setName(String name) {
        this.name = name;
    }

    public String getName(int code) {
        PayWaysEnum[] payWaysEnums = values();
        for (PayWaysEnum payWaysEnum : payWaysEnums) {
            if (payWaysEnum.code == code) {
                returnpayWaysEnum.name; }}return null;
    }

    private String name;

}
Copy the code

Define the payment business process:

public class Pay {

    public void pay(int payMethod, int payWay) {
        if (PayMethodEnum.FACE.getCode() == payMethod) {
            System.out.println("The current payment method is:" + PayMethodEnum.FACE.getName(payMethod));
        } else {
            System.out.println("The current payment method is:" + PayMethodEnum.FINGER.getName(payMethod));
        }
        if (PayWaysEnum.ZHIFUBAO.getCode() == payWay) {
            System.out.println("The current method of payment is:" + PayWaysEnum.ZHIFUBAO.getName(payWay));
        } else {
            System.out.println("The current method of payment is:"+ PayWaysEnum.WEIXIN.getName(payWay)); }}}Copy the code

The test class:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestApplication.class)
public class TestDemo {

    @Test
    public void test() {
        Pay pay = new Pay();
        // Alipay scan face payment
        pay.pay(PayMethodEnum.FACE.getCode(),PayWaysEnum.ZHIFUBAO.getCode());
        System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --");
        // Wechat scan face payment
        pay.pay(PayMethodEnum.FACE.getCode(),PayWaysEnum.WEIXIN.getCode());
        System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --");
        // Alipay fingerprint payment
        pay.pay(PayMethodEnum.FINGER.getCode(),PayWaysEnum.ZHIFUBAO.getCode());
        System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --");
        // Wechat fingerprint payment
        pay.pay(PayMethodEnum.FINGER.getCode(),PayWaysEnum.WEIXIN.getCode());
        System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --"); }}Copy the code

Results:

The current method of payment is: esau's face Current payment way is: pay treasure -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the current payment are: esau's face The current payment way is: WeChat -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the current payment are: fingerprint current payment way is: pay treasure -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the current payment are: Current payment way is: fingerprint WeChat -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --Copy the code

3.2 Using bridge Mode

Define enumeration:

/** * Payment method */
public enum PayMethodEnum {

    FACE(0."Sweep face"),
    FINGER(1."Fingerprint");


    PayMethodEnum(int code, String name) {
        this.code = code;
        this.name = name;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    private int code;

    private String name;

}
Copy the code
/** * Payment method */
public enum PayWaysEnum {

    ZHIFUBAO(0."Alipay"),
    WEIXIN(1."WeChat");


    PayWaysEnum(int code, String name) {
        this.code = code;
        this.name = name;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    private int code;


    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    private String name;

}
Copy the code

Define two top-level abstract interfaces:

/** * Payment mode interface */
public interface IPayMethod {

    void pay();
}
Copy the code
/** * Payment route interface */
public interface IPayWay {

    void pay();
}
Copy the code

Define two payment methods:

/** * fingerprint */
public class FingerPay implements IPayMethod {

    @Override
    public void pay() {
        System.out.println("The current payment method is:"+ PayMethodEnum.FINGER.name()); }}Copy the code
/** * scan face */
public class FacePay implements IPayMethod {

    @Override
    public void pay() {
        System.out.println("The current payment method is:"+ PayMethodEnum.FACE.name()); }}Copy the code

Define two ways of payment:

/** * wechat */
public class WXPayWay implements IPayWay {

    private IPayMethod payMethod;

    public WXPayWay(IPayMethod payMethod) {
        this.payMethod = payMethod;
    }

    @Override
    public void pay() {
        System.out.println("The current payment method is:"+ PayWaysEnum.WEIXIN.getName()); payMethod.pay(); }}Copy the code
/** ** alipay */
public class ZFBPayWay implements IPayWay {

    private IPayMethod payMethod;

    public ZFBPayWay(IPayMethod payMethod) {
        this.payMethod = payMethod;
    }

    @Override
    public void pay() {
        System.out.println("The current payment method is:"+ PayWaysEnum.ZHIFUBAO.getName()); payMethod.pay(); }}Copy the code

The test class:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestApplication.class)
public class TestDemo {

    @Test
    public void test() {
        FacePay facePay = new FacePay();
        FingerPay fingerPay = new FingerPay();
        ZFBPayWay zfbPayFace = new ZFBPayWay(facePay);
        WXPayWay wxPayWayFace = new WXPayWay(facePay);
        ZFBPayWay zfbPayFinger = new ZFBPayWay(fingerPay);
        WXPayWay wxPayWayFinger = new WXPayWay(fingerPay);
        // Alipay scan face payment
        zfbPayFace.pay();
        System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --");
        // Wechat scan face payment
        wxPayWayFace.pay();
        System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --");
        // Alipay fingerprint payment
        zfbPayFinger.pay();
        System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --");
        // Wechat fingerprint payment
        wxPayWayFinger.pay();
        System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --"); }}Copy the code

Results:

Current payment are: pay treasure to current payment are: FACE -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the current payment are: WeChat current payment are: FACE -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the current payment are: pay treasure to current payment are: FINGER -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the current payment are: WeChat current payment are: FINGER -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --Copy the code

4, analysis,

The above two methods realize the four payment processes, mainly through payment channels (Alipay and wechat), including two payment methods (face scanning and fingerprint).

Analysis from code volume:

Do not use: the code is relatively small, but in fact the main business logic to uninstall the Pay class, through the if, else judgment for business logic judgment.

Usage: The code volume has been greatly increased, with many classes added, but pay’s business logic is executed only in its own class, conforming to a single responsibility.

From the scalable level, if the cloud flash payment method is added:

Disable: The Pay class needs to be modified to add logical judgment for flash payment, which does not comply with the open/close principle.

Use: only need to add a flash cloud payment way, do not need to modify other business logic, in line with the open and closed principle.

Code coupling level:

Not used: Code business logic coupled together.

Used: Very low code coupling.

5, summary

Advantages:

1) The client only interacts with high-level abstractions, not internal objects. 2) Comply with the single principle. 3) In accordance with the open and close principle.

Disadvantages:

In the case of many business objects, which can abstract a lot of business logic, the number of classes will be greatly increased, resulting in code complexity.

Willing to output dry Java technology public number: Wolf king programming. The public number has a large number of technical articles, massive video resources, beautiful brain map, might as well pay attention to it! Get lots of learning resources and free books!

Forwarding moments is the biggest support for me!

\

Click “like and watching” if you feel something! Thank you for your support!