实现某个接口并具有特定超类的参数:泛型还是instanceof?
我希望获得有关特定设计的帮助。这是我希望工作的代码:
abstract class Square {...}
abstract class Circle {...}
interface JPGExportable {...}
class SquareJPG extends Square implements JPGExportable {...}
class CircleJPG extends Circle implements JPGExportable {...}
interface Interface {
draw(Square square);
draw(Circle circle);
}
class Canvas implements Interface {
draw(SquareJPG squarejpg) {...}
draw(CircleJPG circlejpg) {...}
}
换句话说,Canvas 应该实现 Interface 规范,但是绘制方法应该仅处理实现 JPGExportable 的 Square 和 Circle 子类。
据我所知,有两种可行的解决方案,但我认为这两种解决方案都不是很漂亮:
/*
* Using generics
*/
interface Interface<S extends Square, C extends Circle> {
draw(S square);
draw(C circle);
}
class Canvas implements Interface<SquareJPG, CircleJPG> {
draw(SquareJPG squarejpg) {...}
draw(CircleJPG circlejpg) {...}
}
/*
* Using instanceof
*/
interface Interface {
draw(S square);
draw(C circle);
}
class Canvas implements Interface {
draw(Square square) {
if (square instanceof SquareJPG)
// do stuff now
}
draw(Circle circle) {
if (circle instanceof CircleJPG)
// do stuff now
}
}
实际上 Square 和 Circle 相当不同,为什么公共超类无法包含任何公共代码。另外,实现 JPGExportable 的超类会让人感觉……错误;这确实是一个子功能。
我不喜欢泛型方式的根本原因是我需要处理 7 种不同的类型。也许我很挑剔,但连续 7 次出现“T extends Type”看起来很难看。
- 有没有看起来更好的第三种解决方案?
- 如果不是,两者中哪一个“更好”?
I would love some help on a particular design. This is the code I wish worked:
abstract class Square {...}
abstract class Circle {...}
interface JPGExportable {...}
class SquareJPG extends Square implements JPGExportable {...}
class CircleJPG extends Circle implements JPGExportable {...}
interface Interface {
draw(Square square);
draw(Circle circle);
}
class Canvas implements Interface {
draw(SquareJPG squarejpg) {...}
draw(CircleJPG circlejpg) {...}
}
In words, Canvas should implement the specification that is Interface, BUT the draw-methods should only handle subclasses of Square and Circle that implement JPGExportable.
To my knowledge there are two solutions that work, but neither of which I think is very pretty:
/*
* Using generics
*/
interface Interface<S extends Square, C extends Circle> {
draw(S square);
draw(C circle);
}
class Canvas implements Interface<SquareJPG, CircleJPG> {
draw(SquareJPG squarejpg) {...}
draw(CircleJPG circlejpg) {...}
}
/*
* Using instanceof
*/
interface Interface {
draw(S square);
draw(C circle);
}
class Canvas implements Interface {
draw(Square square) {
if (square instanceof SquareJPG)
// do stuff now
}
draw(Circle circle) {
if (circle instanceof CircleJPG)
// do stuff now
}
}
In reality Square and Circle are fairly distinct, why a common super-class wouldn't be able to contain any common code. Also, a super-class implementing JPGExportable would feel... wrong; it's really a sub-feature.
An underlying reason why I don't like the generics-way is that I need to handle 7 different types. Perhaps I'm being picky, but 7 occurences of "T extends Type" in a row looks ugly.
- Is there a third solution which looks better?
- If not, which of the two "is better"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
恕我直言,使用泛型是优越的,因为这样编译器可以为您提供一些静态类型检查。在第二种情况下,您会在运行时发现错误。
如果不查看其余代码,就很难找到替代设计。即可以绘制任何 JPGExportable 吗?也许您可以拥有一个只有一种方法绘制(JPGEXportable)的界面?
IMHO using generics is superior, since this way the compiler can give you some static type checking. In the second case you would find the bug during runtime.
It's difficult to find an alternative design without seeing the rest of your code. I.e. is it possible to draw any JPGExportable? Maybe you could have an interface with just one method draw(JPGEXportable)?