- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在分析图形基元(矩形、直线、圆形等)的交互并计算重叠、相对方向、合并等。这被引用为双分派(dispatch)的主要示例(例如 Wikipedia )
Adaptive collision algorithms usually require that collisions between different objects be handled in different ways. A typical example is in a game environment where the collision between a spaceship and an asteroid is computed differently than the collision between a spaceship and a spacestation.1
但我还没有理解主要的解释,而且我也不大体上理解 SO 上的答案。
我当前的代码 (Java) 使用父类(super class) Shape,类似于:
for (int i = 0; i < shapes.size() - 1; i++) {
for (int j = i + 1; j < shapes.size(); j++) {
Shape shape = shapes.get(i).intersectionWith(shapes.get(j));
}
}
在子类(此处为 Rect)中具有特定实现,例如
public class Rect extends Shape {
public Shape intersectionWith(Shape shape) {
if (shape instanceof Rect) {
return this.getCommonBoundingBox((Rect)shape);
} else if (shape instanceof Line) {
return this.intersection((Line)shape);
} else if (shape instanceof Text) {
return this.intersection((Text) shape);
}
}
}
无论如何,我必须编写所有的 n*(n-1)/2
方法(并且已经这样做了)。我还必须有可扩展的代码来适应(比如)以后的日期:
} else if (shape instanceof Circle) {
return this.intersection((Circle)shape);
我不知道如何使用双分派(dispatch)模式,也不知道它的值(value),希望有一个使用 Java 图形基元或类似伪代码的具体示例。
更新:我已经接受了@Flavio,因为(我认为)它回答了所问的确切问题。但是我实际上已经实现了@Slanec,因为它解决了我的问题并且(对我而言)更简单易读。我有一个附属问题“解决方案是否取决于对称关系?”。
“A 与 B 相交”通常等同于“B 与 A 相交”,但“A 与 B 碰撞”并不总是与“B 与 A 碰撞”相同。 (A == 汽车,B == 骑自行车的人)。可以想象,我的交叉点在未来可能不是对称的(例如,“Rect partially obscures Circle”不是对称的,可能有不同的语义。
@Flavio 很好地解决了维护问题,并指出编译器可以检查问题。 @Slanec 通过反射来做到这一点,它看起来像是一种有用的维护辅助工具——我不知道性能受到什么影响。
最佳答案
您可以通过 Visitor
模式在 Java 中实现双重分派(dispatch)。
public interface ShapeVisitor<P, R> {
R visitRect(Rect rect, P param);
R visitLine(Line line, P param);
R visitText(Text text, P param);
}
public interface Shape {
<P, R> R accept(P param, ShapeVisitor<? super P, ? extends R> visitor);
Shape intersectionWith(Shape shape);
}
public class Rect implements Shape {
public <P, R> R accept(P param, ShapeVisitor<? super P, ? extends R> visitor) {
return visitor.visitRect(this, param);
}
public Shape intersectionWith(Shape shape) {
return shape.accept(this, RectIntersection);
}
public static ShapeVisitor<Rect, Shape> RectIntersection = new ShapeVisitor<Rect, Shape>() {
public Shape visitRect(Rect otherShape, Rect thisShape) {
// TODO...
}
public Shape visitLine(Line otherShape, Rect thisShape) {
// TODO...
}
public Shape visitText(Text otherShape, Rect thisShape) {
// TODO...
}
};
}
当您添加一个新的 Shape
子类时,您必须向 ShapeVisitor
接口(interface)添加一个新方法,并且您会因缺少的所有方法而出现编译错误。这很有用,但如果您正在编写一个库并且允许您的用户添加 Shape
子类(但显然不能扩展 ShapeVisitor
接口(interface)),这可能会成为一个大问题。
关于java - 如何使用双重分派(dispatch)来分析图元的交集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19400582/
在使用 dispatch 更新 useReducer 状态后,我需要能够立即执行操作。但是 dispatch 运行异步所以当我运行我的下一段代码时,它在 dispatch 应该更新它之前使用旧状态。
我是 Redux 新手。我必须调度一个操作来更新应用程序的状态,然后使用更新状态来调用我的后端。我使用 thunkMiddleware。 const mapDispatchToProps = disp
我是 Redux 新手。我必须调度一个操作来更新应用程序的状态,然后使用更新状态来调用我的后端。我使用 thunkMiddleware。 const mapDispatchToProps = disp
当我单击 InspectorOption 组件之一时,我的 redux 记录器显示已分派(dispatch)操作并且状态按预期更新。 我的 InspectorSelect 和子 InspectorOp
我有一个模块,其中包含一组函数,实现为带有辅助函数的分派(dispatch)哈希: my $functions = { 'f1' => sub { my %args = @_;
Apple 的 GCD 文档说明如下: GCD provides and manages FIFO queues to which your application can submit tasks
所以我正在获取此 UIImage 数据并将其转换为 base64 中的字符串。问题是它在转换时卡在 UI 线程上,我不确定为什么。 - (void)processImage:(UIImage*)ima
我有一个从后台线程调用的函数 func getValue() -> Bool。这是有意的,也是必须的。现在,getValue() 需要在主线程上执行一些操作,在这种情况下它需要访问 UIApplica
我有一个带有表单的组件,可以将项目添加到列表中。成功将项目添加到列表后,我想使用 form.resetForm(); ,但我想不出一个简单的方法来知道该操作何时成功。我希望我可以订阅 Action 调
我正在努力在 Laravel 6.x 中使用 cursor() 方法获取 3M+ 记录时降低内存。 我有一个 artisan 命令运行以下代码: Product::cursor()->each(fun
好的,所以我一直在尝试通过并发编辑设置电子表格应用程序。我走了laravel回声,redis,套接字路由。 (任何有关仅使用推动器的建议都会被驳回)。现在大多数情况下,我已经开始工作了,我可以从修补匠
假设我有这个布局 span 我将无处不在的点击转换为自定义事件,并使用委托(delegate)的非捕获处理程序将其分派(dispatch)到其原始目标: document.ad
页面加载后,我将在我的 index.js 中调度一个操作 store.dispatch(getWeatherReports()); 来访问天气 API。此操作通过 redux 过程,最终将返回的数据添
我有一个抽象父类 Parent 和六个子类 ChildA though ChildF。 另一个类 Other 有六个(静态)重载方法 olmeth(),六个子类中的每一个。 我怎么写: Parent
这里描述了类似的问题:GWT IllegalArgumentException: encodedRequest cannot be empty 我的GWT应用程序部署在Tomcat6中,该Tomcat
我正在尝试通过 iOS 上的 GCD 将一些代码分派(dispatch)到主队列,但即使是最简单的测试也总是失败。最后归结为: static const int TICK_INTERVAL = 1;
在某些情况下,覆盖扩展中的方法签名似乎会产生不可预知的结果。以下示例演示了具有相似模式的两个不同结果。 class A: UIViewController { func doThing() {
这个问题在这里已经有了答案: Is self retained within this Objective-C block? (1 个回答) 8年前关闭。 假设我有一个简单的电话 dispatch_a
React 中的上下文和 reducers 非常新。我目前正在尝试使用 Context 从折线图上的事件中获取日期字符串。我使用的折线图来自 react-chartjs-2。 我的上下文已设置并提供如
我有一个项目,其中nodejs服务器通过socket.io将推送事件传递到react仪表板,我正在使用Redux。当收到新数据时,会触发一个操作来更新所有相关组件,尽管我不确定我这样做的方式是否正确。
我是一名优秀的程序员,十分优秀!