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

Re: Perl Factory Method Pattern?

by sandfly (Beadle)
on Oct 11, 2004 at 22:51 UTC ( #398323=note: print w/replies, xml ) Need Help??

in reply to Perl Factory Method Pattern?

To answer the questions you actually asked, in reverse order:
  • Yes, this is the way to rebless into a new class.
  • The way you've coded the "new" method, it calls _init using a method call on $self. It needs to be blessed for this to work. You could change this to a simple subroutine call: _init( $self ), and not bother with the first blessing.

What you've coded will work, and most of it is fine; but there are a couple of things I dislike about the class structure you've used:

  • First, you have two _init methods, which do different things: one is a factory method; the other is a genuine instance initialisation.
  • Second, your base class, ProcInfo, has two roles: first, it's a factory; second, it's a ProcInfo thingy.
You've said you have written a few of these; if you put the code from _init into a separate module, called, say, Factory, in a method called something like "makeOSSpecific", you can get reuse. The first parameter would be the base class name, so you'd call it from "new" something like: return Factory::makeOSSpecific( "ProcInfo", $self ). There would be no need to include Factory in the class hierarchy, you'd just require or use it in ProcInfo.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2022-01-22 07:34 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (61 votes). Check out past polls.