elTriberium has asked for the wisdom of the Perl Monks concerning the following question:
I have a question about designing Moose classes: I have a module A which has multiple features B, C, D, because multiple people will work on these and it would be nice to have this logical separation of the different features. The features B, C, D need to have access to A themselves (they run something on A).
I want to put A, B, C, D, ... into different files.
The user should only have to instantiate A and then use the other classes through it, such as:
my $a = A->new(); $a->b->method(); $a->c->another_method();
package A; has b => ( isa => 'B' ); has c => ( isa => 'C' ); sub BUILD { my ($self) = @_; $self->b(B->new(a => $self)); $self->c(C->new(a => $self)); } package B; has a => ( isa => 'A', required => 1); sub method { my ($self) = @_; $self->a->exec(...); # Stuff related to B } package C; has a => ( isa => 'A', required => 1); sub another_method { my ($self) = @_; $self->a->exec(...); # Stuff related to C }
But this solution doesn't seem to be very 'clean' apart from that it does what it should. It's not making use of any OO concepts, it's just hardcoding the different classes together.
I'm not very familiar with Moose Roles, could I use those to achieve the desired behavior?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Moose design question
by moritz (Cardinal) on May 19, 2011 at 19:22 UTC | |
by elTriberium (Friar) on May 19, 2011 at 20:18 UTC | |
Re: Moose design question
by John M. Dlugosz (Monsignor) on May 19, 2011 at 19:22 UTC | |
Re: Moose design question
by TomDLux (Vicar) on May 22, 2011 at 05:52 UTC |