Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re: Conceptual Question About Classes and SubClasses

by merlyn (Sage)
on Aug 20, 2009 at 15:43 UTC ( #790119=note: print w/replies, xml ) Need Help??

in reply to Conceptual Question About Classes and SubClasses

If your CLASS::A object doesn't have the behavior you need for your project, and would benefit from this extra behavior, you could either subclass CLASS::A as you've described, or compose a project-specific object that includes a CLASS::A object as an instance variable (member). Both ways have merits and downsides, and without knowing more about your application, I can't recommend one over the other.

-- Randal L. Schwartz, Perl hacker

The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.

  • Comment on Re: Conceptual Question About Classes and SubClasses

Replies are listed 'Best First'.
Re^2: Conceptual Question About Classes and SubClasses
by ~~David~~ (Hermit) on Aug 20, 2009 at 15:56 UTC
    Thank you for your reply. If I added the additional information, would it change the way you lean:
    I would like my "subclass" of CLASS::A and CLASS::B to share a common set of methods. My project specific methods should work independently of which class they are working on. For instance, whether or not I have A or B, i would like to X->validate them... If I sub-classed them, I think I would have to write two validate methods, in each subclass. I was trying to avoid that.
    Again, thanks for the help... I am not a programmer by trade, I am a chemical engineer so need some conceptual help!
      I believe the right approach is to have a method in X that validates whatever is to validate in X. If you can make that method generic, so that it would work for A and B too, even better

      Any subclasses for which X->validate does everything that is to do are finished now. Any subclass that has to do something more would have their own validate method. That method would then call the validate method of the superclass ($self->SUPER::validate) and if that returns ok, validate the additional stuff of the subclass.

      As you can see, you probably need a validate method in A and in B, BUT you don't need to write any code twice.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://790119]
and the shadows deepen...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2018-07-19 08:10 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (404 votes). Check out past polls.