Theoretical Question Swift - Imtiaz211/interviews GitHub Wiki

Tight Coupling and Loose Coupling

Tight coupling is almost always inappropriate(अनुपयुक्त) for RESTful API design
Tight coupling is when a group of classes are highly dependent on one another.

This scenario arises when a class assumes too many responsibilities, or when one concern is spread over many classes rather than 
having its own class.

Loose coupling is achieved by means of a design that promotes single-responsibility and separation of concerns.

A loosely-coupled class can be consumed and tested independently of other (concrete) classes.
Interfaces are a powerful tool to use for decoupling. Classes can communicate through interfaces rather than other concrete classes, 
and any class can be on the other end of that communication simply by implementing the interface.

Example of tight coupling:

final class APIService {

func exampleService(completion: @escaping(([Entity]) -> Void)) {

// Network calling
//...
let data = Entity(id: "123", title: "DabaDoo")
completion([data])

}

}
final class Interactor {
  
  private let coupledService = APIService()
  
  func callService() {
      coupledService.exampleService { [weak self] entity in
          guard let self = self else {return}
              // Do staff with data
      }
  }
}

Loose Coupling

protocol APIServiceInterface{
    func exampleService(completion: @escaping(([Entity]) -> Void))
}

final class APIService: APIServiceInterface {

func exampleService(completion: @escaping(([Entity]) -> Void)) {

// Network calling
//...
let data = Entity(id: "123", title: "DabaDoo")
completion([data])

}

}

final class Interactor {
  
    private let decoupledService: APIServiceInterface
  
    init(decoupledService: APIServiceInterface) {
        self.decoupledService = decoupledService
    }
  func callService() {
      decoupledService.exampleService { [weak self] entity in
          guard let self = self else {return}
              // Do staff with data
      }
  }
}

How to do App Thinning? Show me the real stuff!

  • There are three ways to do App Thinning. All are independent and have specific purpose.

Slicing Bitcode On-Demand Resources