带有类型化的Swift协议。协议只能用作一般约束

带有类型化的Swift协议。协议只能用作一般约束,swift,protocols,factory,swift-protocols,Swift,Protocols,Factory,Swift Protocols,我在考虑我的应用程序中的验证检查,我认为在实现Validee的任何模型上调用ValidatorFactory,这意味着说哪个类负责创建ValidatorFactory听起来很不错。但下面的代码不起作用:( 代码: 但是,即使我忘记了验证器工厂并尝试运行以下代码,它也无法工作: client.ValidatorFactoryClass.create(fromModel: client) 为什么?如果您遇到的问题是由于编译错误造成的,则需要修改您的验证器工厂结构 您不能直接将协议用作类型。您可以将

我在考虑我的应用程序中的验证检查,我认为在实现Validee的任何模型上调用ValidatorFactory,这意味着说哪个类负责创建ValidatorFactory听起来很不错。但下面的代码不起作用:(

代码:

但是,即使我忘记了验证器工厂并尝试运行以下代码,它也无法工作:

client.ValidatorFactoryClass.create(fromModel: client)

为什么?

如果您遇到的问题是由于编译错误造成的,则需要修改您的
验证器工厂
结构

您不能直接将协议用作类型。您可以将其用作类型的约束。这意味着与其将
Validee
作为
ValidatorFactory.createValidator的参数类型,不如将其用作该类型的约束。此更改将为您提供:

struct ValidatorFactory<T:Validee> {
    static func createValidator(fromModel model: T) -> Validator {
        return model.ValidatorFactoryClass.create(fromModel: model)
    }
}
protocol Validee {
    static func create(fromModel model: Self) -> Validator
}

protocol Validator {
    func validate() throws -> Void
}

struct ValidatorFactory<T:Validee> {
    static func createValidator(fromModel model: T) -> Validator {
        return T.create(fromModel: model)
    }
}

struct Client: Validee {
    static func create(fromModel model: Client) -> Validator {
        return ClientDeliveryAddressValidator(withModel: model)
    }
}

struct ClientDeliveryAddressValidator: Validator {
    typealias Model = Client
    let client: Client

    init(withModel client: Client) {
        self.client = client
    }

    func validate() throws {

    }
}

编辑:

鉴于
Validee
已经了解工厂,因此更简单的做法是更改设计,以便
Validee
知道如何直接创建验证器

这将给你:

struct ValidatorFactory<T:Validee> {
    static func createValidator(fromModel model: T) -> Validator {
        return model.ValidatorFactoryClass.create(fromModel: model)
    }
}
protocol Validee {
    static func create(fromModel model: Self) -> Validator
}

protocol Validator {
    func validate() throws -> Void
}

struct ValidatorFactory<T:Validee> {
    static func createValidator(fromModel model: T) -> Validator {
        return T.create(fromModel: model)
    }
}

struct Client: Validee {
    static func create(fromModel model: Client) -> Validator {
        return ClientDeliveryAddressValidator(withModel: model)
    }
}

struct ClientDeliveryAddressValidator: Validator {
    typealias Model = Client
    let client: Client

    init(withModel client: Client) {
        self.client = client
    }

    func validate() throws {

    }
}
协议有效性{
静态函数创建(fromModel:Self)->验证器
}
协议验证器{
func validate()抛出->无效
}
结构验证器工厂{
静态func createValidator(fromModel模型:T)->Validator{
返回T.create(fromModel:model)
}
}
结构客户端:Validee{
静态func create(fromModel:Client)->Validator{
return ClientDeliveryAddressValidator(带型号:型号)
}
}
结构ClientDeliveryAddressValidator:验证程序{
typealias模型=客户端
让客户:客户
初始化(withModel客户端:客户端){
self.client=client
}
func validate()抛出{
}
}

它以什么方式“不起作用”?@GaryMakin它只是不编译,但现在客户端也是一个工厂。我希望它是一个单独的组件,或者我们可以将它放在一个扩展中。一个扩展是有意义的。你可以在别处定义你的
客户端
类,然后将
结构客户端:Validee
更改为
扩展客户端:Validee
。这将移动所有从真实类中进行验证。然后,将其作为一个静态方法,没有多大意义,或者是真的吗?我只是想将Validator对象的创建放在另一个类中,这样每个组件都能完成它的任务,因为从那时起,客户端也是一个ValidatorFactory(它可以创建不同类的实例,根据需要和情况从客户端对象创建验证器对象。我想使用扩展更“快捷”,但对我来说,这有点不寻常