Hello Amblikai,

I would be interested to know why each data object’s class has to change. However, given that it does, I would prefer a design in which the client code knows only the parent class (i.e., the interface), and has no knowledge of the various child classes. (That is, child classes are treated as “implementation details.”) Here is a sketch of how this can be accomplished:

#! perl use strict; use warnings; package Widget { use Scalar::Util qw( blessed ); sub new { my ($class, $data) = @_; my $self = { Data => $data }; return bless $self, $class; } sub rebless { my ($self, $new_info) = @_; my $class = blessed $self; die unless $class eq __PACKAGE__; $self->{Info} = $new_info; if ($self->{Info} =~ /WXYZ/) { $class = 'Widget_A'; } elsif ($self->{Info} =~ /STUV/) { $class = 'Widget_B'; } else { die "Unrecognised class: $!"; } return bless $self, $class; } sub frobnicate { # my ($self) = @_; print "Implementation of frobnicate() for all Widgets\n"; } sub tweak { die "method tweak() must be overridden in child classes: $!"; } } #-------------------- package Widget_A { use parent -norequire, qw( Widget ); sub tweak { # my ($self) = @_; print "method tweak() for Widget_A objects\n"; } } #-------------------- package Widget_B { use parent -norequire, qw( Widget ); sub tweak { # my ($self) = @_; print "method tweak() for Widget_B objects\n"; } } #-------------------- # Client code #use Widget; my $setup_data = 'Setup'; my $object_1 = Widget->new($setup_data); $object_1->frobnicate(); my $new_info = 'WXYZ'; my $object_2 = $object_1->rebless($new_info); $object_2->tweak();

Output:

2:28 >perl 1387_SoPW.pl Implementation of frobnicate() for all Widgets method tweak() for Widget_A objects 2:28 >

(Of course, the Widget, Widget_A, and Widget_B packages should occupy separate .pm files. I’ve run them together into the “main” .pl file only for my convenience.)

The chief advantage of this design is that whenever a new Widget child class is added, the only existing code that needs to change is the rebless method in Widget.pm.1 Client code remains unaffected.

1Update: Widget.pm must also use each of its child classes.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re: Reclassifying an object by Athanasius
in thread Reclassifying an object 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.