views:

346

answers:

4

I have a issue where I'm working with a particular interface for quite a lot of things. However, I have a particular method that I want to be available only to a particular group of classes (basically, an internal method).

interface IThing {
    function thisMethodIsPublic():void;
    function thisMethodShouldOnlyBeVisibleToCertainClasses():void;
}

The problem is, there is no way to add access modifiers (i.e. public, private, internal) in an interface - at least not in ActionScript 3.0.

So I'm wondering what would be the best practice here? It seems like bad form to make this internal method public, but I need it to be a part of the interface so I can guarantee that classes that implement it have this internal method.

Thanks for your help!

+1  A: 

One possible idea I just thought of would be to define a second interface for the internal method:

interface IThing {
    function thisMethodIsPublic():void;
}

interface IInternalThing {
    function thisMethodShouldOnlyBeVisibleToCertainClasses():void;
}

That way the method would not be visible without type casting when working with IThing, but would be when you explicitly type cast as IInternalThing. This doesn't really solve the problem but it makes that method a little more hidden.

Mims H. Wright
+9  A: 

Answer: Define two interfaces, and keep the 'private' functions in the second interface. If ActionScript supports inheritance for interfaces, then define the 'private' interface as extending the 'public' interface.

Craig Trader
+1 This is the correct way. And yes, actionscript does support inheritance of interfaces.
Amarghosh
A: 

Maybe use namespaces instead of (or in addition) to your interface. Just a thought.

Robert Reinhardt
That was my original approach but I realized that I would have to reference the class with the custom namespace instead of the interface. Good suggestion though.
Mims H. Wright
A: 

Sorry to keep answering my own question but... Another possibility, kind of crazy, would be to create a sort of lock and key for the method I want to make private similar to the SingletonEnforcer often used in AS3.

// IThing.as
interface IThing {
    function thisMethodIsPublic():void;
    function thisMethodShouldOnlyBeVisibleToCertainClasses(key:InternalKey):void;
}

// InternalKey.as
internal class InternalKey {}

This seems like overkill to me but I tested it and it works! Classes external to the InternalKey can see the method but cannot call it. It also guarantees that the interface makes use of the method in question instead of having to typecast to the second interface.

It is also possible to make the second interface be an internal interface instead of using the Internal key.

internal interface IInternalThing extends IThing { 
    function thisMethodShouldOnlyBeVisibleToCertainClasses():void;
}

Actually, both of these are not exactly what I need because I wanted to make the method exposed to a nested package within the package where IThing would be, so basically I want to use custom namespaces here but I can't. I'm probably going to have to move the classes in the nested package back into the same pacakge as IThing.

Mims H. Wright