Question about Interface usage

I have a class that implements two interfaces. ONe is a management interface and the other is a usage interface. Users should only know about the user interface. But I wanted to prevent the ability to cast to the management interface. Since the clas simplemented both there was no way to prevent the casting.

What I have done instead is to wrap the class inside another class. This outer class only implements the user interface but it has forwarding methods for the management interface as well. This prevents any users from casting to the management interface since this wrapper class does not implement it. This wrapper class is package scope so other classes outside the package have no way to cast this to access the management methods.

Is this sensible or have I gone to far?

interface IUser{

publicvoid userMethod1();

publicvoid userMethod2();

}

interface IManager{

privatevoid manageMethod1();

privatevoid manageMethod1();

}

class MainObjectimplements IUser, IManager{

...

}

privatestaticclass SafeAccessorimplements IUser{

private MainObject mo;

publicvoid userMethod1(){mo.userMethod1()};

publicvoid userMethod2(){mo.userMethod2()};

privatevoid manageMethod1(){mo.manageMethod1()};

privatevoid manageMethod1(){mo.manageMethod2()};

}

To the rest of the world I pass out a reference to SafeAccessor as an IUser. Whereas before I passed out a reference to MainObject as an IUser. MainObject can be cast to IManager but SafeAccessor can not.

[2834 byte] By [_dnoyeBa] at [2007-10-2 21:52:59]
# 1
It sounds perfectly reasonable to me...?
dcmintera at 2007-7-14 1:08:55 > top of Java-index,Other Topics,Patterns & OO Design...
# 2
Why not just make the Management interface package private? Then users would not know about it, but all of your classes that need access to it would.- Adam
guitar_man_Fa at 2007-7-14 1:08:55 > top of Java-index,Other Topics,Patterns & OO Design...
# 3
I don't think you can make interfaces package private. Either way, the interface is obtained from a remote location so it needs to be public to begin with. After its obtained though, no other classes need access to it.
_dnoyeBa at 2007-7-14 1:08:55 > top of Java-index,Other Topics,Patterns & OO Design...
# 4
I had a similar issue. I basically came up with the same conclusion. The implementation class for general usage should be a separate class even though it shares methods with the more powerful type.
dubwaia at 2007-7-14 1:08:55 > top of Java-index,Other Topics,Patterns & OO Design...
# 5
> Why not just make the Management interface package> private? Then users would not know about it, but all> of your classes that need access to it would.You can make the interface package-protected but interface methods are always public.
dubwaia at 2007-7-14 1:08:55 > top of Java-index,Other Topics,Patterns & OO Design...
# 6

yeah, so what's the problem there? If the interface is package private, it doesn't matter that the methods are public, if the only way to reference it is with the User interface. Say you had something like this:

public class UserFactory {

public static User getUser( theParameters ) {

return new MyUserAndManager( theParameter );

}

}

public interface User {

public void userMethod();

}

interface Manager {

public void managerMethod();

}

class MyUserAndManager implements User, Manager {

public void userMethod() { }

public void managerMethod() { }

}

in this situation, you can't see the interface Manager, nor the class MyUserAndManager, but you can still interact with MyUserAndManager through the User interface.

I can't see why this wouldn't work, as long as you could restrict all objects interacting with the Manager interface to the same package.

If not, the other method here (utilizing what is basically a proxy?) ought to work okay too.

- Adam

guitar_man_Fa at 2007-7-14 1:08:55 > top of Java-index,Other Topics,Patterns & OO Design...
# 7

> I can't see why this wouldn't work, as long as you

> could restrict all objects interacting with the

> Manager interface to the same package.

No it should work but it's a little icky.

> If not, the other method here (utilizing what is

> basically a proxy?) ought to work okay too.

i think that someone could access the methods through reflection but that's kind of paranoid anyway. In this case both of the interfaces have to be public so it's sort of a moot point.

dubwaia at 2007-7-14 1:08:55 > top of Java-index,Other Topics,Patterns & OO Design...
# 8

>

> Is this sensible or have I gone to far?

If I have to write my code to protect it from the users then I consider it a management problem and not a software one.

You could always add a security manager. That would probably prevent access via reflection but probably not via JNI.

jschella at 2007-7-14 1:08:55 > top of Java-index,Other Topics,Patterns & OO Design...
# 9
> i think that someone could access the methods through> reflection but that's kind of paranoid anyway.I was actually just thinking that too.
guitar_man_Fa at 2007-7-14 1:08:55 > top of Java-index,Other Topics,Patterns & OO Design...
# 10

When I say 'protect' I don't mean in a security sense. This is not a security issue. Just a usage issue. And I consider myself that user who will end up casting just to do something lazy :)

I can't use package private interface because the object that is implementing the interface comes from another package. It comes from a seperate plugin and even classloader. The interfaces are defined in another place that the manager and the implementor can access freely.

_dnoyeBa at 2007-7-14 1:08:55 > top of Java-index,Other Topics,Patterns & OO Design...