TIL๐Ÿ”ฅ์ŠคํŒŒ๋ฅดํƒ€ | MVC

hyihyiยท2024๋…„ 1์›” 30์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
44/70
post-thumbnail

๐Ÿ“– MVC

  • Model : ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์ •๋ณด, ๋ฐ์ดํ„ฐ.
    ๋ฐ์ดํ„ฐ ํ†ต์‹ , ๋กœ์ปฌ DB ์ฒ˜๋ฆฌ, UserManager
    ๋ทฐ๋‚˜ ์ปจํŠธ๋กค๋Ÿฌ์— ๋Œ€ํ•ด์„œ ์–ด๋–ค ์ •๋ณด๋„ ์•Œ์ง€ ์•Š์•„์•ผ ํ•œ๋‹ค.
    ์ฆ‰, ๋ฐ์ดํ„ฐ ๋ณ€๊ฒฝ์ด ์ผ์–ด๋‚ฌ์„ ๋•Œ ๋ชจ๋ธ์—์„œ ํ™”๋ฉด UI๋ฅผ ์ง์ ‘ ์กฐ์ •ํ•ด์„œ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋ทฐ๋ฅผ ์ฐธ์กฐํ•˜๋Š” ๋‚ด๋ถ€ ์†์„ฑ๊ฐ’์„ ๊ฐ€์ง€๋ฉด ์•ˆ ๋œ๋‹ค.

  • View : Model์ด ๊ฐ–๊ณ  ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์‹œ๊ฐํ™”.
    input ํ…์ŠคํŠธ, ์ฒดํฌ๋ฐ•์Šค ํ•ญ๋ชฉ ๋“ฑ๊ณผ ๊ฐ™์€ ์‚ฌ์šฉ์ž ์ธํ„ฐํŽ˜์ด์Šค ์š”์†Œ, ๋‹ค์‹œ ๋งํ•ด ๋ฐ์ดํ„ฐ ๋ฐ ๊ฐ์ฒด์˜ ์ž…๋ ฅ, ๊ทธ๋ฆฌ๊ณ  ๋ณด์—ฌ์ฃผ๋Š” ์ถœ๋ ฅ์„ ๋‹ด๋‹น. ์‚ฌ์šฉ์ž๊ฐ€ ํ™”๋ฉด์— ์ž…๋ ฅํ•œ ๊ฐ’ ์ฒ˜๋ฆฌ

  • Controller : Model๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๊ณ , ๊ฐ€์ ธ์˜จ ๋ฐ์ดํ„ฐ๋ฅผ View์—๊ฒŒ ์ „๋‹ฌ. View๋กœ๋ถ€ํ„ฐ ๋ฐ›์€ ์‚ฌ์šฉ์ž ์ž…๋ ฅ์„ Model์—๊ฒŒ ์ „๋‹ฌํ•ด์คŒ.
    ๋ชจ๋ธ์ด๋‚˜ ๋ทฐ์— ๋Œ€ํ•ด์„œ ์•Œ๊ณ  ์žˆ์–ด์•ผ ํ•œ๋‹ค.
    ๋ชจ๋ธ์ด๋‚˜ ๋ทฐ๋Š” ์„œ๋กœ์˜ ์กด์žฌ๋ฅผ ๋ชจ๋ฅด๊ณ , ๋ณ€๊ฒฝ์„ ์™ธ๋ถ€๋กœ ์•Œ๋ฆฌ๊ณ , ์ˆ˜์‹ ํ•˜๋Š” ๋ฐฉ๋ฒ•๋งŒ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”๋ฐ ์ด๋ฅผ ์ปจํŠธ๋กค๋Ÿฌ๊ฐ€ ์ค‘์žฌํ•˜๊ธฐ ์œ„ํ•ด ๋ชจ๋ธ๊ณผ ๊ทธ์— ๊ด€๋ จ๋œ ๋ทฐ์— ๋Œ€ํ•ด์„œ ์•Œ๊ณ  ์žˆ์–ด์•ผ ํ•œ๋‹ค.
    ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋ฉ”์ธ ๋กœ์ง ๋‹ด๋‹น
    Model๊ณผ View๋Š” ์ง์ ‘์ ์œผ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์ง€ ์•Š์Œ. ์ด ๋‘˜์„ ์—ฐ๊ฒฐ์‹œ์ผœ ์ฃผ๋Š” ๊ฒƒ์ด Controller

