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)
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,
- bridge structural pattern
- abstraction , implementation not bound @ compile time
- abstraction , implementation - both can vary without impact in client
use bridge pattern when:
- you want run-time binding of implementation,
- you have proliferation of classes coupled interface , numerous implementations,
- you want share implementation among multiple objects,
- you need map orthogonal class hierarchies.
useful links:
tutorialspoint artice
dzone article
oodesign article
sourcemaking article
related post:
Comments
Post a Comment