in reply to "polymorphism" hack: nuts or wise?

Someone should follow up my node: If you can determine which datatype is being "inserted" into your container, by checking the inheritance tree and by multi-inheriting a dummy class dictating the class type (node vs something else), you may come up w/ a cleaner implementation. You won't have these random methods in your datatypes-to-be-inserted that will make no sense out of context of the applicaiton.

In the end, you want a good cohesion, but loose coupling. Imagine if you took half of your classes and threw them in another project. Will those classes and their extra methods make sense elsewhere?

This doesn't easily apply to business-level classes, since everyone's business works different. :)

----
Give me strength for today.. I will not talk it away..
Just for a moment.. It will burn through the clouds.. and shine down on me.

Replies are listed 'Best First'.
Re^2: "polymorphism" hack: nuts or wise?
by Ovid (Cardinal) on Aug 26, 2005 at 18:01 UTC

    Can you give a good example of how that works here? The original poster didn't say these objects were related via inheritance. A Tree probably isn't a subclass of Trees. The latter sounds like a collection of the former. The only inheritance being talked about in that node is the "listable" abstract base class. Trying to shoehorn inheritance into this model could be a bad thing, but maybe you're seeing something I'm not.

    Cheers,
    Ovid

    New address of my CGI Course.

      In this case, inheritance would just be used to mark classes as implementing a certain interface. There is no "implements" equivalent in Perl, so you can fake it by making an empty class with a descriptive name and POD that describes an interface, and then adding it to the @ISA for classes which implement that interface. Then you have a simple way to check, via isa().

        Ah, now I get it. I was thinking that inheritance is bad because what one really cares about is interface, not type. Faking "implements" makes that problem go away. (Another tool for my box. Thanks!)

        Cheers,
        Ovid

        New address of my CGI Course.

      Sure. And I'm suggesting inheritance as a different road than using methods to determine type. He doesn't wish for nodes to be inserted into certain containers, no?

      Check out Re: "polymorphism" hack: nuts or wise?,(Update Re^3: "polymorphism" hack: nuts or wise?). Grape limes link a bike?

      ----
      Give me strength for today.. I will not talk it away.. Just for a moment..
      It will burn through the clouds.. and shine down on me.

      Yes, you're right: a node isn't a special case of a tree - and neither is a tree a special case of a trees object. Also, I'm a bit worried about multiple inheritance. I may want to implement use fields; and I can imagine going crazy if I have multiple inheritance.