[Working design mode] Bridging mode bridge

Source: Internet
Author: User

First, the mode analysis:

In the policy mode section, the strategy mode can be transformed into bridging mode by extending the holder to form an abstract class and then implementing multiple specific holders.

Bridging mode is defined as: separating the abstract from the implementation , so that they can be independently changed, in a software system, some types because of their own logic, it has two or more dimensions of the change, using bridging mode can be used to cope with changes in multi-dimensional.

There are two main points

1, there are two dimensions of change, each other's changes should not be disturbed by the other side

2, through the interface to correlate, a total of n*m species combination

3, one of the dimensions, save a reference to this other dimension

Second, the mode code:

Dimension of a

InterfaceImplementor { Public voidOperacitonimpl ();}classImplementoraImplementsimplementor{@Override Public voidOperacitonimpl () {System.out.println ("The Implementora method is executed."); }}classImplementorbImplementsimplementor{@Override Public voidOperacitonimpl () {System.out.println ("The Implementorb method is executed."); }}

Dimension Two,

Abstract code that holds a reference to dimension one and can be manually set

 public  abstract  class   abstraction { protected  Span style= "color: #000000;"    > Implementor Implementor;  public  void   Setimplementor (implementor implementor) { implementor;  public  void   operation () {};}  
 Public class extends Abstraction {    @Override    publicvoid  operation () {        System.out.println ( "Refinedabstraction was carried out");        Implementor.operacitonimpl ();    }}

Client code:

Perform dimension two, call dimension one through dimension two

 Public class Client {    publicstaticvoid  main (string[] args) {        abstraction Abstraction=new  refinedabstraction ();        Abstraction.setimplementor (new  Implementora ());        Abstraction.operation ();    }}

Third, the application scenario

The next chapter of the strategy model, the banking system in the transfer of not only one, such as the bank transfer, cross-bank transfer, second-generation payment, and so on, each method of transfer needs to calculate the handling fee, so here we transform the strategy mode to bridging mode, because the fee dimension does not change, so here only need to modify the transfer dimension

Four, Scene code

Define the transfer interface and retain the calculation fee interface

 Public Abstract class itransfer {    protected  ifee Feeimpl;      Public void Setifee (Ifee feeimpl) {        this. feeimpl=Feeimpl;    }       Public Abstract void execute ();}

Intra-Bank transfer method

 Public class extends Itransfer {    publicvoid  execute () {        System.out.println ("cross-line transfer started .... ");         int fee=feeimpl.getfee ();        System.out.println ("Handling fee:" +fee);        System.out.println ("cross-line transfer complete ....") ");    }}

Cross-line Transfer method

 Public class extends Itransfer {    publicvoid  execute () {        System.out.println ("cross-line transfer started .... ");         int fee=feeimpl.getfee ();        System.out.println ("Handling fee:" +fee);        System.out.println ("cross-line transfer complete ....") ");    }}

Client calls, may be network silver, cabinet surface and other channels

 Public class Client {    publicstaticvoid  main (string[] args) {        itransfer transfer =new  transferout ();        Transfer.setifee (new feefromremote ()); // the cost calculation method of the project is usually injected by the framework         Transfer.execute ();    }}

Execution results

cross-line transfer starts .... The processing fee is being obtained from the core: the bank transfer is completed .... 

V. Description

During the actual use, the transfer method is not initialized to the client, and then the fee calculation is set directly, and the transfer is usually accessed as a path resource, and the fee calculation method is injected into the IOC via spring.

[working design mode] Bridging mode bridge

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.