Re: Question about removing duplication: inheritance vs static methods

From: Steve (steve_a_haigh_at_hotmail.com)
Date: 10/15/03


Date: Wed, 15 Oct 2003 10:49:24 +0100

On Tue, 14 Oct 2003 22:12:48 +0300, tonis
<tonis22@nosp-am.starline.ee> wrote:

>Hi!
>
>I have realized that duplication is something to avoid but it isn't
>always easy to do so. One way is to use inheritance: if two classes have
>something common extract superclass with common code(using 'Template' or
>'Strategy' pattern etc). But this sometimes introduces artificial
>coupling, subclasses become dependent from superclass which can change.
>And if subclass needs modifying programmer most know much about what
>superclass is doing, also code becomes hard to read/browse ....

If you have extraced a method to a superclass you may have identified
*real* coupling, not artificial. However you have a good point if the
super class was created only as a place to hold the common method you
may have a problem. I would only want a superclass if there was a
genuine generalization relationship.

It's really hard to answer your question without some examples of the
problem. The correct answer could be anything!

Have you read "Refactoring" by Fowler? I suspect that it may answer
your questions.

The issue of code being hard to browse is addressed by good tools
(Eclipse is free so there's no excuse!) and by good developers (not
free, but they can learn, so there's no excuse).

>
> Second way is to extract static methods and put it in util classes.
>Probably more easy to do & use but not flexible and I guess not good
>design either. Also, I have noticed that following test first
>programming combined with maxim 'Do simplest thing possible' often leads
>along that way -- producing code full of static methods.

This will probably lead to a non-OO result which will not benefit from
all the good stuff (the abstaction, polymorphism etc). It will end up
being hard to maintain and difficult for other java developers to
understand.
>
>Another question: I have extracted method but don't know where to attach
>it

Then put it back until you know where to attach it:-) As above,
without some more info it is imposible to say what the right answer
is.

~ If emailing, please use: Steve_A_Haigh
~ @
~ hotmail.com
~



Relevant Pages

  • Re: yet another deriving question
    ... "Are there any virtual methods in the superclass i.e. are you overriding in subclasses?" ... that this is not a good case for introducing common superclass. ... Implementation inheritance usually only gets into trouble if subclasses can override the superclass implementations. ... The preference of GoF for delegation over generalization is a very good idea, ...
    (comp.object)
  • Re: Question about removing duplication: inheritance vs static methods
    ... subclasses become dependent from superclass which can change. ... > Second way is to extract static methods and put it in util classes. ... If A and B also have some duplicate data (ie the data that X and X' ... new class (Delegate) to hold the data, and give Delegate a method X''. ...
    (comp.object)
  • Re: yet another deriving question
    ... "Are there any virtual methods in the superclass i.e. are you ... that this is not a good case for introducing common ... duplicate code and thus improves maintainability. ... Open/Closed principle. ...
    (comp.object)
  • Re: yet another deriving question
    ... Today I was talking to a colleague about some problem. ... "Are there any virtual methods in the superclass i.e. are you ... that this is not a good case for introducing common ... In a dynamic typed language, base classes should be useful without ...
    (comp.object)
  • RE: Very easy question about filter!!!
    ... Extracting Values Common To Two Lists ... You can easily extract values that appear in both of two lists. ... > I would like to have a third column if john, ...
    (microsoft.public.excel.misc)