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

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
OK, .. I hope an example makes it clearer

Container = SharedHouse Element = Person ->do_something = ->pay_rent

this used to work as long as one Person could only live in ONE house at a time.

my $cont = SharedHouse->new('Prag'); my $elem = Person->new('Egon'); $cont->add_elem($elem); # method chaining $cont->get_elem('Egon')->pay_rent();

Now the requirement changed to manage multiple houses in the same program with overlapping sets of inhabitants.

my best guess is that get_elem should now return an object of a new class Inhabitant pointing to one SharedHouse and one Person

  • handles methods involving the Container directly
  • delegates other methods to an object Person
like this, these would return different objects of type Inhabitant

$elem1 = $cont1->get_elem('Egon'); $elem2 = $cont2->get_elem('Egon');

but both are internally pointing to the same Person 'Egon'

$elem1->{person} == $elem2->{person}

such that

$elem1->pay_rent() pays the rent for the SharedHouse object in $cont1


$elem1->comb_hair() delegates to $elem1->{person}->comb_hair()

I hope it's clearer now. :)

(the real model is even more complicated, since the container is actually a matrix of two types of elements and values in the cell. Think of objects of type Room like $kitchen, and $Egon->owns("Kitchen", "Table"); )

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

In reply to Re: OO Pattern Container x Elements and Method Chaining (House, Person, Inhabitant) by LanX
in thread OO Pattern Container x Elements and Method Chaining by LanX

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2022-01-28 15:52 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (73 votes). Check out past polls.