Friday, April 3, 2020

Structural Patterns: Bridge Vs Adapter Vs Decorator

Structural Patterns: Bridge Vs Adapter Vs Decorator

In this article we will look at Bridge, Adapter & Decorator pattern:

Bridge: Used to bridge two compatible interfaces
Adapter: Deals with two incompatible interfaces
Decorator: Enhance the same interface, such a way the existing object is not impacted


Bridge:

This pattern can be used when we want to bridge/combine two interface/subsystem 

For example Car and Registration are two subsystem, and RegisteredCar bridges both.


Adapter:

This pattern can be used to deal with two incompatible interface/subsystem. Adapter makes things work after they are designed.

For ex: Driver drives Car, now if we want to make driver drives auto?
AutoAdapter ensure driver actions (start, stop) are converted to auto operation (on/off)



Decorator:

This pattern enhances an object, without changing its interface

For example, Honda, Maruti can be enhanced to Sport Car without modifying Car/Maruti/Honda
With this SportCar can be built/enhanced over Maruti/Honda or any Car otherwise we might have built separate class says HondaSportCar & MarutiSportCar.




Hope this was useful one, if you have any question/comment, please comment below, i will try to answer my best. Thank you!