๐Ÿ› Architecture - Android MVC ์•„ํ‚คํ…์ฒ˜ ํŒจํ„ด

sery270ยท2021๋…„ 1์›” 31์ผ
1

Android

๋ชฉ๋ก ๋ณด๊ธฐ
1/13

์•ˆ๋…•ํ•˜์„ธ์š” :) ์˜ค๋Š˜์€ Android ๊ฐœ๋ฐœ์—์„œ ์‚ฌ์šฉ๋˜๋Š” MVC ์•„ํ‚คํ…์ฒ˜ ํŒจํ„ด์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. Android ๊ฐœ๋ฐœ ๊ฒฝํ—˜์ด ์žˆ์œผ์‹œ๋‹ค๋ฉด, ์˜ˆ์ œ๋กœ ์ค€๋น„ํ•œ ์ฝ”๋“œ๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ์ดํ•ด๋ฅผ ๋„์šฐ์‹œ๋ฉด ๋” ์ข‹์Šต๋‹ˆ๋‹ค. ์•ž์œผ๋กœ, MVP, MVVM ์•„ํ‚คํ…์ฒ˜๋„ ์ •๋ฆฌํ•ด๋ณผ ์˜ˆ์ •์ด๋‹ˆ ๊ธฐ๋Œ€ํ•ด์ฃผ์„ธ์š”. ๊ทธ๋Ÿผ ์˜ค๋Š˜๋„ ํ™”์ดํŒ… ์ž…๋‹ˆ๋‹ค ๐ŸŒฟ


MVC ์•„ํ‚คํ…์ฒ˜๋ž€?

  • Model์€ ๋ฐ์ดํ„ฐ ๋กœ์ง์„ ๋‹ด๋‹น, ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ
  • View๋Š” ์‚ฌ์šฉ์ž์˜ ํ™”๋ฉด์„ ๋‹ด๋‹น, UI ์ฒ˜๋ฆฌ ( C๋กœ๋ถ€ํ„ฐ ์•Œ๋ฆผ์„ ๋ฐ›๊ณ , ์ด ์•Œ๋ฆผ์„ M๊ณผ ํ•จ๊ป˜ UI๋กœ ๊ตฌ์„ฑ)
  • Controller๋Š” ๋น„์ฆˆ๋‹ˆ์Šค๋กœ์ง์„ ๋‹ด๋‹น, ์‚ฌ์šฉ์ž์˜ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ

MVC ์•„ํ‚คํ…์ฒ˜ ํŒจํ„ด์˜ ํ๋ฆ„

  1. Control์ด ์‚ฌ์šฉ์ž ์ด๋ฒคํŠธ๋ฅผ ๊ฐ์ง€ํ•œ๋‹ค.
  2. Control๋Š” ์‚ฌ์šฉ์ž ์ด๋ฒคํŠธ์— ๋”ฐ๋ฅธ ๋ฐ์ดํ„ฐ์˜ ์—…๋ฐ์ดํŠธ ์œ ๋ฌด๋ฅผ ํ™•์ธํ•œ๋‹ค.
  3. ๋ฐ์ดํ„ฐ ์—…๋ฐ์ดํŠธ yes) Model์ด ๋ฐ์ดํ„ฐ ๊ฐฑ์‹  ์ฒ˜๋ฆฌ๋ฅผ ํ•œ๋‹ค.
  4. Model์€ View์—๊ฒŒ ์ž์‹ (Model)์ด ์—…๋ฐ์ดํŠธ ๋˜์—ˆ๋‹ค๋Š” ์‚ฌ์‹ค์„ ์•Œ๋ฆฐ๋‹ค. (๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๋‹จ๊ณ„๊ฐ€ ์•„๋‹˜์— ์ฃผ์˜)
  5. View๋Š” Model์ด ์—…๋ฐ์ดํŠธ ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.
  6. ๋ชจ๋ธ ์—…๋ฐ์ดํŠธ yes) View๋Š” ์—…๋ฐ์ดํŠธ๋œ ๋ฐ์ดํ„ฐ๋ฅผ Model๋กœ ๋ถ€ํ„ฐ ๊ฐ€์ ธ์˜ค๊ณ , ์ด๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ UI ๊ฐฑ์‹  ์ฒ˜๋ฆฌ๋ฅผ ํ•œ๋‹ค.

Android์—์„œ MVC ์•„ํ‚คํ…์ฒ˜๋ž€?

์•ˆ๋“œ๋กœ์ด๋“œ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์•„ํ‚คํ…์ฒ˜

MVC : Model/ View/ Controller

MVP : Model/ View/ Presenter

MVVM : Model/ View/ ViewModel

์•ˆ๋“œ๋กœ์ด๋“œ์—์„œ์˜ MVC ์•„ํ‚คํ…์ฒ˜ ํŒจํ„ด์˜ ํ๋ฆ„

> Activity/ Fragment๊ฐ€ View์™€ Control์˜ ์—ญํ• ์„ ๋™์‹œ ์ˆ˜ํ–‰
  1. Activity(Control)๊ฐ€ ์‚ฌ์šฉ์ž ์ด๋ฒคํŠธ๋ฅผ ๊ฐ์ง€ํ•œ๋‹ค. : Activity์˜ Onclicklistener์—์„œ ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒ
  2. Activity(Control)๋Š” ์‚ฌ์šฉ์ž ์ด๋ฒคํŠธ์— ๋”ฐ๋ฅธ ๋ฐ์ดํ„ฐ์˜ ์—…๋ฐ์ดํŠธ ์œ ๋ฌด๋ฅผ ํ™•์ธํ•œ๋‹ค.
  3. ๋ฐ์ดํ„ฐ ์—…๋ฐ์ดํŠธ yes) Model์ด ๋ฐ์ดํ„ฐ ๊ฐฑ์‹  ์ฒ˜๋ฆฌ๋ฅผ ํ•œ๋‹ค.
  4. Model์€ Activity(View)๋กœ ์ž์‹ (Model)์ด ์—…๋ฐ์ดํŠธ ๋˜์—ˆ๋‹ค๋Š” ์‚ฌ์‹ค์„ ์•Œ๋ฆฐ๋‹ค.
  5. Activity(View)๋Š” Model์ด ์—…๋ฐ์ดํŠธ ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.
  6. ๋ชจ๋ธ ์—…๋ฐ์ดํŠธ yes) Activity(View)๋Š” ์—…๋ฐ์ดํŠธ๋œ ๋ฐ์ดํ„ฐ๋ฅผ Model๋กœ ๋ถ€ํ„ฐ ๊ฐ€์ ธ์˜ค๊ณ , ์ด๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ UI ๊ฐฑ์‹  ์ฒ˜๋ฆฌ๋ฅผ ํ•œ๋‹ค.

MVC ์•„ํ‚คํ…์ฒ˜ ์˜ˆ์ œ

MainActiviy.kt

package com.example.mvc

import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.core.content.ContextCompat

class MainActivity : AppCompatActivity() {
    lateinit var model: Model
    private lateinit var textView: TextView
    private lateinit var button: Button

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // View
        textView = findViewById<TextView>(R.id.textView)
        button = findViewById<Button>(R.id.button)
        textView.setTextColor(ContextCompat.getColor(this,R.color.black))

        // Controller
        var selected = false
        model = Model()
        textView.text = model.unselectedString
        button.text = model.unselectedString
        button.setOnClickListener {
            selected = !selected
            textView.text = if (selected) model.selectedString else model.unselectedString
            button.text = if (selected) model.selectedString else model.unselectedString

        }


    }
}

Model.kt

package com.example.mvc

class Model {
    // Model
    val selectedString = "Selected"
    val unselectedString = "Unselected"
}

xml๋ฅผ ํฌํ•จํ•œ ์›๋ณธ ์ฝ”๋“œ๋Š” ์ €์˜ Github์—์„œ ํ™•์ธํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


MVC ์•„ํ‚คํ…์ฒ˜์˜ ์žฅ์ 

  • ์ƒ๊ฐํ•  ๋ถ€๋ถ„์ด ๋งŽ์ง€ ์•Š์•„, ๊ฐœ๋ฐœ ๊ธฐ๊ฐ„์ด ์งง์„ ์ˆ˜ ์žˆ๋‹ค
  • ์ฝ”๋“œ ์ž์ฒด๋ฅผ ํŒŒ์•…ํ•˜๋Š” ๊ฒƒ์ด ์–ด๋ ต์ง€ ์•Š๋‹ค.

MVC ์•„ํ‚คํ…์ฒ˜์˜ ๋‹จ์ 

  • ์œ ์ง€๋ณด์ˆ˜๊ฐ€ ์–ด๋ ค์›Œ์ง„๋‹ค.
    • ๋ณต์‚ฌ ๋ถ™์—ฌ๋„ฃ๊ธฐ๊ฐ€ ๋งŽ์•„์ง„๋‹ค.
      • ๋ณต๋ถ™์ด ๋งŽ์•„์ ธ ์ „์ฒด์ ์ธ ์ฝ”๋“œ ์–‘์ด ์ฆ๊ฐ€ํ•œ๋‹ค.
    • ์ŠคํŒŒ๊ฒŒํ‹ฐ ์ฝ”๋“œ๊ฐ€ ๋  ๊ฐ€๋Šฅ์„ฑ์ด ๋งŽ์•„์ง„๋‹ค.
      • ํ•จ์ˆ˜ ๋ถ„๋ฆฌ ๋ฐ ํด๋ž˜์Šค ๋ถ„๋ฆฌ๊ฐ€ ์ ์ ˆํ•˜๊ฒŒ ์ด๋ค„์ง€์ง€ ์•Š์„ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’๋‹ค.
        • ํ•œ ํด๋ž˜์Šค์—์„œ 3๊ฐœ์˜ ์—ญํ• ์˜ ๋™์ž‘์ด ์ด๋ค„์งˆ ์ˆ˜ ์žˆ์–ด, ํ•œ ํด๋ž˜์Šค์˜ ์ฝ”๋“œ๊ฐ€ ๋งค์šฐ ๋ฐฉ๋Œ€ํ•ด์งˆ ์ˆ˜ ์žˆ๋‹ค.
        • ํ•œ ํด๋ž˜์Šค์˜ ์ฝ”๋“œ ์–‘์ด ๋งŽ์•„์ง„๋‹ค.
  • ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์ž‘์„ฑ์ด ์–ด๋ ต๋‹ค.
    • ๋Œ€๋ถ€๋ถ„์˜ ์ฒ˜๋ฆฌ๊ฐ€ UI์—์„œ ์ด๋ฃจ์–ด์ง€๊ธฐ ๋•Œ๋ฌธ์—, UI ์œ„์ฃผ์˜ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์•ผํ•œ๋‹ค.
  • Model๊ณผ View์˜ ๊ฒฐํ•ฉ๋„๊ฐ€ ๋†’์•„์ง„๋‹ค.
    • ๋Œ€๋ถ€๋ถ„์˜ ์ฝ”๋“œ๋ฅผ View์—์„œ Model์„ ์ง์ ‘ ํ˜ธ์ถœํ•˜์—ฌ ์‚ฌ์šฉํ•˜๊ฒŒ ๋œ๋‹ค.

๋ ˆํผ๋Ÿฐ์Šค

์ง€์‹๋คํ”„

taehwandev/AndroidMVPSample

https://www.youtube.com/watch?v=KCDCElsFZ38

profile
๊ฐœ๋ฐœ์„ธ๋ฆฌ์˜ ์„ฑ์žฅ๊ธฐ๐ŸŒฟ

0๊ฐœ์˜ ๋Œ“๊ธ€