How to expose a method in an interface without mak

2019-02-23 21:38发布

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!

5条回答
别忘想泡老子
2楼-- · 2019-02-23 22:08

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.

查看更多
孤傲高冷的网名
3楼-- · 2019-02-23 22:13

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.

查看更多
闹够了就滚
4楼-- · 2019-02-23 22:27

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

查看更多
仙女界的扛把子
5楼-- · 2019-02-23 22:29

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.

查看更多
Luminary・发光体
6楼-- · 2019-02-23 22:30

Today I've encountered same problem. Fortunately I had opportunity to make a superclass instead of interface. Instead of using this:

internal interface IPhysicalObject extends IIDObject {

    function get shape():IShape;

}

I wrote this:

public class PhysicalObject extends IDObject {

    public function PhysicalObject():void {

        ...

    }

    internal function get shape():IShape { ... }

}

Of course it's only possible if classes that implement IPhysicalObject interface do not extend any other class. I think that is another possible solution.

查看更多
登录 后发表回答