본문은 야곰 아카데미 커리어 스타터 캠프를 통해 학습한 내용을 회고한 글입니다.
델리게이션을 사용한 리팩토링 Refactoring with delegation
기존에 싱글톤을 사용해서 만든 프로젝트를 델리게이션을 사용해서 리팩토링 해보았다.
class FruitStore {
var fruitStock = [Fruit.딸기: 10, .바나나: 10, .파인애플: 10, .키위: 10, .망고: 10]
static let shared = FruitStore()
private init() {}
func useFruit(juice: Juice) -> [Fruit: Int]? {
...
}
class FruitStore {
var fruitStock = [Fruit.딸기: 10, .바나나: 10, .파인애플: 10, .키위: 10, .망고: 10]
func useFruit(juice: Juice) -> [Fruit: Int]? {
...
}
기존에 있던 싱글톤을 없애주었다. static 키워드를 사용해 만들어준 shared를 없애고 private init()역시 없애준다.
struct JuiceMaker {
let fruitStore = FruitStore.shared
func makeJuice(juiceName: Juice) -> Juice? {
if fruitStore.useFruit(juice: juiceName) != nil {
return juiceName
}
return nil
}
}
struct JuiceMaker {
let fruitStore: FruitStore
init(_ fruitStore: FruitStore) {
self.fruitStore = fruitStore
}
func makeJuice(juiceName: Juice) -> Juice? {
if fruitStore.useFruit(juice: juiceName) != nil {
return juiceName
}
return nil
}
}
FruitStore.shared를 지우고 이니셜라이저를 생성한다.
private func presentModal() {
guard let stockModifyViewController = self.storyboard?.instantiateViewController(withIdentifier: "stockModifyViewController") else {
return
}
let navigationController = UINavigationController(rootViewController: stockModifyViewController)
navigationController.modalPresentationStyle = UIModalPresentationStyle.fullScreen
present(navigationController, animated: true, completion: nil)
}
private func presentModal() {
guard let stockModifyViewController = self.storyboard?.instantiateViewController(withIdentifier: "stockModifyViewController") as? StockModifyViewController else {
return
}
stockModifyViewController.delegate = self
let navigationController = UINavigationController(rootViewController: stockModifyViewController)
navigationController.modalPresentationStyle = UIModalPresentationStyle.fullScreen
present(navigationController, animated: true, completion: nil)
}
self.storyboard?.instantiateViewController를 StockModifyViewController로 다운캐스팅을 해주고, stockModifyViewController의 delegate에 self 즉 selectJuiceViewController를 지정해준다.
protocol FruitStockDelegate {
var fruitStore: FruitStore { get }
func fetchStock(fruit: Fruit) -> Int?
}
extension FruitStockDelegate {
func fetchStock(fruit: Fruit) -> Int? {
let stock = fruitStore.fruitStock[fruit]
return stock
}
}
protocol FruitStock {
var delegate: FruitStockDelegate? { get set }
}
FruitStockDelegate라는 프로토콜을 만들어준다. 그리고 extension을 통해서 func를 정의해준다. FruitStockDelegate 내부에서는 fetchStock을 정의만 해놓고 코드 블록을 사용하지 않는다.
final class SelectJuiceViewController: UIViewController, FruitStockDelegate {
let fruitStore = FruitStore()
...
}
selectJuiceViewController에서 FruitStockDelegate 프로토콜을 채택해준다.
final class StockModifyViewController: UIViewController, FruitStock {
var delegate: FruitStockDelegate?
...
}
StockModifyViewController에서 FruitStock 프로토콜을 채택해준다. 그리고 delegate 변수를 만들어준다.
참조
'YAGOM CAREER STARTER' 카테고리의 다른 글
[토요스터디A반] 20230121: UML을 바탕으로 코드 작성하기 (0) | 2023.01.23 |
---|---|
[TIL] 20230119: 데이터전달(델리게이트, 노티피케이션센터) (0) | 2023.01.21 |
[TIL] 20230113: Protocol2 (0) | 2023.01.14 |
[TIL] 20230112: Closures (0) | 2023.01.13 |
[TIL] 20230110: Protocol1 (0) | 2023.01.11 |