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

时间:2015-11-09 09:30:03

标签: java ios swift protocols

对于iOS应用程序,我想到了一个架构,用于处理存在3层的可变数据集。我们知道如何在Java中实现它,但不确定在Swift中是否建议使用这样的方法。当然在Swift中我们必须使用协议。

你们是否可以就如何使用Swift正确实现Java体系结构提供建议?

先谢谢你的想法!

Java示例:

确定使用方法的通用接口:

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

一些实现接口并传递正确对象的抽象映射器类:

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

通过接口方法的自定义实现扩展映射器的一些类。

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
}

1 个答案:

答案 0 :(得分:1)

定义协议:

public protocol AbstractDataMapper: class {

    typealias T

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

使类符合协议

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
}

扩展您的课程:

public class MyClientMapper: AbstractClientMapper {

}

public class MyProductMapper: AbstractProductMapper {

}

public class MyOrderMapper: AbstractOrderMapper {

}