Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^2: OO Pattern Container x Elements and Method Chaining (House, Person, Inhabitant)

by NERDVANA (Friar)
on Oct 09, 2021 at 13:54 UTC ( #11137390=note: print w/replies, xml ) Need Help??


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

In 25 years of programming I have never actually run into a case where an element needed to know its container and also belong to multiple containers. *shrug*

In most cases I’ve had where the element knew about its container, it was created and owned by the container, so there was no way to create it independent from the container, and the container was free to choose whether the element would be persistent using weak references, or temporary and only held by the strong reference given to the caller when they retrieve the object.

If you have a sub X that needs to know about 2 objects, A and B, and A seems like a logical “container” and B seems like a thing that ought to be contained, then I would make A’s “get_B” return a temporary object C which holds strong references to A and B and has X as one of its methods.

package A; use Moo; has ‘b_set’, is => ‘rw’; sub get_b($self, $name) { return C->new(a => $self, b => $self->b_set->{$name}); } package B; use Moo; sub do_thing_with_a($self, $a) { … } package C; use Moo; has ‘a’, is => ‘ro’; has ‘b’, is => ‘ro’, handles => [qw( … )]; # all of b’s methods sub do_thing($self) { $self->b->do_thing_with_a($self->a); }
  • Comment on Re^2: OO Pattern Container x Elements and Method Chaining (House, Person, Inhabitant)
  • Download Code

Replies are listed 'Best First'.
Re^3: OO Pattern Container x Elements and Method Chaining (House, Person, Inhabitant)
by LanX (Sage) on Oct 09, 2021 at 17:21 UTC
    > In 25 years of programming

    Well I just dug up a book on OO modeling I bought about the same time ago, and it explicitly lists several object "associations" like this.

    If interested check on "aggregation", "composition", etc

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

Re^3: OO Pattern Container x Elements and Method Chaining (House, Person, Inhabitant)
by LanX (Sage) on Oct 09, 2021 at 14:06 UTC
    as I said, there is legacy code which is using Method Chaining (I didn't design it)

    $house->get_member("Willy)->clean_house();

    Now the model changed and Person "Willy" can be member of different households

    > I have never actually run into a case where an element needed to know its container

    I have no idea how method chaining would be implemented else, since ->clean_house() is referring to $house

    In the new model case ->get_member() can't return a Person object anymore

    One possibility discussed here is to return a Wrapper-Object Inhabitant ° which wraps the relation

    'Person ->belongs_ to-> House'

    and delegates accordingly.

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

    °) update s/Member/Inhabitant/ for consistency to previous posts

    UPDATE

    regarding your suggestion, see Re^3: OO Pattern Container x Elements and Method Chaining

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11137390]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2022-08-16 23:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?