directory

  • The introduction
  • Applicable scenario
  • The code for

It takes about 3 minutes to read this article

The introduction

Bridges are structural design patterns

Used to decouple abstraction from implementation. Used to separate the abstract from the concrete implementation so that they can change independently.

Bridge pattern We prefer to combine two classes rather than inherit them

The advantage of bridging mode is that it is easy to separate abstraction and implementation; It also provides excellent scalability, and its implementation details are transparent to the customer.

The disadvantage is that the introduction of bridge pattern will increase the difficulty of system understanding and design, because the aggregation association is based on the abstraction layer, developers are required to design and program for the abstraction. (Food is the original sin)

Applicable scenario

Bridge model applicable scenario is usually a system need to build the abstract character and specific character between more flexibility, a class has two independent dimensions change, and these two dimensions need to be extended, avoid establish static inheritance of contact between the two levels, through bridge mode can make them in abstraction layer to build a relationship.

The bridge pattern is especially useful for systems that do not want to use inheritance or where multi-level inheritance causes the number of system classes to increase dramatically.

The code for

Suppose we are a provider of aggregation services

External support wechat pay, Alipay pay, payment methods support password, brush face

And I’m going to ask John to do it and I’m going to write it like this

if(pay ="Alipay") {
  if (payType = "Pay with your face") {
      // Swipe the face to pay the check
  }else (payType = "Pay by Password") {
     // Password payment verification}}else {
  pay = "Alipay") {
  if (payType = "Pay with your face") {
      // Swipe the face to pay the check
  } else (payType = "Pay by Password") {
     // Password payment verification}}}Copy the code

Lee decided not to, and refactored the code

Let’s write an interface IPayType that supports the verification (password, face) and payment actions for the current payment type

public interface IPayType {

    void payTypeCheck(a);

    void pay(BigDecimal amount);
}
Copy the code

Face and password payment to implement the IPayType interface, interface implementation

public class PayCypher implements IPayType{

    @Override
    public void payTypeCheck(a) {
        System.out.println("Passcode payment verified");
    }

    @Override
    public void pay(BigDecimal amount) {
        System.out.println("Payment"+ amount); }}Copy the code
public class PayFace implements IPayType{

    @Override
    public void payTypeCheck(a) {
        System.out.println("Face pay check passed.");
    }

    @Override
    public void pay(BigDecimal amount) {
        System.out.println("Payment"+ amount); }}Copy the code

Next, develop our wechat Pay and Alipay pay categories

public class AiLiPay extends Pay{

    public AiLiPay(IPayType payType) {
        super(payType);
    }

    @Override
    IPayType pay(BigDecimal amount) {
        System.out.println("Alipay payment...");
        payType.payTypeCheck();
        payType.pay(amount);
        return null; }}Copy the code
public class WeChatPay extends Pay{

    public WeChatPay(IPayType payType) {
        super(payType);
    }

    @Override
    IPayType pay(BigDecimal amount) {
        System.out.println("Wechat Pay...");
        payType.payTypeCheck();
        payType.pay(amount);
        return null; }}Copy the code

The test start

public class Test {
    public static void main(String[] args) {
        WeChatPay weChatPay = new WeChatPay(new PayCypher());
        weChatPay.pay(new BigDecimal("20"));

        AiLiPay aiLiPay = new AiLiPay(new PayFace());
        aiLiPay.pay(new BigDecimal("201")); }}/ / outputWechat Pay... Password payment verification through payment20Alipay payment in... Face payment check through payment201
Copy the code

Of course, using the bridge mode, we can also arbitrary combination of wechat + face, Alipay + password, for the subsequent code to provide a good extension

This is the bridge mode. Are you tired of it?