简体   繁体   中英

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. We know how we should implement it in Java, but are not sure if an approach like this is recommended in Swift. Of course in Swift we would have to use protocols.

Can any of you give an advise on how we can properly achieve below Java architecture with Swift?

Thanks for you're thoughts in advance!

Java example:

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 {

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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