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
}
}
}
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
}
}
}
위에 tight coupling 파트와 다르게 APIService를 직접 호출한게 아닌
protocol을 호출하여 결합도를 낮추었다.