๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐ŸŽ iOS/Architecture

[Design Pattern] Apple์˜ MVC ํŒจํ„ด์ด๋ž€?

by Fomagran ๐Ÿ’ป 2021. 6. 3.
728x90
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š” Foma ๐Ÿ‘Ÿ ์ž…๋‹ˆ๋‹ค!

 

์ €๋ฒˆ ๊ธ€์— MVC ํŒจํ„ด์— ๋Œ€ํ•ด์„œ ๋‹ค๋ค˜์—ˆ๋Š”๋ฐ iOS์—์„œ ๋‹ค๋ฃจ๋Š” ๊ฒƒ๊ณผ๋Š” ์กฐ๊ธˆ ๋‹ค๋ฅด๊ฒŒ

 

์ปค์Šคํ…€๋ทฐ๋ฅผ ์ง์ ‘ ๋งŒ๋“ค์–ด์„œ ๋ญ”๊ฐ€ ๋” ์ •์„์ ์ธ MVC ํŒจํ„ด์„ ๊ตฌํ˜„ํ–ˆ์–ด์š”.

 

(ํ˜น์‹œ ์•ˆ๋ณด์…จ๊ฑฐ๋‚˜ ๊ถ๊ธˆํ•˜์‹  ๋ถ„๋“ค์€ ์—ฌ๊ธฐ ์—์„œ ๋ด์ฃผ์„ธ์š”!)

 

ํ•˜์ง€๋งŒ ๋ณดํ†ต MVCํ•˜๊ณ ๋Š” ์กฐ๊ธˆ ๋‹ค๋ฅธ ๋ถ€๋ถ„์ด ์žˆ์–ด์š”.

 

๊ทธ๋ž˜์„œ ์˜ค๋Š˜์€ ์• ํ”Œ์˜ MVC ํŒจํ„ด์— ๋Œ€ํ•ด์„œ ์ •๋ฆฌํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!


Apple's MVC

 

์• ํ”Œ์€ ์ „ํ˜•์ ์ธ MVC ํŒจํ„ด์—์„œ ๋ชจ๋ธ๊ณผ ๋ทฐ์™€ ์ปจํŠธ๋กค๋Ÿฌ๊ฐ€ ์„œ๋กœ ์˜์กด์ ์ธ ๊ฒƒ์„ ๋ถ„๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด

 

์ž์‹ ๋“ค๋งŒ์˜ MVC ํŒจํ„ด์„ ๊ฐœ๋ฐœํ•ฉ๋‹ˆ๋‹ค.

 

์ปจํŠธ๋กค๋Ÿฌ๊ฐ€ ๋ทฐ์™€ ๋ชจ๋ธ์„ ์—ฐ๊ฒฐ์‹œ์ผœ์ฃผ์–ด ์„œ๋กœ ๋ถ„๋ฆฌ๋˜์–ด ์žˆ๊ณ  ์„œ๋กœ์— ๋Œ€ํ•ด ์•Œ ํ•„์š”๊ฐ€ ์—†๊ฒŒ๋” ๋งŒ๋“œ๋ ค๊ณ  ํ–ˆ์œผ๋‚˜....

 

 

 

์ปจํŠธ๋กค๋Ÿฌ๊ฐ€ ๋ทฐ์˜ ๋ผ์ดํ”„ ์‚ฌ์ดํด์— ๊ด€์—ฌํ•˜๊ฒŒ ๋˜๋ฉด์„œ MV - C ํŒจํ„ด์ด ๋˜์–ด๋ฒ„๋ฆฝ๋‹ˆ๋‹ค.

 

 

์ด๋ ‡๊ฒŒ ๋˜๋ฉด์„œ ๋ทฐ์˜ ์ฝ”๋“œ์™€ ์ปจํŠธ๋กค๋Ÿฌ์˜ ์ฝ”๋“œ๊ฐ€ ๊ฒน์น˜๋ฉด์„œ ๊ฒฐ๊ณผ์ ์œผ๋กœ ๋ทฐ์ปจํŠธ๋กค๋Ÿฌ๊ฐ€ ๋น„๋Œ€ํ•ด์ง€๊ณ  ์—ญํ• ์ด ์ง€๋‚˜์น˜๊ฒŒ ์ปค์ ธ๋ฒ„๋ ธ์Šต๋‹ˆ๋‹ค.

 

