Don't ask to ask, just ask | |
PerlMonks |
Polymorphism and Abstractionby hypochrismutreefuzz (Scribe) |
on Aug 15, 2004 at 16:49 UTC ( [id://383108]=perlmeditation: print w/replies, xml ) | Need Help?? |
I was thinking in regards to It's a dog, but what kind? (polymorphism , in Perl OO), "Its a dog, but what kind". Polymorphism in Perl seems to me to be largely a matter of style. For instance, in C++, you can have an abstract base class that defines virtual functions that are overridden by the derived classes. You have something like:
The C++ compiler and linker handle all the chores of setting up the vtable and dispatching function calls to the correct function definition. The system provides a built in layer of abstraction between the idea of the object and its implimentation. It occured to me that a proxy class might provide a similar abstraction for Perl. Continuing the previous posters use of dogs:
The Dog::Proxy class holds all the information about the Dog hierarchy and all construction of objects deriving from Dog::Base take place here. The proxy has no methods or attributes and passes back a reference to the new Dog::Breed. I also disallowed the creation of a Dog::Base, but I suppose that in practice it might be preferable to be able to create a base class object as well.
Dog::Base currently just initializes some attributes that supposedly will be common to all Dogs.
And the test code:
With the results:
A somewhat simple if not simplistic hierarchy. The proxy provides a benifit in that it abstracts out the concepts implicit in constructing an instance of a more abstract class, in this case a type of dog. In my own development I have been attempting to go from the specific to the general in this way; by abstracting out the common elements of each type of thing, and by trying to notice what patterns tend to be used by each type. That is, what is the same about each type and what is different.
Back to
Meditations
|
|