Computer Science Design Patterns/Bridge

{{Computer Science Design Patterns/Page Bridge pattern is useful when a code often changes for an implementation as well as for a use of code. In your application, you should have provider classes and client classes: {{{!}} cellspacing="0px" style="margin: auto;" {{!}} {{{!}} cellspacing="0px" style="border: solid 2px black;" {{!}} style="border-bottom: solid 1px black; text-align: center;"{{!}}Client1 {{!}}- {{!}} style="border-bottom: solid 1px black;"{{!}} - provider: Provider {{!}}- {{!}} {{!}}} {{!}} {{!}} {{{!}} cellspacing="0px" style="border: solid 2px black;" {{!}} style="border-bottom: solid 1px black; text-align: center;"{{!}}Client2 {{!}}- {{!}} style="border-bottom: solid 1px black;"{{!}} - provider: Provider {{!}}- {{!}} {{!}}} {{!}}... {{!}} {{{!}} cellspacing="0px" style="border: solid 2px black;" {{!}} style="border-bottom: solid 1px black; text-align: center;"{{!}}ClientN {{!}}- {{!}} style="border-bottom: solid 1px black;"{{!}} - provider: Provider {{!}}- {{!}} {{!}}} {{!}}style="font-size:200%"{{!}}&loz;&mdash; {{!}} colspan="5" {{!}} {{{!}} cellspacing="0px" style="border: solid 2px black; width: 100%;" {{!}} style="border-bottom: solid 1px black; text-align: center;"{{!}}Provider {{!}}- {{!}} style="border-bottom: solid 1px black;"{{!}} {{!}}- {{!}} + doProcess {{!}}} {{!}}- {{!}} colspan="6" {{!}} {{!}} {{!}} {{!}} {{!}}... {{!}} {{!}}- {{!}} colspan="6" {{!}} {{!}} {{{!}} cellspacing="0px" style="border: solid 2px black;" {{!}} style="border-bottom: solid 1px black; text-align: center;"{{!}}Provider1 {{!}}- {{!}} style="border-bottom: solid 1px black;"{{!}} {{!}}- {{!}} + doProcess {{!}}} {{!}} {{!}} {{{!}} cellspacing="0px" style="border: solid 2px black;" {{!}} style="border-bottom: solid 1px black; text-align: center;"{{!}}Provider2 {{!}}- {{!}} style="border-bottom: solid 1px black;"{{!}} {{!}}- {{!}} + doProcess {{!}}} {{!}}... {{!}} {{{!}} cellspacing="0px" style="border: solid 2px black;" {{!}} style="border-bottom: solid 1px black; text-align: center;"{{!}}ProviderN {{!}}- {{!}} style="border-bottom: solid 1px black;"{{!}} {{!}}- {{!}} + doProcess {{!}}} {{!}}}
 * mode={{{mode|reading}}}
 * page={{{page|{{SUBPAGENAME}}}}}
 * stage=25
 * stage date=21 May 2013
 * stage comment=Add more illustrations.
 * previous=Adapter
 * next=Builder
 * content=

Each client class can interact with each provider class. However, if the implementation changes, the method signatures of the Provider interface may change and all the client classes have to change. In the same way, if the client classes need a new interface, we need to rewrite all the providers. The solution is to add a bridge, that is to say a class that will be called by the clients, that contains a reference to the providers and forward the client call to the providers.

{{{!}} cellspacing="0px" style="margin: auto;" {{!}} style="vertical-align: bottom;"{{!}} {{{!}} cellspacing="0px" style="border: solid 2px black;" {{!}} style="border-bottom: solid 1px black; text-align: center;"{{!}}Client1 {{!}}- {{!}} style="border-bottom: solid 1px black;"{{!}} - bridge: Bridge {{!}}- {{!}} {{!}}} {{!}} {{!}} style="vertical-align: bottom;"{{!}} {{{!}} cellspacing="0px" style="border: solid 2px black;" {{!}} style="border-bottom: solid 1px black; text-align: center;"{{!}}Client2 {{!}}- {{!}} style="border-bottom: solid 1px black;"{{!}} - bridge: Bridge {{!}}- {{!}} {{!}}} {{!}}... {{!}} style="vertical-align: bottom;"{{!}} {{{!}} cellspacing="0px" style="border: solid 2px black;" {{!}} style="border-bottom: solid 1px black; text-align: center;"{{!}}ClientN {{!}}- {{!}} style="border-bottom: solid 1px black;"{{!}} - bridge: Bridge {{!}}- {{!}} {{!}}} {{!}}style="font-size:200%"{{!}}&loz;&mdash; {{!}} style="vertical-align: bottom;"{{!}} {{{!}} cellspacing="0px" style="border: solid 2px black;" {{!}} style="border-bottom: solid 1px black; text-align: center;"{{!}}Bridge {{!}}- {{!}} style="border-bottom: solid 1px black;"{{!}} - provider: Provider {{!}}- {{!}} + doProcessForClient {{!}}} {{!}}style="font-size:200%"{{!}}&loz;&mdash; {{!}} style="vertical-align: bottom;" colspan="5" {{!}} {{{!}} cellspacing="0px" style="border: solid 2px black; width: 100%;" {{!}} style="border-bottom: solid 1px black; text-align: center;"{{!}}Provider {{!}}- {{!}} style="border-bottom: solid 1px black;"{{!}} {{!}}- {{!}} + doProcess {{!}}} {{!}}- {{!}} colspan="8" {{!}} {{!}} {{!}} {{!}} {{!}}... {{!}} {{!}}- {{!}} colspan="8" {{!}} {{!}} {{{!}} cellspacing="0px" style="border: solid 2px black;" {{!}} style="border-bottom: solid 1px black; text-align: center;"{{!}}Provider1 {{!}}- {{!}} style="border-bottom: solid 1px black;"{{!}} {{!}}- {{!}} + doProcess {{!}}} {{!}} {{!}} {{{!}} cellspacing="0px" style="border: solid 2px black;" {{!}} style="border-bottom: solid 1px black; text-align: center;"{{!}}Provider2 {{!}}- {{!}} style="border-bottom: solid 1px black;"{{!}} {{!}}- {{!}} + doProcess {{!}}} {{!}}... {{!}} {{{!}} cellspacing="0px" style="border: solid 2px black;" {{!}} style="border-bottom: solid 1px black; text-align: center;"{{!}}ProviderN {{!}}- {{!}} style="border-bottom: solid 1px black;"{{!}} {{!}}- {{!}} + doProcess {{!}}} {{!}}}

In the future, the two interfaces (client/bridge and bridge/provider) may change independently and the bridge may trans-code the call order.

Examples
It's hard to find an example in a library as this pattern is designed for versatile specifications and a library does not change constantly between two versions.

Cost
The cost of this pattern is the same as the adapter. It can be planned at design time but the best way to decide to add it is the experience feedback. Implement it when you have frequently changed an interface in a short time.

Creation
Its implementation is easy but can be expensive. It depends on the complexity of the interface. The more methods you have, the more expensive it will be.

Maintenance
If you always update the client/bridge interface and the bridge/provider interface the same way, it would be more expensive than if you do not implement the design pattern.

Removal
This pattern can be easily removed as automatic refactoring operations can easily remove its existence.

Advices

 * Put the bridge term in the name of the bridge class to indicate the use of the pattern to the other developers.

Implementation
The following Java (SE 6) program illustrates the bridge pattern.

It will output: API1.circle at 1.000000:2.000000 radius 7.5000000 API2.circle at 5.000000:7.000000 radius 27.500000

Output: API1.circle at 1:3 radius 17.5 API2.circle at 5:7 radius 27.5

C#
The following C# program illustrates the "shape" example given above and will output: API1.circle at 1:2 radius 7.5 API2.circle at 5:7 radius 27.5

C# using generics
The following C# program illustrates the "shape" example given above and will output: API1.circle at 1:2 radius 7.5 API2.circle at 5:7 radius 27.5

The following Python program illustrates the "shape" example given above and will output: API1.circle at 1:2 7.5 API2.circle at 5:7 27.5

An example in Ruby.

A Scala implementation of the Java drawing example with the same output.

An example in D.

This example in Perl uses the MooseX::Declare module.

The following Delphi program illustrates the "shape" example given above and will output: API1.circle at 1:2 7.5 API2.circle at 5:7 27.5

}}