Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: Designing an OO program with multiple inherited classes

by Bloodnok (Vicar)
on Dec 09, 2009 at 13:52 UTC ( #811925=note: print w/replies, xml ) Need Help??

in reply to Designing an OO program with multiple inherited classes

    How do I access a class variable from from within

The simple answer is (or should be), you don't - you should use an accessor method on the class instead.

    ...Obj is not a type of Foo or Bar; instead, Foo and Bar are parts of Obj...

With that one observation, you have both hit the nail squarely on the head and also gone some way to answering your own question viz: these days the preference is for composition/aggregation (see here for a discussion of the terms aggregation and composition) over inheritance.

A user level that continues to overstate my experience :-))

Replies are listed 'Best First'.
Re^2: Designing an OO program with multiple inherited classes
by punkish (Priest) on Dec 09, 2009 at 15:02 UTC
    these days the preference is for composition/aggregation
    Right then. But, how do I implement the above? Your link to the wikipedia article explained some, but what next? I am a relative OO-n00b, so you will have to guide me a bit more.

    Many thanks, and please, no Moose suggestions. :-)


    when small people start casting long shadows, it is time to go to bed

      You cannot get to those  my variables. Class variables are usually handled with some sort of package variable, like  %Obj::foo_cache , by creating accessor methods that are in the same scope (think closure) that the kiddie subclasses can call, or by squirreling away references to the data inside each and every instance. Perl only objectifies methods, nothing else, so games have to be played to get class data visible to subclasses.

      The way you're asking this question, I think you need to brush up on how Perl handles packages, name spaces, closures, and the like. The techniques are the same for OO and standard procedural.

      Also, this isn't "has-a", this is pure "is-a". At no point do I see you trying to allocate an intermediate object, and redirecting request to it. I don't know if that matters or not. For "has-a", Foo would not have Obj as a base, but would have a reference to a Obj object and would pass foo_cache requests to it.

      All that said, the easiest way is an accessor method in the Obj package, like this snippet

       sub foo_cache() { \%foo_cache; }

      and your Obj::Foo subclass would use stuff like

      if ( exists $self->foo_cache()->{$key} ) { # cache hit $value = $self->foo_cache()->{$key}; } else { # this means a cache miss $value = calculate_something_worth_caching(); $self->foo_cache()->{$key} = $value; # store in cache }

      If you think you've just broken encapsulation, and you've now tightly bound the Obj::Foo subclass to the implementation details of its parent Obj class, then you understand why I wouldn't do it it this way. But if you're just learning, this should get you going. Once you've gotten a bit more experience, convert that to having a get_foo() and set_foo() in Obj foo can call and be unaware of the details of %foo_cache. I mean, caches are supposed to be transparent, right?

      In a lot of cases, caching can be implemented with the Memoize module, so you might want to look at it too.

      - doug

      PS: Love the moose. Touch the moose. Grok the moose.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://811925]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2018-06-24 04:18 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.