Adding methods to objects on the fly.
in
Programming Questions
•
2 years ago
Hi all,
I have no idea, if what I'm thinking should be possible, actually IS possible, but let me try to explain:
I want to add new methods (behaviors) to initially "dumb" objects. In my sketch, all objects can have several, very different behaviors, but only one of them at a time. So up until now, I'd just create one kind of object "preloaded" with all methods it could eventually use. And then the object would await certain conditions, switch to the method it's told to use and go with it from there.
But... since I'm expecting my final behaviors/methods to become quite complex at some point, I'm wondering if there is another approach to what I'm planning to do. As mentioned above, I would create initially dumb objects, which don't have any behavior/methods and are completely oblivious of any behaviors they might receive. Then, when needed, a "controlling function" will select one of the objects and add one specific method to it. This way, every object only uses one method at a time and isn't cluttered with the other heavy methods it's not using.
Obviously, the script won't be able to write the method as new lines of code into the object. So I'm thinking there might be some way to tell an object to look for it's behavior with an other object. Some kind of "cross-referencing" of two objects. Sorry if this sounds abstract, but I really have no idea IF this is possible (or if it actually makes sense to try to do it in the first place). And I have no idea what this kind of technique might be called (if it even exists).
Again, I have no clue if my thinking is completely off track. But I'd really appreciate any kind of input on where I could start looking more into this.
Thanks!
cheers,
Greg
Greg
1