๐Ÿค” MVC ํŒจํ„ด์„ ์™œ ์“ธ๊นŒ?

์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ Model, View, Controller ์„ธ ๊ฐ€์ง€ ๊ตฌ์„ฑ ์š”์†Œ๋กœ ๋ถ„๋ฆฌํ•ด ์„œ๋กœ ๋…๋ฆฝ์ ์œผ๋กœ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๋‹ค. ์ด๋Š” ํ•œ ๋ถ€๋ถ„์˜ ๋ณ€๊ฒฝ์ด ๋‹ค๋ฅธ ๋ถ€๋ถ„์— ๋ฏธ์น˜๋Š” ์˜ํ–ฅ์„ ์ตœ์†Œํ™”ํ•˜๋ฉฐ, ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์œ ์ง€๋ณด์ˆ˜์™€ ํ™•์žฅ์ด ์‰ฝ๋‹ค.

๐Ÿ“ ํ๋ฆ„

  1. ์‚ฌ์šฉ์ž์˜ ์•ก์…˜์€ Activity/Fragment๊ฐ€ ๋ฐ›์Œ
  2. Activity/Fragment๋Š” ์ด๋ฅผ ํ™•์ธํ•˜๊ณ  ํ•„์š”ํ•œ ๊ฒฝ์šฐ Model์— ์—…๋ฐ์ดํŠธ
  3. Controller๋Š” Model๋กœ๋ถ€ํ„ฐ ์ „๋‹ฌ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ ํ†ตํ•ด View๋ฅผ ์—…๋ฐ์ดํŠธ

๐Ÿšจ????????????????์งˆ๋ฌธํ•ด์•ผ ํ•จ
"Activity/Fragment๊ฐ€ Controller์˜ ์—ญํ• ๊นŒ์ง€ ๋งก๊ณ  ์žˆ์Œ"๋ผ๊ณ  ์ ํ˜€์žˆ๋Š”๋ฐ Activity/Fragment๋ฅผ Controller๋ผ๊ณ  ๋ถ€๋ฅด๋Š” ๊ฒŒ ์•„๋‹ˆ๊ณ  Controller๊ฐ€ ์›๋ž˜ ๋”ฐ๋กœ ์žˆ์–ด์•ผ ํ•˜๋Š”์ง€???

๐Ÿšจ????????????????์งˆ๋ฌธํ•ด์•ผ ํ•จ
View์™€ Model ์‚ฌ์ด์˜ ํ™”์‚ดํ‘œ๋Š” ์—†๋Š”์ง€

์˜ˆ์‹œ

1๋‹จ๊ณ„: ์‚ฌ์šฉ์ž ์•ก์…˜ ์ˆ˜์‹ 
์‚ฌ์šฉ์ž๊ฐ€ ์›น ์‚ฌ์ดํŠธ์˜ ๊ฒ€์ƒ‰ ๋ฐ”์— ํŠน์ • ๋„์„œ์˜ ์ œ๋ชฉ์„ ์ž…๋ ฅํ•˜๊ณ  ๊ฒ€์ƒ‰ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค.

