实现某个接口并具有特定超类的参数:泛型还是instanceof?

发布于 2024-09-07 03:36:08 字数 1463 浏览 3 评论 0原文

我希望获得有关特定设计的帮助。这是我希望工作的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无尽的现实 2024-09-14 03:36:08

恕我直言,使用泛型是优越的,因为这样编译器可以为您提供一些静态类型检查。在第二种情况下,您会在运行时发现错误。

如果不查看其余代码,就很难找到替代设计。即可以绘制任何 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)?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文