Your question is too abstract. The Factory Method Pattern is usually used to create objects of different classes. If the resulting object is always of the same class, just built in a different way, you rather want a Builder.

You can have a Factory of Builders, but I fear I used too much imagination so the following might not be usable for your situation. It also doesn't make any sense for the simple situation I created, but that's how OO programming works - without particular objects and behaviours, it's all hand waving. It's hard to abstract from something that's already been abstracted.

#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; { package MyClass; use Moo; has [qw[ id name data ]] => (is => 'ro', required => 1); sub job { my ($self) = @_; $self->data->{ $self->id } } } { package MyClass::Builder::FromElements; use Moo; has root => (is => 'ro'); sub build { my ($self, %data) = @_; return 'MyClass'->new(data => \%data, map +($_ => $self->root->findvalue("/r/$ +_")), qw( id name )) } } { package MyClass::Builder::FromAttributes; use Moo; has root => (is => 'ro'); sub build { my ($self, %data) = @_; return 'MyClass'->new(data => \%data, map +($_ => $self->root->findvalue("/r/\ +@$_")), qw( id name )) } } { package MyClass::Builder::Factory; use Moo; use XML::LibXML; has xml => (is => 'ro'); has _root => (is => 'lazy', init_arg => undef); sub build_builder { my ($self) = @_; if (2 == $self->_root->findvalue('count(/r/id | /r/name)')) { return MyClass::Builder::FromElements->new(root => $self-> +_root) } elsif (2 == $self->_root->findvalue('count(/r/@id | /r/@name +)')) { return MyClass::Builder::FromAttributes->new(root => $self +->_root) } else { die "Can't build.\n"; } } sub _build__root { my ($self) = @_; return 'XML::LibXML'->load_xml(string => $self->xml)->document +Element } } my %DATA = (12 => 'CEO', 14 => 'CTO'); for my $xml ( '<r><id>12</id><name>John</name></r>', '<r id="14" name="Jane"/>' ) { my $builder = 'MyClass::Builder::Factory'->new(xml => $xml)->build +_builder; my $o = $builder->build(%DATA); say join ', ', map $o->$_, qw( id name job ); }

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

In reply to Re: Using a factory class to return objects of the same class by choroba
in thread Using a factory class to return objects of the same class by Amblikai

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.