2๋‹จ๊ณ„: Model ์—…๋ฐ์ดํŠธ
๊ฒ€์ƒ‰ ์š”์ฒญ์„ ๋ฐ›์€ Controller๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ๋„์„œ ์ œ๋ชฉ์„ ๊ธฐ๋ฐ˜์œผ๋กœ BookModel์—์„œ ํ•ด๋‹น ๋„์„œ ์ •๋ณด๋ฅผ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค. BookModel์€ ๋„์„œ์˜ ์ œ๋ชฉ, ์ €์ž, ์ถœํŒ๋…„๋„, ISBN, ๋Œ€์—ฌ ์ƒํƒœ ๋“ฑ์˜ ์ •๋ณด๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ์— ํ•ด๋‹นํ•˜๋Š” ๋„์„œ ์ •๋ณด(๋“ค)๋Š” BookModel์„ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.

3๋‹จ๊ณ„: View ์—…๋ฐ์ดํŠธ
Controller๋Š” BookModel๋กœ๋ถ€ํ„ฐ ๋ฐ›์€ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‚ฌ์šฉ์ž์—๊ฒŒ ๋„์„œ ์ •๋ณด๋ฅผ ํ‘œ์‹œํ•˜๋Š” View๋ฅผ ์—…๋ฐ์ดํŠธํ•ฉ๋‹ˆ๋‹ค. ์ด ๋•Œ, View๋Š” ๋„์„œ์˜ ๋ชฉ๋ก์„ ํ‘œ์‹œํ•˜๋ฉฐ, ๊ฐ ๋„์„œ ์˜†์—๋Š” ๋Œ€์—ฌ ๊ฐ€๋Šฅ ์—ฌ๋ถ€์™€ ๋Œ€์—ฌ๋ฅผ ์œ„ํ•œ ๋ฒ„ํŠผ์ด ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค.
์‚ฌ์šฉ์ž๊ฐ€ ํŠน์ • ๋„์„œ์˜ ๋Œ€์—ฌ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด, Controller๋Š” ํ•ด๋‹น ๋„์„œ์˜ ๋Œ€์—ฌ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์‹œ BookModel์„ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค. BookModel์€ ํ•ด๋‹น ๋„์„œ์˜ ๋Œ€์—ฌ ์ƒํƒœ๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๊ณ , ์ด ์ •๋ณด๋ฅผ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
๋„์„œ๊ฐ€ ์„ฑ๊ณต์ ์œผ๋กœ ๋Œ€์—ฌ๋˜๋ฉด, Controller๋Š” ๋Œ€์—ฌ ์„ฑ๊ณต ๋ฉ”์‹œ์ง€๋ฅผ ํฌํ•จํ•œ ์—…๋ฐ์ดํŠธ๋œ View๋ฅผ ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.

Model์€ ๋„์„œ ์ •๋ณด์™€ ๋Œ€์—ฌ ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•˜๋ฉฐ, View๋Š” ์‚ฌ์šฉ์ž ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๋‹ด๋‹นํ•˜๊ณ , Controller๋Š” ์‚ฌ์šฉ์ž์˜ ์ž…๋ ฅ๊ณผ ์‹œ์Šคํ…œ์˜ ์‘๋‹ต ์‚ฌ์ด์—์„œ ์ค‘๊ฐ„์ž ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค

๐Ÿ“์˜ˆ์‹œ ์ฝ”๋“œ

Model

Book ํด๋ž˜์Šค๋Š” ๋„์„œ ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๊ณ  ๊ด€๋ฆฌ
UI๋ฅผ ์ง์ ‘ ์ˆ˜์ •ํ•˜๋ฉด ์•ˆ๋œ๋‹ค.

์ง€ํ‚จ ์˜ˆ์‹œ

data class Book(val title: String, val author: String)

์ง€ํ‚ค์ง€ ์•Š์€ ์˜ˆ์‹œ

Book ํด๋ž˜์Šค๊ฐ€ View์˜ ๊ตฌํ˜„์— ์˜์กดํ•˜๊ฒŒ ๋งŒ๋“ฆ

data class Book(val title: String, val author: String, val view: BookView) {
    fun display() {
        view.showTitle(title)
    }
}

View

