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

Re: Perl Factory Method Pattern?

by sandfly (Beadle)
on Oct 11, 2004 at 22:51 UTC ( [id://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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2024-04-23 10:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found