Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is a bridging pattern in a series of design patterns

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "what is the bridging pattern of the design pattern series". In the daily operation, I believe that many people have doubts about what is the bridging pattern of the design pattern series. The editor consulted all kinds of materials and sorted out the simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "what is the bridging pattern of the design pattern series"! Next, please follow the editor to study!

1. Overview

Bridge pattern is a kind of structural design pattern, which can divide a large class or a series of closely related classes into two separate hierarchies: abstract and implementation, so that they can be used separately in development.

2. Applicable scenarios

1) if you want to split or reassemble a complex class with multiple functions, you can use bridging mode. 2) if you want to extend a class in several independent dimensions, you can use this pattern. There are other classes to implement the properties of the object, so you don't have to do all the work yourself. 3) if you need to switch between different implementations at run time, you can use bridging mode. The bridge pattern can replace the implementation object in the abstract part, and the concrete operation is as simple as assigning a new value to the member variable.

3. Examples

There are the following scenarios:

Payment method: Wechat, Alipay payment method: fingerprint, face sweep

3.1 do not use bridging mode

Define two enumerations

/ * payment method * / public enum PayMethodEnum {FACE (0, "face scan"), 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) {return payMethodEnum.name;}} return null;} public void setName (String name) {this.name = name;} private int code Private String name;} / * 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) {return payWaysEnum.name }} return null;} private String name;}

Define the payment business process:

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

Test class:

@ RunWith (SpringRunner.class) @ SpringBootTest (classes = TestApplication.class) public class TestDemo {@ Test public void test () {Pay pay = new Pay (); / / Alipay pay pay.pay (PayMethodEnum.FACE.getCode (), PayWaysEnum.ZHIFUBAO.getCode ()) System.out.println ("- -"); / / Wechat sweeps face to pay 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 ("-");}

Results:

The current payment method is: face sweeping current payment method is: Alipay-the current payment method is: face sweeping current payment method is: Wechat-- -- current payment method is: fingerprint current payment method is: Alipay-- current payment method is: fingerprint current payment method is: Wechat-

3.2 use bridging mode

Define enumerations:

/ * payment method * / public enum PayMethodEnum {FACE (0, "face scan"), 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;} / * payment means * / 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;}

Define two top-level abstract interfaces:

/ * payment method API * / public interface IPayMethod {void pay ();} / * payment way Interface * / public interface IPayWay {void pay ();}

Define two payment methods:

/ * fingerprint * / public class FingerPay implements IPayMethod {@ Override public void pay () {System.out.println ("current payment method is:" + PayMethodEnum.FINGER.name ();}} / * sweep face * / public class FacePay implements IPayMethod {@ Override public void pay () {System.out.println ("current payment method is:" + PayMethodEnum.FACE.name () }}

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 ("current payment method is:" + PayWaysEnum.WEIXIN.getName ()); payMethod.pay () }} / * Alipay * / public class ZFBPayWay implements IPayWay {private IPayMethod payMethod; public ZFBPayWay (IPayMethod payMethod) {this.payMethod = payMethod;} @ Override public void pay () {System.out.println ("current payment method is:" + PayWaysEnum.ZHIFUBAO.getName ()); payMethod.pay ();}}

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 pays zfbPayFace.pay (); System.out.println ("- -"); / / Wechat pays wxPayWayFace.pay () System.out.println ("- -"); / / Alipay fingerprint payment zfbPayFinger.pay (); System.out.println ("- -") / / Wechat fingerprint payment wxPayWayFinger.pay (); System.out.println ("- -");}}

Results:

Current payment method is: Alipay current payment method is: FACE-current payment method is: Wechat current payment method is: FACE-when The former payment method is: Alipay current payment method is: FINGER-current payment method is: Wechat current payment method is: FINGER--

4. Analysis

As the above two methods have achieved four kinds of payment process, which is mainly through payment channels (Alipay, Wechat), including two payment methods (face scan, fingerprint).

In terms of the amount of code:

Do not use: the code is still relatively small, but in fact, the main business logic should be unloaded from the Pay class, and the business logic is judged by if,else judgment.

Usage: the amount of code has increased a lot, and many classes have been added, but pay's business logic is only executed in its own classes, in line with a single responsibility.

From a scalable level, if cloud flash payment is added:

Do not use: the Pay class needs to be modified to add the logical judgment of cloud flash payment, which is not in line with the principle of opening and closing.

Use: only need to add a cloud flash payment channel, no need to modify other business logic, in line with the principle of opening and closing.

Code coupling level:

Do not use: code business logic is coupled together.

Used: code coupling is extremely low.

At this point, the study of "what is the bridging pattern of the design pattern series" is over. I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report