How to expose a method in an interface without making it public for all classes

I have a problem when I work with a specific interface for quite a few things. However, I have a specific method that I want to get only for a specific group of classes (basically, the internal method).

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

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

So I wonder what would be the best practice here? The poor form seems to make this internal method publicly available, but I need it to be part of the interface, so I can guarantee that the classes that implement it have this internal method.

Thanks for your help!

+4
source share
5 answers

Answer. Define two interfaces and save the 'private' functions in the second interface. If ActionScript supports inheritance for interfaces, define the 'private' interface as an extension to the "open" interface.

+12
source

One of the possible ideas that I was just thinking about is to define a second interface for the internal method:

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

Thus, the method will not be visible without applying a tick when working with IThing , but it will be when you explicitly enter cast as IInternalThing . This does not really solve the problem, but makes this method a bit more hidden.

+1
source

You can use namespaces instead (or in addition) to your interface. Just a thought.

0
source

Sorry to continue to answer my own question, but ... Another possibility, like a crazy one, would be to create some kind of lock and key for the method that I want to make private, similar to SingletonEnforcer , which is often used in AS3.

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

This seems redundant to me, but I tested it and it works! Classes external to InternalKey can see this method, but cannot call it. It also ensures that the interface uses the method in question instead of resorting to casting to the second interface.

It is also possible that the second interface is an internal interface instead of using an internal key.

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

In fact, both of them are not quite what I need, because I wanted to make the method open for the nested package in the package where IThing will be, so basically I want to use custom namespaces here, but I can’t, Me, you may have to move the classes in the nested package back to the same pacakge as IThing .

0
source

Today I faced the same problem. Fortunately, I had the opportunity to make a superclass instead of an interface. Instead of 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, this is only possible if classes that implement the IPhysicalObject interface do not extend any other class. I think this is another possible solution.

0
source

All Articles