Search code examples
apache-flexactionscript-3frameworksencapsulationswiz

Swiz mandates weak encapsulation


I just started using Swiz, and, it seems like Swiz forces you to create classes with weak encapsulation. Swiz requires all event handlers to be public in order to mediate events.

Assume that component 'A' dispatches a few events, which I want to listen to in component 'B'. Traditionally, I'll just add event listeners on 'A' in 'B' and all the event handlers in 'B' can be kept private. However, if, I am using Swiz, I need to make all the handlers, mediating events, public.

Am I missing something here, is there a way to bypass this problem. I really, don't want to pollute the public interface of my class.

Thanks.


Solution

  • As I mentioned on the mailing list, there is no way around it, unfortunately. Since there is no way to access private members of classes, the only way B can use private event handlers for events from A is if addEventListener() is called from within B. Since Swiz is obviously not operating within your classes, it has no way to access those members.

    Swiz aims to keep your application code as free from references (including inheritance) to Swiz classes as possible. Therefore, you can think of it as configuring your app "from the outside". Unlike the JVM, Flash Player simply allows no access to private members, so for Swiz to interact with your code, it has to be public.