Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^2: Designing multiple related modules

by Bod (Parson)
on May 12, 2021 at 16:34 UTC ( [id://11132502]=note: print w/replies, xml ) Need Help??


in reply to Re: Designing multiple related modules
in thread Designing multiple related modules

PS. I may be missing something but (4) Factory works in conjuction with (2), not as an alternative

I my mind, (2) would involve creating an instance of the subclass directly through Bod::Social::Twitter->new whereas (4) would rely on the factory class to create the subclass with Bod::Social::Factory->new(network => 'Twitter')

I think that (3) and (4) are probably the same thing but with just different terminology...aren't they?

Replies are listed 'Best First'.
Re^3: Designing multiple related modules
by bliako (Monsignor) on May 13, 2021 at 18:29 UTC

    Well, again I would call the Factory not a class but a package with a "static" method: Bod::Social::Factory::new_for(network => 'Twitter') (borrowing from Fletch's comment above). Perl makes that distinction possible whereas I guess in Java it would technically be a class. OTOH, why not if it suits you?

    That said, and purisms aside, I would expect the constructor of a class to return ... erm ... an object of that same class. In Java, I hope I guess correctly, it is impossible for a constructor to return anything other than the object of that class (there is no return statement as such). So, Bod::Social::Factory->new(network => 'Twitter') returning TwitterClass or XYZClass or ... depending on input params strikes me as a bit weird, or unexpected. Where Bod::Social::Factory::new_for(network => 'Twitter'); (notice the difference in ::new_for) is more intuitive for me. But even TIMTOWTDI is spelled in many ways hehe!

    Now, what would be inside new()/new_for()? I would put in there this:

    use Bod::Social::Twitter; sub new_for { my ($network, $params) = @_; if( $network eq 'Twitter' ){ return Bod::Social::Twitter->new($param +s) } elsif( ... ){ ... } die "don't know network $network" }

    Which also implies a class Bod::Social::Twitter, which can be a subclass of Bod::Social (option (2)) or be completely independent, if you did not find enough common ground to create a social API in Bod::Social

    That was helpful to me: https://www.tutorialspoint.com/design_pattern/factory_pattern.htm, but thankfully Perl is way less restrictive than Java.

    bw, bliako

      I would expect the constructor of a class to return ... erm ... an object of that same class

      Yes...that's kind of obvious but until you mentioned it wasn't front of mind.

      Having the constructor return the same class or a subclass of it has been added to my mental list of things to make sure of when creating a new module.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (1)
As of 2024-05-23 15:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found