Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Instance data inheritance?

by herveus (Prior)
on May 16, 2005 at 11:25 UTC ( [id://457417]=note: print w/replies, xml ) Need Help??


in reply to Instance data inheritance?

Howdy!

If you have a case where MyClass::Item ISA MyClass, then you can rebless the object into the subclass. I'm using that technique right now on a project.

In my case, I have a set of fairly closely coupled classes (and that coupling is reasonable in this case). The parent class carries the data used all over the place and the subclasses carry more task specific data. As the object (and it could well be a Singleton) gets passed from one phase to another, it gets reblessed into the class that encapsulates the phase specific state and behavior.

This may not be the best structure, but it has fallen out rather naturally as I grok this ball of mud I've inherited.

In my case, I take care to capture the previous class and (effectively) localize the class change to the phase in question.

yours,
Michael

Replies are listed 'Best First'.
Re^2: Instance data inheritance?
by merlyn (Sage) on May 16, 2005 at 14:13 UTC
    In Smalltalk, rather than doing the equivalent of "rebless", a much more easy-to-understand metaphor is to have the object carry ("has a") its current "state" as an instance variable, so the data payload would remain constant, but things that you would have changed with "reblessing" are all delegated to an updated-as-necessary "partial brain".

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Re^2: Instance data inheritance?
by blazar (Canon) on May 17, 2005 at 07:41 UTC
    If you have a case where MyClass::Item ISA MyClass, then you can rebless the object into the subclass. I'm using that technique right now on a project.
    I must say that I had thought of using "real inheritance" by cloning the "parent instance" data (or some relevant portion of it) into the child objects - however it seeemed to me to be totally unacceptable in terms of data payload.

    Now that you point it out, reblessing doesn't seem to be affected by this downside, yet I'm slightly concerned about it, from the conceptual poin of view since it conveys the impression of including data into the "item" objects that really doesn't belong to them (but they must have access to it).

      Howdy!

      The objects are initially created as X, then serially reblessed into subclasses of X. Each subclass extends the state and behavior from there.

      yours,
      Michael
        Howdy!
        Fine thanks. (Except that I should have given an exam today, and -yet another time- I didn't!)
        The objects are initially created as X, then serially reblessed into subclasses of X. Each subclass extends the state and behavior from there.
        So far so fine - nothing unexpected or surprising! But then... I must apologize for maybe it's just me, but I'm not really sure which exact statement of mine you're addressing.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://457417]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (9)
As of 2024-04-23 09:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found