์ด๋ ‡๋‹ค๋ณด๋‹ˆ ํ˜„์žฌ์˜ MVP ๋””์ž์ธ ํŒจํ„ด๊ณผ ๋น„์Šทํ•˜๊ฒŒ ๋˜์–ด๋ฒ„๋ ธ์ฃ .

 

์žฅ์ ๊ณผ ๋‹จ์ ์„ ์ •๋ฆฌํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

์žฅ์ 

 

- ๊ฐœ๋ฐœ ์†๋„๊ฐ€ ์••๋„์ ์œผ๋กœ ๋น ๋ฅด๋‹ค.

 

- ์ฝ”๋“œ๊ฐ€ ์ ๊ฒŒ ์“ฐ์ธ๋‹ค.

 

- ๋งŽ์€ ๊ฐœ๋ฐœ์ž์—๊ฒŒ ์นœ์ˆ™ํ•˜๋‹ค.

 

- ๋ชจ๋ธ๊ณผ ๋ทฐ์˜ ์ƒํ˜ธ์˜์กด์„ฑ์„ ์—†์•ด๋‹ค.

 

๋‹จ์ 

 

- ๋ทฐ์ปจํŠธ๋กค๋Ÿฌ์˜ ์—ญํ• ์ด ๋น„๋Œ€ํ•ด์ง„๋‹ค.

 

- ํ…Œ์ŠคํŠธ๊ฐ€ ์šฉ์ดํ•˜์ง€ ์•Š๋‹ค.

 

- ์œ ์ง€๋ณด์ˆ˜๊ฐ€ ํž˜๋“ค๋‹ค.


Model

 

๋ชจ๋ธ์€ ๊ธฐ์กด์˜ MVC ํŒจํ„ด๊ณผ ๋™์ผํ•˜๊ฒŒ ๋ทฐ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์—ญํ• ์ž…๋‹ˆ๋‹ค.

 

 

 

์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

import UIKit

struct AppleMVC_Character {
    
    //ํ™”๋ฉด์„ ๊ตฌ์„ฑํ•  ๋ฐ์ดํ„ฐ
    let image:UIImage
    let name:String
    let gender:String
    let country:String
    
    //์ดˆ๊ธฐํ™”
    init(name:String,image:UIImage,gender:String,country:String) {
        self.name = name
        self.image = image
        self.gender = gender
        self.country = country
    }
}

 


ViewController

 

์ปจํŠธ๋กค๋Ÿฌ ์•ˆ์— ๋ทฐ๊ฐ€ ์žˆ๋Š” ํ˜•ํƒœ๋กœ ์ปจํŠธ๋กค๋Ÿฌ๊ฐ€ ์ž์‹ ์˜ ์—ญํ• ๊ณผ ๋ทฐ์˜ ์—ญํ• ์„ ๋™์‹œ์— ์ˆ˜ํ–‰ํ•œ๋‹ค.

 

์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

๋ทฐ์ปจํŠธ๋กค๋Ÿฌ ์ž์ฒด์—์„œ ์œ ์ €์— ๋Œ€ํ•œ ์ธํ„ฐ๋ž™์…˜์„ ๊ฐ์ง€ํ•˜๊ณ  ๋ชจ๋ธ์„ ์—…๋ฐ์ดํŠธํ•˜๋ฉฐ

 

๋ฐ”๋€ ๋ชจ๋ธ์˜ ๊ฐ’์— ๋งž๊ฒŒ ๋ทฐ๋ฅผ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค.

 

์ปจํŠธ๋กค๋Ÿฌ์—์„œ ๊ฑฐ์˜ ๋ชจ๋“  ๊ฒƒ์„ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋น„๊ต์  ๋‹จ์ˆœํ•œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

import UIKit

