java - Does the Bridge Pattern decouples an abstraction from implementation? -


i learned bridge pattern different articles , have implemented per understanding . 1 thing confusing me bridge pattern says

bridgepattern decouples abstraction implementation 2 can vary independently

what meaning of statement? implementation resides @ in separate jar ?

what meaning of vary independently statement ?

considering provided journaldev article, elaborate answer.

any appreciated.

bridgepattern decouples abstraction implementation.

abstraction , implementation can vary independently since concrete class not directly implement abstraction ( interface)

uml diagram wikipedia

key note: two orthogonal class hierarchies (the abstraction hierarchy , implementation hierarchy) linked using composition (and not inheritance).this composition helps both hierarchies vary independently.

implementation never refers abstraction. abstraction contains implementation interface member (through composition).

coming question regarding example code in journaldev article :

shape abstraction

triangle redefinedabstraction

color implementor

redcolor concreteimplementor

a concrete shape object : triangle extends shape not implement color interface.

public class triangle extends shape{ } 

redcolor , greencolor implement color interface.

the concrete shape object (triangle) independent of implementing abstraction (i.e. color interface).

shape tri = new triangle(new redcolor()); 

here triangle contains concrete color object ( composition). if there change in color abstraction (interface), redcolor , greencolor responsible implementing abstraction of color interface.

shapes triangle not affected changes in contract color interface. color interface can vary independently. possible because shape holds contract uses composition rather implementation.

in summary,

  1. bridge structural pattern
  2. abstraction , implementation not bound @ compile time
  3. abstraction , implementation - both can vary without impact in client

use bridge pattern when:

  1. you want run-time binding of implementation,
  2. you have proliferation of classes coupled interface , numerous implementations,
  3. you want share implementation among multiple objects,
  4. you need map orthogonal class hierarchies.

useful links:

tutorialspoint artice

dzone article

oodesign article

sourcemaking article

related post:

when use bridge pattern? how different adapter pattern?


Comments