在我的 GWT 应用程序中,我有一个像这样的类:
public class AppActivityMapper implements ActivityMapper {
@Override public Activity getActivity(Place place) {
if(place instanceof ThisPlace) {
return new ThisActivity((ThisPlace)place);
}
if(place instanceof ThatPlace) {
return new ThatActivity((ThatPlace)place);
}
if(place instanceof AnotherPlace) {
return new AnotherActivity((AnotherPlace)place);
}
// you get the idea
}
}
ActivityMapper、Activity 和 Place 对象是框架的一部分,界面暗示这就是它的用途。
但是,根据Liskov 替换原则,接受类型但对子类进行类型检查以推断要采取什么行动的方法违反了该原则。
GWT 的 ActivityMapper 接口本质上是否鼓励违反 LSP?或者是否有另一种符合 LSP 的方法来编码我没有想到的方法?