假设我有一个适用于 Rectangles 的程序(这是我的实际问题的简化示例),我将其建模为
struct Rectangle {
let left:Int
let right:Int
let top:Int
let bottom:Int
func rationalized() -> Rectangle {
return Rectangle(left: min(self.left, self, right), right: max(self.left, self.right), top: min(self.top, self.bottom)
}
}
一个合理化的矩形,基本上是一个具有正宽度和高度的矩形。对于许多操作(例如缩放、平移、联合、相交等),最好保证矩形是合理的。事实上,可以将合理化逻辑实习在一个init(...)
,这样你就永远无法创建一个不合理的逻辑。但是在某些情况下,您希望支持不合理的结构,至少是暂时的,也许是为了在拖放过程中进行编辑,例如
func replacing(left newValue:Int) -> Rectangle {
return Rectangle(left: newValue, right: self.right, top: self.top, bottom: self.bottom)
}
想要这样做会使将合理化逻辑放在 init() 中会适得其反。
但是另一种方法是在几乎所有地方(除了拖放站点)都在rationalized()
调用我的代码。我正在尝试确定是否可以以某种方式使用结构和类型来做到这一点。
如果我使用类,我可以有一个 Rectangle 超类和一个 RationalizedRectangle 子类,其中 RationalizedRectangle 覆盖 init 来完成工作。然后我通常可以使用 RationalizedRectangles(甚至在适当的时候将其指定为类型),但允许通过使用 Rectangle 来编辑它们,并在最后转换为 RationalizedRectangle。
但是 Swift 结构不支持继承。所以我不知道如何惯用地实现这一点。我可以在结构中添加一个 isRationalizing:Boolean,然后在其上进行分支,但这看起来很俗气。
是否有一个基于结构的习语可以在这里工作?