【发布时间】:2017-05-27 03:27:16
【问题描述】:
我目前正在开发一个基本上有两种变体的 Android SDK,基本变体(供第三方开发人员使用)和特权变体(供内部使用)。
特权 SDK 只是添加了第三方开发人员无法直接访问的附加功能。
我的想法是使用宏来选择性地删除功能,但 Java 不支持。
我的下一个想法是采用基本变体并在其中扩展类和接口以产生特权变体。
我当前的问题如下使用继承方法(这产生了一种代码味道,向我表明可能有更好的解决方案):
BaseAPI 的一个实例有一个BaseInterface 的实例,在它的某些方法中使用BaseDevice 作为参数。
特权 SDK 有一个 PrivilegedAPI、PrivilegedInterface 和 PrivilegedDevice 的实例。
问题在于希望接口采用BaseDevice 或PrivilegedDevice 的任一实例。
我最喜欢这个 BaseInterface:
public interface BaseInterface {
void deviceConnected(BaseDevice device);
}
还有这个 PrivilegedInterface:
public interface PrivilegedInterface extends BaseInterface {
//overwrites deviceConnected in BaseInterface with PrivilegedDevice
@Override
void deviceConnected(PrivilegedDevice device);
}
但我不能在PrivilegedInterface 中用PrivilegedDevice 的不同参数覆盖deviceConnected。
我的另一个想法是利用构建风格来隐藏功能,但这似乎也不适合。
有什么想法吗?
【问题讨论】:
标签: java android inheritance