Few days back, I appeared for an interview and got the chance to present my Object Oriented design skills. I was given the task to design Switch and Light for a room. I had a pen and white board with me and from there my struggle started .....
Anybody who has heard of Bridge pattern will say it is meant to "decouple an abstraction from its implementation so that the two can vary independently" (http://en.wikipedia.org/wiki/Bridge_pattern)
However, this statement even after reading multiple times do not fit into my mind. I will try to explain this pattern in somewhat different way. In a typical software application, you may have multiple layers/hierarchy of code which will mix with other. Let us try to feel this using below examples:
- One layer could be Switch layer: OnOff Switch, DimmerSwitch(can be used for dim the light apart from on/off). Another Layer could be Equipment: Light, Fan, TV. These two layers can be mixed: OnOff Switch for TV,Light and Fan, DimmerSwitch for Bulb. Whenever, you encounter this kind of mixing think of Bridge pattern as a Horse coming for rescue. Bridge patterns helps us in avoiding class explosion (if we use possible combinations among layers) and one layer mixing/using another layer instance resolved dynamically at runtime.
- One layer could be Shapes to draw: Circle,Rectangle. Second layer could be drawing implementation: Vector based, Bitmap based. Instead of having VectorCircle, VectorRectange, BitmapCircle, BitmapRectangle (class explosion + maintenance nightmare when changed) we can use bridge to help us out .
Mixing of Switch with Bulb and light shown in above scenario. I hope "mixing of layers" makes more sense than "decouple an abstraction from its implementation so that the two can vary independently" !!!!
Another example:
Another example:
Do Share your feedback and comments.
No comments:
Post a Comment