我的应用程序是用于连接“模块”(通过模块端口)的编辑器。端口有端口类型。每个端口类型都有其关联的比较器。如果它们的属性满足比较器中实现的规则,则两种类型是兼容的。
我希望用户通过实现新的端口类型和如何连接它们的规则来扩展应用程序(通过 eclipse 扩展点基础设施)。
这意味着在运行时我只能与端口类型接口进行交互。没有具体的类是已知的。所有具体的实现都由工厂返回。
我实现了这两个接口:
public interface IPortType {
[many attributes]
}
public interface IComparator {
public boolean isCompatible(IPortType source, IPortType target);
// more interaction methods!
}
我目前的解决方案有点难看,因为通用 isCompatible(IPortType source, IPortType target) 方法是一种委托,必须在所有子类中重写。简单地重载 isCompatible() 方法在这里不起作用。
但更大的缺点是违反了开闭原则:当应该支持一种新类型时,必须扩展所有具体的 Comparator 类。但是,当类型之间有更多的交互(如转换等)时,如何保持规则类的数量较少?我的意图是在一类中保留一种类型的所有规则。
一个具体的比较器示例:
public class ATypeComparator implements IComparator {
public boolean isCompatible(IPortType source, IPortType target) {
if (!(source instanceof AType))
return false;
if (target instanceof BType)
return isCompatible(source, (BType) target);
if (target instanceof CType)
return isCompatible(source, (CType) target);
}
public boolean isCompatible(AType source, BType target) {...}
public boolean isCompatible(AType source, CType target) {...}
}
你将如何解决这个问题?
感谢您的任何建议!