class AppleMVC_CharacterViewController: UIViewController {

    
    //์ „์ฒด ๋ชจ๋ธ ๋ฐ์ดํ„ฐ
    let characters:[AppleMVC_Character] = [
        AppleMVC_Character(name:"์Šคํฐ์ง€๋ฐฅ",image: #imageLiteral(resourceName: "แ„‰แ…ณแ„‘แ…ฉแ†ซแ„Œแ…ตแ„‡แ…กแ†ธ"), gender: "๋‚จ์ž", country:"๋ฏธ๊ตญ"),
        AppleMVC_Character(name:"๋šฑ์ด",image: #imageLiteral(resourceName: "แ„„แ…ฎแ†ผแ„‹แ…ต"), gender: "๋‚จ์ž", country:"๋ฏธ๊ตญ"),
        AppleMVC_Character(name:"์ง•์ง•์ด",image: #imageLiteral(resourceName: "แ„Œแ…ตแ†ผแ„Œแ…ตแ†ผแ„‹แ…ต"), gender: "๋‚จ์ž", country:"๋ฏธ๊ตญ"),
        AppleMVC_Character(name:"ํ”Œ๋žญํฌํ†ค",image: #imageLiteral(resourceName: "แ„‘แ…ณแ†ฏแ„…แ…กแ†ผแ„แ…ณแ„แ…ฉแ†ซ"), gender: "๋‚จ์ž", country:"๋ฏธ๊ตญ"),
        AppleMVC_Character(name:"ํํ์—ฌ์‚ฌ",image: #imageLiteral(resourceName: "แ„‘แ…ฉแ†ผแ„‘แ…ฉแ†ผแ„‡แ…ฎแ„‹แ…ตแ†ซ"), gender: "์—ฌ์ž", country:"๋ฏธ๊ตญ")
    ]
    
    //MARK:- IBOutlets
    
    @IBOutlet weak var country: UILabel!
    @IBOutlet weak var name: UILabel!
    @IBOutlet weak var gender: UILabel!
    @IBOutlet weak var image: UIImageView!
    @IBOutlet weak var nextButton: UIButton!
    @IBOutlet weak var previousButton: UIButton!
    
    //MARK:- Properties
    
    //ํ˜„์žฌ ์ธ๋ฑ์Šค
    private var index:Int = 0
    
    //ํ˜„์žฌ ๋ชจ๋ธ ๋ฐ์ดํ„ฐ
    private var character:AppleMVC_Character!
    
    //MARK:- Life Cycle
    override func viewDidLoad() {
        super.viewDidLoad()
        configure()
    }
    
    //MARK:- Helpers
    
    //์ดˆ๊ธฐํ™”
    private func configure() {
        character = characters[index]
        updateView()
    }
    
    //๋ทฐ ์—…๋ฐ์ดํŠธ
    private func updateView() {
        
        //์ƒˆ๋กœ์šด ๋ชจ๋ธ
        let newCharacter = characters[index]
        
        //์ƒˆ๋กœ์šด ๋ชจ๋ธ์— ๋งž๊ฒŒ ๋ทฐ ๋ณ€๊ฒฝ
        self.image.image = newCharacter.image
        self.name.text = newCharacter.name
        self.gender.text = newCharacter.gender
        self.country.text = newCharacter.country
    }
    
    //MARK:- IBActions
    
    //์ด์ „ ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €๋‹ค๋ฉด
    @IBAction func tapPreviousButton(_ sender: Any) {
        //์ด์ „ ์ธ๋ฑ์Šค๋กœ ๋ณ€๊ฒฝ
        if index > 0 {
            index -= 1
        }
        //๋ทฐ ์—…๋ฐ์ดํŠธ
        updateView()
    }
    
    //๋‹ค์Œ ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €๋‹ค๋ฉด
    @IBAction func tapNextButton(_ sender: Any) {
        //๋‹ค์Œ ์ธ๋ฑ์Šค๋กœ ๋ณ€๊ฒฝ
        if index < 4 {
            index += 1
        }
        //๋ทฐ ์—…๋ฐ์ดํŠธ
        updateView()
    }
}

์˜ค๋Š˜์€ ์ด๋ ‡๊ฒŒ ์ „ํ†ต์ ์ธ MVC์™€๋Š” ์•ฝ๊ฐ„ ๋‹ค๋ฅธ Apple๋งŒ์˜ MVC์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์•˜์Šต๋‹ˆ๋‹ค.

 

์ต์ˆ™ํ•ด์„œ ๊ทธ๋Ÿฐ์ง€ ํ™•์‹คํžˆ ๊ตฌ์กฐ๋„ ๋‹จ์ˆœํ•˜๊ณ  ๊ฐœ๋ฐœํ•˜๋Š” ์†๋„๊ฐ€ ๋นจ๋ž์Šต๋‹ˆ๋‹ค.

 

ํ˜น์‹œ๋ผ๋„ ๊ถ๊ธˆํ•˜์‹  ์ ์ด ์žˆ๊ฑฐ๋‚˜ ํ‹€๋ฆฐ ๋ถ€๋ถ„์ด ์žˆ๋‹ค๋ฉด ๋Œ“๊ธ€๋กœ ์•Œ๋ ค์ฃผ์„ธ์š”!

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€