Timeline for Is "If a method is re-used without changes, put the method in a base class, else create an interface" a good rule-of-thumb?
Current License: CC BY-SA 3.0
9 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Aug 11, 2014 at 8:34 | comment | added | Stas Bichenko | After almost a year trying out different approaches, I find myself turning to this practice more and more, which often boils down to using strategy pattern. This is why I'm choosing this answer as accepted. | |
Aug 11, 2014 at 8:33 | vote | accept | Stas Bichenko | ||
Nov 13, 2013 at 18:04 | comment | added | Danny Tuppeny | @exizt Inheriting from a class is a big deal; you're taking a (likely-permanent) dependency on something, which in many languages you can only have one of. Code re-use is a fairly trivial thing to use up this often-only base class on. What if you end up with methods where you want to share them with another class, but it already has a base class because of reusing other methods (or even, that it's part of a "real" hierarchy used polymorphically(?)). Use inheritance when ClassA is a type of ClassB (eg. a Car inherits from Vehicle), not when it shares some internal implementation details :-) | |
Nov 13, 2013 at 13:27 | comment | added | Stas Bichenko | @DannyTuppeny Why not? | |
Nov 13, 2013 at 7:08 | comment | added | Danny Tuppeny | I can't believe this is the only answer of many to steer the OP away from inheritances :( Inheritance is not for code re-use! | |
Nov 12, 2013 at 21:23 | comment | added | Simon Bergot |
@exizt First, I am bad at choosing names. So PetEatingBehavior is probably the wrong one. I cannot give you an implementation since this is only a toy example. But I can describe refactoring steps: it has a constructor which takes all information used by cats/dogs to define the eat method (teeth instance, stomach instance, etc). It only contains the code common to cats and dogs used in their eat method. You simply have to create a PetEatingBehavior member and forward it calls to dog.eat/cat.eat.
|
|
Nov 12, 2013 at 21:10 | comment | added | Stas Bichenko |
Could you provide a basic example of PetEatingBehavior implementation?
|
|
Nov 12, 2013 at 20:51 | history | edited | Simon Bergot | CC BY-SA 3.0 |
added 1 characters in body
|
Nov 12, 2013 at 20:42 | history | answered | Simon Bergot | CC BY-SA 3.0 |