[디자인 패턴] Delegation Pattern
Enables object to use another “helper” object to provide data or perform a task.
It has 3 parts:
Object Needing a Delegate(a.k.a. delegating object)
The object that has a delegate: usually held as a “weak” property to avoid retain cycle.
Delegate protocol
defines the method a delegate
Delegate
“helper” object that implements the delegate protocol
delegate protocol -> the implementation is much more flexible
언제 써야 하나?
Use to break up large classes or create generic, reusable components.
UIKit 에서 가장 흔하게 쓰입니다. (e.g. ~DataSource
, ~Delegate
가 들어간 이름들)
DataSource: 데이터를 제공 (e.g. UITableViewDataSource
: UITableViewCell
를 제공 to display)
Delegate: 데이터나 이벤트를 받음 (e.g. UITableViewDelegate
: row가 선택될 때마다 알림제공)
주의사항
매우 유용하지만 과하게 사용할 수 있다. 하나의 객체에 대해 너무 많은 delegate 를 만들지 않도록 조심할 것.