[디자인 패턴] Delegation Pattern

J_sung_0o0
1 min readSep 13, 2020

--

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 를 만들지 않도록 조심할 것.

--

--

J_sung_0o0
J_sung_0o0

Written by J_sung_0o0

Apple WWDC 19 & 20 Winner (The 1st two-time winner of all time from South Korea)

No responses yet