Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Feedback on Class::Container

by kwilliams (Sexton)
on Jun 30, 2002 at 07:09 UTC ( #178328=perlquestion: print w/replies, xml ) Need Help??

kwilliams has asked for the wisdom of the Perl Monks concerning the following question:


I've been working on the new Class::Container module, and I'm interested in getting feedback on it, and trying to figure out its place in the OO world:

It solves certain issues in Perl OO framework development, explained in the "Scenario" section of its POD. I'm not sure whether these issues don't exist in other languages, or whether they exist and have well-known solutions, or whether they exist and typically aren't solved in any good way. Might anyone be able to shed some light?

I know I've found Class::Container to be useful in two large-ish OO frameworks so far (HTML::Mason 1.10 [released June 25] and AI::Categorizer 0.05 [as yet unreleased]).


Replies are listed 'Best First'.
Re: Feedback on Class::Container
by educated_foo (Vicar) on Jun 30, 2002 at 15:55 UTC
    From reading the "Scenario" section of the pod, this sounds like "factory methods." Passing the parent class constructor parameters to initialize a child class is a bit unorthodox. Normally, you'd either pass these to the child class constructor, which would filter out the ones it wants, and pass the rest to its parent, or make a factory method in the parent, like so:
    sub Parent::factory { my $type = shift; # ... $type->new(@_); }
    This way each constructor takes what it wants, and if there are leftovers in the base class constructor, it can signal an error.


      educated_foo wrote:
      From reading the "Scenario" section of the pod, this sounds like "factory methods."

      Yes, it can definitely be used that way, but it can do more too. It supports simple "has-a" relationships, "factory" relationships, and "uses" relationships.

      Also, you're talking about "parent" or "child" - it seems like you're thinking of @ISA inheritance, but Class::Container is focused primarily on containment relationships.

      Assuming you really did mean containment relationships, if I used a bottom-up approach like the one you're suggesting, I'd have to manage lots of bottom-level objects instead of just one top-level object. That seems very inconvenient. It is supported by Class::Container if you want to do that, though - you can affect a contained object "wheel" (of class "Wheel") when creating the "car" (of class "Car") by any of these three approaches:

      • pass constructor parameters for the default "Wheel" class to the "Car" constructor
      • pass a "wheel_class" parameter to the "Car" constructor that could change the wheel's class to "My::Wheel", optionally including any additional parameters that "My::Wheel" takes
      • pass an already-created "wheel" object to the "Car" constructor
Re: Feedback on Class::Container
by samtregar (Abbot) on Jun 30, 2002 at 17:45 UTC
    Very interesting module. I think there's a good bit of overlap with Class::MethodMaker which also has some advanced composition support. I think that if I were implementing Class::Container I would do it as a set of new method generators for Class::MethodMaker.

    Reading the POD the only thing that wasn't clear to me was what the "parse" key in the hash passed to valid_params() does. Also, how does the Params::Validate tie-in work exactly? Perhaps you could add an example.


      Hi Sam,

      I'm not sure it's very similar to Class::MethodMaker. I had a hard time wading through MethodMaker's docs, though - what part do you mean by "advanced composition support"?

      The "parse" key doesn't do anything - the remarks after that example say that unknown parameters like "parse" are just ignored. In Mason, it's actually used to parse things from the httpd.conf file, but Class::Container knows nothing about this.

        Class::MethodMaker has a method maker called "object" that declares a slot to be of a particular object type. For example:

        use Class::MethodMaker new_hash_init => 'new', object => [ HTML::Mason::Lexer => 'lexer' ];

        You can then choose to proxy selected methods from the contained class. For example, to create a class called CGI::Simple that contains a object in a slot called 'query' and proxies new() and param() to it:

        package CGI::Simple; use Class::MethodMaker new_hash_init => 'new', object => [ CGI => { slot => 'query', comp_mthds => [ 'new', 'param' ], } ];

        This seems just nextdoor to your module to me.


Re: Feedback on Class::Container
by SteveRoe (Novice) on Jul 01, 2002 at 16:21 UTC

    This looks like a good way to work towards writing patterns in perl - this reminded me of the Composite pattern (as described by Gamma et Al). Is this the idea?

    It appears that the top down approach you have taken (with delayed build) is a good way to manage object proliferation - are you planning to add support for object re-use as per the Flyweight pattern? What about Iterators and Visitors???

    Are there any robust implementations of such things already in existence?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://178328]
Approved by ariels
Front-paged by ariels
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2021-06-14 14:10 GMT
Find Nodes?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)

    Results (62 votes). Check out past polls.