How do you write a new method? Easy. You find similar code in the system then cut and paste it into the new class, then modify it as needed. This is a common approach but it leads to duplication of code and duplicated code is harder to maintain. Now, I'm not saying that you shouldn't cut and paste code. I'm saying that once you're finished, look at the original and look at the new code and try to find some way to make it common. Perhaps one more general method in the superclass would handle both cases. Maybe the superclass method would call helper methods which would be implemented differently in different subclasses. Maybe the code you're implementing is an algorithm that could be extracted into another object using the Strategy design pattern. Cutting and pasting code should set off warning bells that you're duplicating functionality and you should look at ways to remove the duplication.
Download