J
Julien
Hi,
I have a situation where I would like to "instantiate" an object. Note
that I really mean object (which is an instance of its class), not
class. Hence, a child object inherits not only its parent object's
attributes (and the attribute values of the parent, which will be
common to all child instances), but also gains additional attributes
whose values may be different on a per-object-instance basis. So we
have per-instance data vs. object data, a bit analoguous to having
per-object attributes versus class data (which is common to all objects
of the class).
To do this, I've created a parent class called "Instantiable", which
provides a constructor method "new_instance()". When new_instance() is
called on an Instantiable object, it creates a new object of the class
"Instance". This new Instance object has a reference to it's parent
object (a weak reference, so that destruction of the parent does not
depend on destruction its children). The Instance class uses an
AUTOMETHOD (like AUTOLOAD, see Class::Std) such that if an unknown
method is called on an Instance, this is translated into a call using
the parent object's reference on a method of the same name and
arguments, but with an extra argument inserted to identify which object
instance was initially called. Finally, any sub-class of Instantiable
is expected to provide an "instance_init" method, which is called by
new_instance() when appropriate. The instance_init method can be
CUMULATIVE, so that you can instantiate a hierarchy of Instantiable
objects with one call. When implementing methods, there are now 3
types of method calls i) a class method, ii) an object (class-instance)
method and newly iii) an object-instance method.
The application is in a situation where you have a set of identical
objects (e.g. they are all of class Dog), but need to maintain separate
state information for each (e.g. sleeping, barking...), even though
they also common attributes (e.g. hair colour). Another example are
logic gates or flip-flops where you might have 1000s of identical
gates, but they are in a different state. My particular application
has to do with biochemical states of molecules.
If anyone is interested, I can post my code (it's not quite ready yet,
in a few days...). I would welcome comments and tips, I would like to
know if something equivalent or better already exists. I am not sure
if I am using the correct or best vocabulary for "object-instance". I
am not sure which is the best place to keep the instance data, though
currently I keep in the sub-class of Instantiable and that seems to
work. Finally, I would like to use attributes to designate certain
class attributes as being instance attributes instead of object
attributes (similar to ATTR designation in Class::Std). Also, it might
be reasonable to split instance_init into START_INSTANCE and
BUILD_INSTANCE to follow the standard set build Class::Std.
I've already searched CPAN for something similar but nothing jumps out
at me.
A quick and dirty synopsys:
# class InstantiableObject is a sub-class of Instantiable
$object = InstantiableObject->new();
# instance name is I0, InstantiableObject::instance_init(@args) is
called by new_instance
$object_instance = $object->new_instance("I0", @args);
$object->foo(); # call an object (class instance)
method
$object_instance->bar() # call an object-instance method (translated
to $object->bar("I0") )
##############
package InstantiableObject; {
use base qw(Instantiable);
use Class::Std;
# class methods
sub fee {
my $class = shift;
....
}
# instance methods
sub foo {
my $self = shift;
....
}
# object-instance method
sub bar {
my $self = shift;
my $instance = shift; #
.....
}
}
I have a situation where I would like to "instantiate" an object. Note
that I really mean object (which is an instance of its class), not
class. Hence, a child object inherits not only its parent object's
attributes (and the attribute values of the parent, which will be
common to all child instances), but also gains additional attributes
whose values may be different on a per-object-instance basis. So we
have per-instance data vs. object data, a bit analoguous to having
per-object attributes versus class data (which is common to all objects
of the class).
To do this, I've created a parent class called "Instantiable", which
provides a constructor method "new_instance()". When new_instance() is
called on an Instantiable object, it creates a new object of the class
"Instance". This new Instance object has a reference to it's parent
object (a weak reference, so that destruction of the parent does not
depend on destruction its children). The Instance class uses an
AUTOMETHOD (like AUTOLOAD, see Class::Std) such that if an unknown
method is called on an Instance, this is translated into a call using
the parent object's reference on a method of the same name and
arguments, but with an extra argument inserted to identify which object
instance was initially called. Finally, any sub-class of Instantiable
is expected to provide an "instance_init" method, which is called by
new_instance() when appropriate. The instance_init method can be
CUMULATIVE, so that you can instantiate a hierarchy of Instantiable
objects with one call. When implementing methods, there are now 3
types of method calls i) a class method, ii) an object (class-instance)
method and newly iii) an object-instance method.
The application is in a situation where you have a set of identical
objects (e.g. they are all of class Dog), but need to maintain separate
state information for each (e.g. sleeping, barking...), even though
they also common attributes (e.g. hair colour). Another example are
logic gates or flip-flops where you might have 1000s of identical
gates, but they are in a different state. My particular application
has to do with biochemical states of molecules.
If anyone is interested, I can post my code (it's not quite ready yet,
in a few days...). I would welcome comments and tips, I would like to
know if something equivalent or better already exists. I am not sure
if I am using the correct or best vocabulary for "object-instance". I
am not sure which is the best place to keep the instance data, though
currently I keep in the sub-class of Instantiable and that seems to
work. Finally, I would like to use attributes to designate certain
class attributes as being instance attributes instead of object
attributes (similar to ATTR designation in Class::Std). Also, it might
be reasonable to split instance_init into START_INSTANCE and
BUILD_INSTANCE to follow the standard set build Class::Std.
I've already searched CPAN for something similar but nothing jumps out
at me.
A quick and dirty synopsys:
# class InstantiableObject is a sub-class of Instantiable
$object = InstantiableObject->new();
# instance name is I0, InstantiableObject::instance_init(@args) is
called by new_instance
$object_instance = $object->new_instance("I0", @args);
$object->foo(); # call an object (class instance)
method
$object_instance->bar() # call an object-instance method (translated
to $object->bar("I0") )
##############
package InstantiableObject; {
use base qw(Instantiable);
use Class::Std;
# class methods
sub fee {
my $class = shift;
....
}
# instance methods
sub foo {
my $self = shift;
....
}
# object-instance method
sub bar {
my $self = shift;
my $instance = shift; #
.....
}
}