Re: duck-type-checking?



2008/11/12 Joe Strout <joe@xxxxxxxxxx>:

If you need to know that it walks like a duck, mates like a duck and
tastes like a duck when roasted, you probably want it to really /be/ a
duck and should go back to inheritance.

I can't agree; there are times when inheritance just won't do, for example
when you don't have control over the object creation, because they come from
some factory method you can't change. In that case you may need to make a
wrapper instead of a subclass, but if you've faithfully implemented the
interface of the original class, you should be able to use it wherever the
original class could be used (within reason).

And then the original class definition changes -- new members added --
but the ones from the factory class don't change, and so are no longer
compliant with the the factory class (which doesn't support the new
"form_pun_relating_to_avoiding_a_high_hazard()" method) . Fine. If you
checked for all the members of the class that your code uses, it makes
no difference. ABCs give you a way of doing this, but in their absence
it's a long list of checks. If you /can/ use inheritance then it saves
having to do those checks.

--
Tim Rowe
.



Relevant Pages

  • Re: Trying to understand the purpose of interfaces
    ... Twist the inheritance hierarchy. ... Take the duck example that was given by another poster. ... Quack method in RubberDuck, even though it doesn't inherit from Duck ... With an IQuack interface definition, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Trying to understand the purpose of interfaces
    ... Twist the inheritance hierarchy. ... contains that functionality somewhere in the hierarchy. ... Take the duck example that was given by another poster. ... Quack method in RubberDuck, even though it doesn't inherit from Duck ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Trying to understand the purpose of interfaces
    ... Twist the inheritance hierarchy. ... contains that functionality somewhere in the hierarchy. ... Take the duck example that was given by another poster. ... Quack method in RubberDuck, even though it doesn't inherit from Duck ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: how to cope with changing element data structure? (any design pattern for this)
    ... Before you start making the code changes, refactor the original class ... by using the right mix of delegation and inheritance. ...
    (comp.object)
  • Re: delegation vs. inheritance
    ... IMHO the word inheritance is not choosen well. ... In fact, you can inherit rectangle from square or square from rectangle, ... it quacks like a duck, ...
    (comp.object)