PDA

View Full Version : calling the document class from other object instances



rm_lism
September 19th, 2008, 12:55 AM
I have a Flash CS3 fla with a document class specified. Lots of other classes exist, too, and they're directly or indirectly spawned from the document class. I can pass "this" as a parameter from the document class to instances I create so that those child objects have a reference to the document class. But it seems to me that I shouldn't need to do that. That all children must "know" what the document class is.

Is that true? If it is, how to I talk to the document class from other object instances in my project? In other words, what's the syntax to reference a method in the document class?

sekasi
September 19th, 2008, 12:59 AM
You don't.

You can, but you're not really supposed to. That goes against one of the fundamental ideas of OOP called 'encapsulation'.

Children should never be allowed to access their parents directly. Classes should never be aware of what's "around" them.

One way to do this properly is to dispatch an event in your class, and have your document class listen to the event. When it's picked up, you trigger whatever function it is you want to trigger.

rm_lism
September 19th, 2008, 01:04 AM
Thanks for the quick reply. That makes sense. I just thought that in this case, since the document class is sort of tied to the main timeline, that it might be accessible in atypical ways. But dispatching an event would work fine.

Does it make sense to pass a reference to the document class to child classes that I figure might need to talk back to the document class? Obviously, it's doable, but is that "appropriate?"

I'm a pretty good coder, but my sense of architecture could stand to be a bit better than it is.

sekasi
September 19th, 2008, 02:04 AM
That's a bit more appropriate, but you're relying on super tight coupling if you do that, which is really bad for scalability and maintainability. Dispatching events is a neutral and pretty correct way of doing it ;)

rm_lism
September 19th, 2008, 11:49 AM
Thanks.

This project will have a fair amount of messaging going from child object instances to either the Main class or another class that'll need to control some big picture things related to the goings on in child instances. That could mean having a lot of methods in the Main class to handle all this messaging. Would it be better to have fewer such methods and pass different events depending on what's going on so that one or a few methods could handle and parse out the messaging?

I have a decent theoretical understanding of OOP, but the practical transference of that knowledge isn't always very strong.

rm_lism
September 19th, 2008, 02:38 PM
A follow up - subclasses of the document class will need to reference the main timeline via the document class to execute the addChild() method. They'll need a reference to the document class to do that. But how can they subscribe to events broadcast by the document class if they don't have a reference to it? In other words:

docClass.addEventListener(MyEvent.EVENT, doSomething);

Thoughts?

rm_lism
September 25th, 2008, 12:30 AM
Sorry to bump this...

If anyone could help on this, I'd appreciate it. So I want to find ways to message with the document class without having to pass a reference from it to the many subclasses. But since the subclasses don't know who the document class is, how do I get a message from the subclass to the document class? And vice versa, for that matter.

I can't do:

documentClass.addEventListener("msg", method);

from a subclass because the subclass doesn't know what the document class is. I've tried various things, but it doesn't seem to work.

Same thing in the other direction. How do I broadcast something from the document class to all potential listeners if they can't register with the document class?

I expect that all this can be done via some approach that I'm almost already doing. I just don't know how, yet.