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!

4 comments:

  1. Arun, you know Input Stream and Buggered reader are different interfaces/class hierarchy but still it's Decorator. Objective is to enhance the function without touching original implementation, but that doesn't mean they follow same abstraction. Check this again

    ReplyDelete
    Replies
    1. True. The idea is the existing object should not effected we can use inheritance or aggregation. In fact aggregation is better as you specified. It is up to implementer. Design pattern does not specify the implementation method it is left to user.

      Delete
  2. What I mean is SportsCar need not be. Car (in this example). It can ve SUV but still can enhance a Car

    ReplyDelete
    Replies
    1. Yes correct, I have reference of Car in sportscar. ie. sports car uses car not extends. There was limitation with free online tool i had to stick with couple of arrow & line. i used line for inheritance and arrow (directional) for aggregation.

      Delete