Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Update: added a more intuitive example here


Lets say we have two classes Container and Element

and you can

  • add Elements to a Container
  • extract them
  • apply method chaining
my $cont = Container->new(); my $elem = Element->new('name'); $cont->add_elem($elem); # method chaining $cont1->get_elem('name')->do_something(); # identical to $elem1 = $cont1->get_elem('name'); $elem1->do_something(); # *

For this to work does $elem2 obviously need to know that it belongs to $container.

So it's N->1 : N Element -> 1 Container

and $elem1 == $elem

with updated property $elem->{member_of}=$cont Now after using this for a long time new requirements arise and Elements need to belong to multiple Containers.

So now it's N->M : N Element -> M Container

Clearly the old model with $elem2 == $elem doesn't work anymore because

$elem2 = $cont2->get_elem('name'); can't be member_of two different containers to allow method chaining

I don't think that $elem1 and $elem2 should belong to class Element either, but to a "wrapper" class ContainerElement referencing $elem, i.e. $elem1->{master}=$elem

Then I could think of many solutions, some involving inheritance, some AUTOLOAD to make sure that

$elem1->do_something(...) always does $elem1->{master}->do_something(..)

without hardcoding all methods.

I don't wanna reinvent the wheel and I'm already starting to worry too much about performance so here the question ...

What are the usual OO-Patterns to solve this? :)

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

update

*) OK Sorry, do_something() is doing something with the relation $cont <- $elem like set_weight($cont,$elem)


In reply to OO Pattern Container x Elements and Method Chaining by LanX

Title:
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 How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-25 09:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found