Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.

Re: Change the parent of an instance

by perrin (Chancellor)
on May 11, 2004 at 18:29 UTC ( [id://352531]=note: print w/replies, xml ) Need Help??

in reply to Change the parent of an instance

Whoa, bad idea. You should re-think your model so that you are changing properties, not identity, and make your code handle multiple types of objects that implement the same interface (polymorphism). For example, add a "is_carryable" method to all of your classes, with an implementation for the Foe class like this:

sub is_carryable { my $self = shift; if ( $self->is_alive() ) { # can't carry a live orc! return 0; } else { # dead body return 1; } }

Replies are listed 'Best First'.
Re: Re: Change the parent of an instance
by muba (Priest) on May 11, 2004 at 18:38 UTC
    Might be a good idea, altough in this case many of the methods will be in an if-else manner, just because the two things share a lot although they hardly share anything.

    I know it might look like I'm rejecting all advises, I'm aware of that, but the 'multimorphing' idea as it seems to be called is not very usable I think.
      What I'm suggesting is that you are thinking about it at the wrong level. What's important to your program might not be whether or not the object is a foe but rather whether or not it is carryable. Each kind of object may have unique rules about when it is carryable. In general, the idea is to push as much of the decision-making about how an object will behave in a certain situation into the object itself (by asking it with methods like this) rather than have some other code that decides what the object can do based on what type it is.

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-06-23 21:29 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.