This is the 7th day of my participation in the August Text Challenge.More challenges in August

basis

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.

Suitable for application Scenarios

  • If you want to split or recombine a complex class with multiple functions, you can use bridge mode.
  • You can use this pattern if you want to extend a class on several independent dimensions.
  • If you need to switch between different implementations at run time, you can use bridge mode.

The advantages and disadvantages

  • The client code only interacts with high-level abstractions, not platform details.
  • Open and close principle. You can add abstractions and implementations without affecting each other.
  • Single responsibility principle. The abstraction part focuses on dealing with high-level logic, and the implementation part deals with platform details.
  • Using this pattern for highly cohesive classes can make your code more complex

For example,

The following example is about payment. The payment methods include wechat payment, Alipay payment, and then specific payment

  • Wechat password payment
  • Wechat fingerprint payment
  • Alipay password payment
  • Alipay fingerprint payment

Train of thought

Above two combination, can put through the bridge pattern design, it can be divided into abstract part and implementation part, a payment there are a number of payment mode (such as WeChat have a password, fingerprint payment), so the payment will be set to abstract parts, and will pay mode to achieve department, abstract class depends on the implementation class, If a new payment method or payment mode is added later, it can be better expanded.

Code practice

Payment mode implementation class

/** * Public interface IPayModel {String payModel(); }Copy the code
/** * @author * @date 2021-08-28 15:05 */ public class PasswordPayModel implements IPayModel {@override Public String payModel() {return "[password] "; }}Copy the code
/** * @author * @date 2021-08-28 15:04 */ public FingerprintPayModel implements IPayModel {@override Public String payModel() {return "payModel "; }}Copy the code

Payment method abstract class

/** * @author * @date 2021-08-28 17:22 * Public abstract class AbstractPayType {protected IPayModel IPayModel; public AbstractPayType(IPayModel iPayModel) { this.iPayModel = iPayModel; } protected abstract void pay(); }Copy the code
/** * @author extends AbstractPayType {public class extends AbstractPayType AliPayType(IPayModel iPayModel) { super(iPayModel); } @override public void pay() {system.out.println (" + ipaymodel.paymodel ()+"); }}Copy the code
Public class extends AbstractPayType {public class WeChatPayType extends AbstractPayType {public WeChatPayType(IPayModel iPayModel) { super(iPayModel); } @override public void pay() {system.out.println (" + ipaymodel.paymodel ()+"); }}Copy the code

The test class

Public class BridgeTest {public static void main(String[] args){new WeChatPayType(new PasswordPayModel()).pay(); new WeChatPayType(new FingerprintPayModel()).pay(); new AliPayType(new PasswordPayModel()).pay(); new AliPayType(new FingerprintPayModel()).pay(); }}Copy the code

The test results

reference

www.cnblogs.com/xiaofuge/p/…

Refactoring. The guru/design – patt…