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.

