AlgorithmsCodilityPatternsTutorials

Although the same in some aspects, the bridge and adapter patterns serve different purposes.

Similar to strategy pattern in a manner that we provide a strategy on which to act upon, and similar level of code reusage,

the bridge pattern allows us to avoid unnecessary subclassing by splitting our hierarchy into multiple trees, and combine/bridge them together.

Maybe this one is best shown using a sketch like this one below. When u have a situation like this one:

 

Refactor it to:

It is immediately obvious how practical this solution is. Lets try to implement it:

First, lets define some sensors(inputs) with their specific’s dumbed down in order to not create a confusion.

Now, lets see how to put them to use. Lets say you can alarm your client by activating a siren or a danger light(output).

Now, you can combine the Heat and Motion sensors with siren or light freely, as long as the contract between them is not broken.

Next, lets see the composite pattern.