BookView ํด๋ž˜์Šค๋Š” ์‚ฌ์šฉ์ž์—๊ฒŒ ์ •๋ณด๋ฅผ ํ‘œ์‹œํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ •์˜
Model๋กœ๋ถ€ํ„ฐ ์ง์ ‘ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์œผ๋ฉด ์•ˆ๋˜๊ณ  BookController๋ฅผ ํ†ตํ•ด ๋ฐ›์•„์˜จ ํ›„ ์ถœ๋ ฅํ•ด์•ผ ํ•œ๋‹ค.

์ง€ํ‚จ ์˜ˆ์‹œ

class BookView {
    fun showBooks(books: List<Book>) {
        println("Search Results:")
        books.forEach { book ->
            println("'${book.title}' by ${book.author}")
        }
    }
}

์ง€ํ‚ค์ง€ ์•Š์€ ์˜ˆ์‹œ(์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด Book ๊ฐ์ฒด์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ง์ ‘ ์ „๋‹ฌ ๋ฐ›์Œ)

BookView๋Š” Book ๋ชจ๋ธ์— ์ง์ ‘ ์˜์กดํ•˜๊ฒŒ ๋จ

class BookView(private val books: List<Book>) {
    fun displayBooks() {
        for (book in books) {
            println("Book: ${book.title} by ${book.author}")
        }
    }
}

Controller

BookController ํด๋ž˜์Šค๋Š” ์‚ฌ์šฉ์ž์˜ ์ž…๋ ฅ์„ ์ฒ˜๋ฆฌํ•˜๊ณ 
: ๊ฒ€์ƒ‰ ์ฟผ๋ฆฌ(์ž…๋ ฅ)์„ ์ธ์ž๋กœ ๋ฐ›๊ณ  ๊ฒ€์ƒ‰์ฐฝ์„ ํ†ตํ•ด ์ž…๋ ฅํ•œ ๊ฒ€์ƒ‰์–ด๋ฅผ ์ฒ˜๋ฆฌ

Book ๋ชจ๋ธ ๊ฐ์ฒด ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ด€๋ฆฌํ•˜๊ณ  filter๋ฅผ ํ†ตํ•ด Model์— ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒ
BookView ์ธ์Šคํ„ด์Šค์— ์ ‘๊ทผํ•ด ํ•„ํ„ฐ๋ง๋œ ๋„์„œ ๋ฆฌ์ŠคํŠธ๋ฅผ searchBooks(results)๋กœ ์ „๋‹ฌํ•จ

class BookController {
    private val books = listOf(
        Book("The Great Gatsby", "F. Scott Fitzgerald"),
        Book("To Kill a Mockingbird", "Harper Lee"),
        Book("1984", "George Orwell")
    )

    private val view = BookView()

    fun searchBooks(query: String) {
        val results = books.filter { it.title.contains(query, ignoreCase = true) }
        view.showBooks(results)
    }
}

์‚ฌ์šฉ ์˜ˆ

fun main() {
    val controller = BookController()

    println("Enter book title to search:")
    val query = readLine() ?: ""
    controller.searchBooks(query) // ์‚ฌ์šฉ์ž ์ž…๋ ฅ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ฒ€์ƒ‰
}

๐Ÿšจ???????????????์งˆ๋ฌธํ•ด์•ผ ํ•จ
:์ด๋ ‡๊ฒŒ ํ•˜๋Š” ์ด์œ ๊ฐ€ ์ˆ˜์ •ํ•  ๋•Œ ์—ฌ๋Ÿฌ ๋ถ€๋ถ„์„ ์ˆ˜์ •ํ•˜์ง€ ์•Š๊ณ  Controller๋งŒ ์ˆ˜์ •ํ•˜๋ฉด ๋˜๊ฒŒ ํ•˜๋ ค๊ณ  ๊ทธ๋Ÿฐ๊ฐ€...?

profile
๋‚ด๊ฐ€ ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๊ฒŒ ์“ฐ๋Š” ๋ธ”๋กœ๊ทธ

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