5. Protocol Oriented Programming
If someone is oriented towards or oriented to a particular thing or
person, they are mainly concerned with that thing or person.
oriented
[ ɔːrientɪd ]
ADJECTIVE
27. Dependency Injection
class AppDelegate: UIResponder, UIApplicationDelegate {
let container = Container()
func application(application:,didFinishLaunchingWithOptions:)
-
>
Bool {
container.register(Repository.self) { _ in UserDefaultRepository() }
container.register(ToDoToggableService.self) { c in
let repository = c.resolve(Repository.self)!
return ToDoServiceImpl(repository: repository)
}
return true
}
}
func Inject<Service>(_ serviceType: Service.Type)
-
>
Service? {
(UIApplication.shared.delegate as? AppDelegate)
?
.
container.resolve(serviceType)
}
class ToDoListViewController: UITableViewController {
let service: ToDoToggableService = Inject(ToDoToggableService.self)!
.
.
.
}
https:
/
/
github.com/Swinject/Swinject
28. Q. 다음 중 올바르지 않은 것은?
protocol P {}
protocol PROTOCOL {}
extension PROTOCOL: P {}
protocol C {}
class CLASS {}
extension CLASS: C {}
protocol S {}
struct STRUCT {}
extension STRUCT: S {}
protocol E {}
enum ENUM {}
extension ENUM: E {}
1⃣ 2⃣
3⃣ 4⃣
29. 🤔 OOP 에서는 Protocol 의 역할이 많구나!
😏 그렇다면 Protocol을 더 적극적으로 써보자
🧐 class, struct, enum 전부 다 Protocol 적용하게 하자!
😠 Protocol이 진짜 중요해졌네!!!
😎 그렇다면 이제부터 Protocol을 중심으로 코딩해
🤩 그걸 Protocol Oriented Programming 이라고 하자
의식의 흐름
30. Summary
1. Protocol 로 할 수 있는 것들 == Protocol을 사용해서 얻는 이점
•Delegate
•구현 강제
•추상화
•느슨한 결합
➡ 유연한 확장
2. 구현 과정에서 구현체 보다 Protocol 을 우선해서 생각하자
3. POP == Protocol을 적극 활용하는 OOP
Oriented Programming