In this lecture we'll dive together into how simple and straightforward it is to use dependency injection to clean up your classes, make sure your classes remain as easy-to-test as possible while truly embracing protocol oriented programming.
to B? • Who creates controller B? • How would you give different implementations for different classes? • How to initialize controllers from storyboards? • Who decide the object scopes (singleton, multiple instances)?
} } class PetOwner: Person { let name: String let pet: Animal init(name: String, pet: Animal) { self.name = name self.pet = pet } } protocol Animal { var name: String { get } } class Cat: Animal { let name: String init(name: String) { self.name = name } }
let pet = (person as! PetOwner).pet print(animal.name) // prints "Mimi" print(animal is Cat) // prints "true" print(person.name) // prints "Stephen" print(person is PetOwner) // prints "true" print(pet.name) // prints "Mimi" print(pet is Cat) // prints "true"
container.resolve(Animal.self, name:"dog")! print(cat.name) // prints "Mimi" print(cat is Cat) // prints "true" print(dog.name) // prints "Hachi" print(dog is Dog) // prints "true"