So now the instance has a instance of the delegate, and decides to pass control off to it as appropriate. If you have a standard subclass "is-a" relationship; the subclassed methods always have to resolve to the parent class. If you have a delegated "has-a" relationship, then any given instance of the class can be set up to have a delegate to handle methods selectively - and that delegate can be any class you want, as long as it can execute the delegated method. Also note that you can, with this setup dynamically change the delegate after the "subclass" is instantiated.package BasicX::AAA1; sub new { my($class, $delegate) = @_; my $self = {}; bless $self, $class; $self->delegate($delegate || BasicX->new()); return $self; } sub delegate { my $self, $new_delegate_instance) = @_; $self->{_delegate} = $new_delegate_instance if defined $new_delega +te_instance; return $self->delegate; } sub delegated_method { my $self = shift; if $self->delegate->can('delegated_method') { $self->delegate->delegated_method(); } else { # handle it yourself } }
In reply to Re: Multiple instances of the same base class
by pemungkah
in thread Multiple instances of the same base class
by jck000
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |