Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: Inheritance when subclass passed as a parameter

by davies (Prior)
on Feb 05, 2020 at 11:09 UTC ( [id://11112423]=note: print w/replies, xml ) Need Help??


in reply to Re: Inheritance when subclass passed as a parameter
in thread [Solved] Inheritance when subclass passed as a parameter

Thanks a lot. I'm pretty sure that a factory method is what I need. I've never used them before, so I have some googling to do. Are there any especially good (or bad) docs? I am certainly confused (which is why I'm coming here for help) and the fog is starting to lift thanks to everyone's help.

My problem with Service::XML->new is that I really need to call it as Service::$var->new, as I don't know which service will be used until I get the data. I tried various forms of that and got various syntax errors. Obviously I will have to do lots of error checking in case a service is requested that has no subclass, but I'm trying to get the basics working first. Which I won't if I make trivial blunders like the character class Choroba pointed out.

Regards,

John Davies

Update: https://www.perl.com/pub/2003/08/15/design3.html/ looks helpful to me, although I think there are some more modern constructs I could use.

Replies are listed 'Best First'.
Re^3: Inheritance when subclass passed as a parameter
by haukex (Archbishop) on Feb 05, 2020 at 11:27 UTC
    I'm pretty sure that a factory method is what I need. I've never used them before, so I have some googling to do. Are there any especially good (or bad) docs?

    I'm not sure, probably Wikipedia is a good start - but the basic concept is pretty simple, it's a class method in a central place somewhere that simply dispatches to the appropriate constructor depending on the arguments. The advantage is that the code calling the factory doesn't have to have any knowledge of the subclasses that exist, or their names. In a strongly typed language the factory method would simply be returning an object that is-a Service.

    My problem with Service::XML->new is that I really need to call it as Service::$var->new, as I don't know which service will be used until I get the data. I tried various forms of that and got various syntax errors.

    That'll only work if the entire class name is a string, as in my $class="Service::$var"; my $obj=$class->new;. Which is certainly an option, but if you want some flexibility in $var and/or better error messages in case of invalid $var values, then a factory method is probably better.

    These tests pass:

      wiki.c2.com is also another good design patterns resource (along with the original GangOfFour book ISBN 9780201633610).

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-19 05:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found