in reply to Do I need a Factory Class At This Point?
package Tree; sub add_item { my ($self, $item) = @_; my $parent_node = ...; $parent_node->add(new Node($item)); }
would be transfored into the following when using a factory:
package Tree; sub add_item { my ($self, $item) = @_; my $node_factory = $self->{node_factory}; my $parent_node = ...; $parent_node->add($node_factory->create($item)); } sub set_node_factory { my ($self, $node_factory) = @_; $self->{node_factory} = $node_factory; } package NodeFactory; # Overridable sub create { shift(@_); return Node->new(@_); }
But that's pretty useless in Perl, since it lacks the typing of Java. In Perl, you could easily accept a class name (e.g. 'Node') or a code ref (e.g. sub { Node->new(@_) }) instead of a factory.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Do I need a Factory Class At This Point?
by jffry (Hermit) on Feb 03, 2006 at 17:45 UTC | |
by ikegami (Patriarch) on Feb 03, 2006 at 21:23 UTC |