简体   繁体   English

如何使用Swift(iOS)执行以下架构。 添加了Java代码示例

[英]How to do the following architecture with Swift (iOS). Added a Java code example

For an iOS application a friend of my thought of an architecture for working with a variable set of data existing of 3 layers. 对于一个iOS应用程序,我想到一个朋友,该架构可用于处理3层数据集。 We know how we should implement it in Java, but are not sure if an approach like this is recommended in Swift. 我们知道如何在Java中实现它,但是不确定在Swift中是否建议使用这种方法。 Of course in Swift we would have to use protocols. 当然,在Swift中,我们必须使用协议。

Can any of you give an advise on how we can properly achieve below Java architecture with Swift? 你们中的任何人都可以就如何使用Swift在Java架构下正确实现提供建议吗?

Thanks for you're thoughts in advance! 谢谢您提前考虑!

Java example: Java示例:

An generic interface that determines the used methods: 确定所使用方法的通用接口:

public interface AbstractDataMapper<T> {
  public void create(T);
  public T read(string);
  public void update(T);
  public void delete(T);
}

Some abstract mapper classes implementing the interface and passing the right object: 一些实现接口并传递正确对象的抽象映射器类:

public abstract class AbstractClientMapper implements AbstractDataMapper<Client>{}
public abstract class AbstractProductMapper implements AbstractDataMapper<Product>{}
public abstract class AbstractOrderMapper implements AbstractDataMapper<Order>{}

Some classes that extend the mappers with custom implementation of the interface methods. 一些类通过接口方法的自定义实现扩展了映射器。

public class MyClientMapper extends AbstractClientMapper {
  //override methods from interface
}

public class MyProductMapper extends AbstractProductMapper {
  //override methods from interface
}

public class MyOrderMapper extends AbstractOrderMapper {
  //override methods from interface
}

Define protocol: 定义协议:

public protocol AbstractDataMapper: class {

    typealias T

    func create(object: T)
    func read(string: String) -> T
    func update(object: T)
    func delete(object: T)
}

Conform classes to protocol : 使类符合协议

public class AbstractClientMapper: AbstractDataMapper {

    public typealias T = Client

    public func create(object: T) { }
    public func read(string: String) -> T { }
    public func update(object: T) { }
    public func delete(object: T) { }
}

public class AbstractProductMapper: AbstractDataMapper {
    public typealias T = Product

    //implement the rest of methods
}

public class AbstractOrderMapper: AbstractDataMapper {
    public typealias T = Order

    //implement the rest of methods
}

Extend your classes: 扩展课程:

public class MyClientMapper: AbstractClientMapper {

}

public class MyProductMapper: AbstractProductMapper {

}

public class MyOrderMapper: AbstractOrderMapper {

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM