apple/DesignPattern, Architecture

Swift 디자인패턴 MVC Pattern (MVC 패턴)

lgvv 2022. 4. 8. 13:53

Swift 디자인패턴 MVC Pattern (MVC 패턴)

 

MVC 패턴은 개발 아키텍처 중 하나로 모바일, 서버 등 다양한 곳에서 활용.

  • 다만 남용할 경우 Massvie View Controller라는 불리듯이 유지보수가 어려워질 수 있음.
  • 아주 간단한 기능을 제외하곤 유지보수를 위해 추천하지 않음.

 

히스토리

  • 2022-04-08: 디자인 패턴 스터디 정리
  • 2024-11-28: 포스팅 글 재정리 및 예제 변경

MVC pattern

 

코드 예제

가장 간단한 예제를 첨부

 

import UIKit
import SwiftUI
import Combine

private struct Model {
    var name: String
    var count: Int
}

private class ViewController: UIViewController {
    
    // MARK: - State
    
    @Published var model: Model = .init(name: "김철수", count: 0)
    
    private var cancellables: Set<AnyCancellable>
    
    // MARK: - Action
    
    enum Action {
        case up
        case down
    }
    
    // MARK: - Mutation
    
    func send(action: Action) {
        switch action {
        case .up:
            model.count += 1
        case .down:
            model.count -= 1
        }
    }
    
    private func bind() {
        $model
            .sink { [weak self] model in
                guard let self else { return }
                self.label.text = model.name + "의 숫자는" + "\(model.count)"
            }.store(in: &cancellables)
    }
    
    init() {
        self.cancellables = .init()
        
        super.init(nibName: nil, bundle: nil)
        
        bind()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - UIComponents
    
    private lazy var upButton: UIButton = {
        let btn = UIButton()
        btn.addTarget(self, action: #selector(upButtonTapped), for: .touchUpInside)
        return btn
    }()
    
    @objc
    private func upButtonTapped() {
        send(action: .up)
    }
    
    private lazy var downButton: UIButton = {
        let btn = UIButton()
        btn.addTarget(self, action: #selector(upButtonTapped), for: .touchUpInside)
        return btn
    }()
    
    @objc
    private func downButtonTapped() {
        send(action: .down)
    }
    
    private let label = UILabel()
}

 

 

(참고)

https://developer.apple.com/library/archive/documentation/General/Conceptual/CocoaEncyclopedia/Model-View-Controller/Model-View-Controller.html

 

Model-View-Controller

Model-View-Controller The Model-View-Controller design pattern (MVC) is quite old. Variations of it have been around at least since the early days of Smalltalk. It is a high-level pattern in that it concerns itself with the global architecture of an applic

developer.apple.com

https://www.raywenderlich.com/books/design-patterns-by-tutorials/v3.0/chapters/3-model-view-controller-pattern

 

Design Patterns by Tutorials, Chapter 3: Model-View-Controller Pattern

The model-view-controller (MVC) pattern separates objects into three distinct types: models, views and controllers! MVC is very common in iOS programming, because it's the design pattern that Apple chose to adopt heavily in UIKit. In this chapter you’ll

www.kodeco.com:443