Yes, its always confusing or rather challenging to understand exact difference between Adapter and Bridge - both are very similar, isn't it? Additionally its more difficult to recall it whenever asked, don't worry I got a simple technique here.
Just remember first letter from pattern name and that's it. :)
Adapter starts with letter 'A' - map with After.
Bridge starts with letter 'B' - map with Before.
Adapter comes into picture in a common scenario of integration with another(third party) system/module which is not designed to produce output suitable for first system. Assume that there are two independent modules are already in place, working perfectly fine but they can't talk with each other because they don't follow common mechanism for input/output - there comes superhero "Adapter" to save us. Adapter works like middle-man between Person1 (Module/Program) and Person2 (Module/Program), where Person1 delivers data in "xml" format but Person2 accepts data in "Json" format, now middle-man adapter helps in parsing and converting data from xml to json format and makes end-to-end system working. On the other side Bridge is before hand, when the given problem can be solved with orthogonal class hierarchy but get stuck with scalability issue and also limits extension, bridge design pattern helps by putting structure in such a way that interface and implementation both are independent so abstraction is covered at interface level but actual implementation differs based on specific condition or need. Interface is a center point which acts as bridge between independent classes and with the set of classes implementing interface.
The easiest example and explanation I got from following link:
Adapter:
http://www.allapplabs.com/java_design_patterns/adapter_pattern.htm
Bridge:
http://www.allapplabs.com/java_design_patterns/bridge_pattern.htm
http://www.tutorialspoint.com/design_pattern/bridge_pattern.htm
https://sourcemaking.com/design_patterns/bridge
If you like this technique then please share your comments or feedback.
Best clarification I've seen for this comparison.
ReplyDeleteThanks a lot is very excellent explanation
ReplyDelete