There are two examples below that produce the same result. Trying to understand which one is better in which situations (Pros and Cons).
(1) Two classes with Dependency injection
class ServiceClass {
init() {}
func printHelloWorld() {
print("Hello World")
}
}
class ExampleClass {
let service: ServiceClass
init(service: ServiceClass) {
self.service = service
}
func doSomething() {
service.printHelloWorld()
}
}
(2) Class conforming to protocol
protocol ServiceProtocol {
func doSomething()
}
extension ServiceProtocol {
func printHelloWorld() {
print("hello world")
}
}
class ExampleClass: ServiceProtocol {
init() {}
func doSomething() {
printHelloWorld()
}
}