|Keep It Simple, Stupid|
Re^5: Tutorial: Introduction to Object-Oriented Programmingby fruiture (Curate)
|on Dec 12, 2002 at 21:12 UTC||Need Help??|
Perhaps it helps if i use "attribute" instead of "member", so one can't confuse "member" with "instance"...
Abigail's proposal is to store attribute values of instances in lexical (or only package-global) hashes to make them safe from subclasses that might clobber attributes that are private to the superclass:
A subclass might also need a '_very_internal_flag' (stored as attribute of an instance) and suddenly it all breaks.
So the idea is to store the attribute data in kind-of "registry": a central repository that keeps thes attributes in it's defining class.
My first thought on that was to write a module that is a superclass and contains such a registry for all classes. It gives you two methods: get( attribute ) and set(attribute,value) (probably with diofferent names). These methods get/store these values in that central registry and determine the class, in which that actual attribute was defined and thus must be kept apart from other descending classes defining the same attribute. I thought of something like that because of my lazyness: it would save typing for many classes based on that principle.
Such a module is easy to write:
Usage as follows:
The above example shows how the subclass Rover defines an attribute that perhaps is ment to override the Car's color attribute but it won't: The attribute you access via get_set_color() is different from the attribute you access via color(), because they are accessing the data from different packages. The subclass can only override the superclass' attribute by using the superclass' method for that (or by using the optional last argument to oo_get,oo_set, which is the dirty/unofficial method).
This is great, but it's different from what you're used to when writing objects that store their attributes(aka members) in themselves.