gpt4 book ai didi

Swift Struct 习语取代 OOP 类型继承模式?

转载 作者:可可西里 更新时间:2023-11-01 00:55:06 24 4
gpt4 key购买 nike

假设我有一个使用矩形的程序(这是我实际问题的简化示例),我将其建模为

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 父类(super class)和一个 RationalizedRectangle 子类,其中 RationalizedRectangle 覆盖 init 来完成工作。然后我通常可以使用 RationalizedRectangles(甚至在适当的时候将其指定为类型),但允许使用 Rectangle 编辑它们,并在最后转换为 RationalizedRectangle。

但是 Swift 结构不支持继承。所以我不知道如何惯用地完成这个。我可以在结构中添加一个 isRationalizing:Boolean,并在其上进行分支,但这看起来很俗气。

是否有一种基于结构的习语可以在这里工作?

最佳答案

您应该能够使用协议(protocol)来完成此操作。您可以将公共(public)逻辑移动到一个协议(protocol)中,然后创建两个符合该协议(protocol)但具有不同初始值设定项的类。然后,在您要引用特定对象类型的地方,您可以引用协议(protocol)。

protocol RectangleProtocol {
var left:Int {get}
var right:Int {get}
var top:Int {get}
var bottom:Int {get}
}

struct Rectangle: RectangleProtocol {
let left: Int
let right: Int
let top: Int
let bottom: Int

init(leftValue:Int, rightValue:Int, topValue:Int, bottomValue:Int) {
self.left = leftValue
self.right = rightValue
self.top = topValue
self.bottom = bottomValue
}
}

struct RationalRectangle: RectangleProtocol {
let left: Int
let right: Int
let top: Int
let bottom: Int

init(leftValue:Int, rightValue:Int, topValue:Int, bottomValue:Int) {
self.left = min(leftValue, rightValue)
self.right = max(leftValue, rightValue)
self.top = min(topValue, bottomValue)
self.bottom = max(topValue, bottomValue)
}
}

let rectangle: RectangleProtocol = Rectangle(leftValue: 4, rightValue 4, topValue: 8, bottomValue: 8)
let rationalRectangle: RectangleProtocol = RationalRectangle(leftValue: 4, rightValue:8, topValue: 7, bottomValue: 4)

// Now both of these represent a struct that conforms to the RectangleProtocol.

关于Swift Struct 习语取代 OOP 类型继承模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51735777/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com