Let's discuss a small scenario.
Consider we have a web form which contains a group of Pair elements (Textbox and button).
When the button is clicked for the first time the corresponding textbox color changes to something else and when clicked again returns to normal.
In the form, handle the click event for each as follows:
You can see here, much of the code is repeating.
Imagine the situation when we will have a group of say 10 pairs.
- Code management is difficult.
- Very difficult to extend the functionality as any change requires changes in all blocks.
- As the group increases the size increases in the same extent resulting in a performance penalty.
- Testing is difficult since we will not be sure whether a block is working properly even when others work great.
Wow!!! What's that?
- We have only a single copy of the actual logic so code management is easy.
- Easily extend to more functionality.
- Even when the number of groups increase the size of the code increases only a little.
- Logic is working for one group => Logic will work for all groups.
Download the source code attached and don't forget to share your comments here.