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

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

Android

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

์•ˆ๋…•ํ•˜์„ธ์š” :) Android ๊ฐœ๋ฐœ์—์„œ ์‚ฌ์šฉ๋˜๋Š” MVP ์•„ํ‚คํ…์ฒ˜ ํŒจํ„ด์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์˜ค๋Š˜์€ ์ž˜ ์•Œ๋ ค์ง„ MVP์˜ ๊ตฌํ˜„ ๋ฐฉ๋ฒ•๋“ค ์ค‘์—์„œ Google์—์„œ ์ œ๊ณตํ•œ ์ƒ˜ํ”Œ ์ฝ”๋“œ์˜ Contract ์ •์˜ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ์„ค๋ช…์„ ์ž์„ธํžˆ ํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ ์˜ค๋Š˜๋„ ํ™”์ดํŒ… ์ž…๋‹ˆ๋‹ค ๐ŸŒฟ

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

  • Model์€ ๋ฐ์ดํ„ฐ ๋กœ์ง์„ ๋‹ด๋‹นํ•˜๊ณ , ๋กœ์ปฌ ๋ฐ์ดํ„ฐ๋‚˜ ์„œ๋ฒ„์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์š”์ฒญํ•˜์—ฌ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
  • View๋Š” ํ™”๋ฉด์—์„œ ๋ฐœ์ƒํ•˜๋Š” ์ด๋ฒคํŠธ๋ฅผ ๊ฐ์ง€ํ•˜๊ณ , UI ์ฒ˜๋ฆฌ๋ฅผ Presenter์—๊ฒŒ ์š”์ฒญํ•ฉ๋‹ˆ๋‹ค.
  • Presenter๋Š” View๋กœ๋ถ€ํ„ฐ ์š”์ฒญ๋ฐ›์€ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌํ•˜๊ณ  ์ฒ˜๋ฆฌํ•œ ๊ฒฐ๊ณผ๋ฅผ View์—๊ฒŒ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. (๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๊ฑฐ๋‚˜, ๋ฐ์ดํ„ฐ๋ฅผ ๊ณ„์‚ฐ ๋“ฑ์˜ ์ฒ˜๋ฆฌ๋กœ ๊ฐ€๊ณตํ•ฉ๋‹ˆ๋‹ค.)

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

  1. View๊ฐ€ ์‚ฌ์šฉ์ž ์ด๋ฒคํŠธ๋ฅผ ๊ฐ์ง€ํ•œ๋‹ค.
  2. View๊ฐ€ Presenter์—๊ฒŒ ์ด๋ฒคํŠธ ๋ฐœ์ƒ ์—ฌ๋ถ€๋ฅผ ์ „๋‹ฌํ•œ๋‹ค. (์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ ์š”์ฒญ)
  3. Presenter๋Š” Model์—๊ฒŒ, View๊ฐ€ ์š”์ฒญํ•œ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ์— ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์š”์ฒญํ•œ๋‹ค.
  4. Model์€ ๋กœ์ปฌ or ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.
  5. Model์€ ๊ฐ€์ ธ์˜จ ๋ฐ์ดํ„ฐ๋ฅผ Presenter์—๊ฒŒ ์ „๋‹ฌํ•œ๋‹ค.
  6. Presenter๋Š” View๊ฐ€ ์š”์ฒญํ•œ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ๋ฅผ ํ•œ๋‹ค. (๋ฐ์ดํ„ฐ ๊ฐ€๊ณต)
  7. Presenter๋Š” View์—๊ฒŒ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ์ „๋‹ฌ ํ•œ๋‹ค.
  8. View๋Š” Presenter๋กœ๋ถ€ํ„ฐ ์ „๋‹ฌ๋ฐ›์€ ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ง€๊ณ  UI๋ฅผ ๊ฐฑ์‹ ํ•œ๋‹ค.

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

์•ˆ๋“œ๋กœ์ด๋“œ์—์„œ MVP๋ฅผ ๊ตฌํ˜„ํ•˜๋ ค๋ฉด ?

  • Google architecture๋ฅผ ๋”ฐ๋ฅด๋Š” ๋ฐฉ๋ฒ•

    • Contract : View์™€ Presenter์— ๋Œ€ํ•œ interface์„ ์ž‘์„ฑ
    • Presenter : Contract.Presenter์„ ์ƒ์†๋ฐ›์•„์„œ ๊ตฌํ˜„
    • View (Fragment) : Contract.View์„ ์ƒ์†๋ฐ›์•„์„œ ๊ตฌํ˜„
  • View์— ๋Œ€ํ•œ interface๋งŒ ์ •์˜ํ•˜๋Š” ๋ฐฉ๋ฒ•

    • interface View : View์— ๋Œ€ํ•œ interface๋งŒ ์ •์˜
    • Presenter : interface ์ •์˜ ์—†์ด ํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ์‚ฌ์šฉ
    • View : interface View์„ ์ƒ์†๋ฐ›์•„์„œ ์ •์˜
  • PresenterImpl์„ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•

    • Presenter : Presenter์™€ View์— ๋Œ€ํ•œ interface์„ ๊ตฌํ˜„
    • PresenterImpl : Presenter์„ ์ƒ์†๋ฐ›์•„์„œ ๊ตฌํ˜„
    • View : Presenter.View์„ ์ƒ์†๋ฐ›์•„์„œ ๊ตฌํ˜„

Google android-architecture ์˜ ๊ตฌ์„ฑ

  • Activity

    • ์‹ค์งˆ์ ์œผ๋กœ View์™€ Presenter๋ฅผ ์ƒ์„ฑํ•˜์—ฌ, ์—ฐ๊ฒฐํ•ด์ฃผ๋Š” ๊ณณ์œผ๋กœ, ๋‘ ์—ญํ• ์˜ ๋งค๊ฐœ์ฒด๊ฐ€ ๋˜๋Š” ๊ณณ์ž…๋‹ˆ๋‹ค.
  • Fragment

    • MVP ์ค‘ View์— ํ•ด๋‹นํ•˜๋Š” ๊ณณ์ž…๋‹ˆ๋‹ค.
    • Contract.View ์ธํ„ฐํŽ˜์ด์Šค์— ๋Œ€ํ•œ ๊ตฌํ˜„์ฒด์ž…๋‹ˆ๋‹ค.

    ์™œ Activity์™€ Fragment๋ฅผ ๊ฐ™์ด ์“ฐ๋‚˜์š” ?

    • ๊ฐ™์ด ์‚ฌ์šฉํ•˜๋ฏ€๋กœ์จ, MVP ๊ตฌํ˜„์— ์žˆ์–ด ๋” ๋‚˜์€ ๋ถ„๋ฆฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, Fragment์™€ Presenter ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ ๋ฐ ์—ฐ๊ฒฐํ•˜๋Š” ์ฝ”๋“œ๊ฐ€ Fragment, Presenter ํด๋ž˜์Šค์— ์œ„์น˜ํ•˜๊ฒŒ ํ•˜์ง€ ์•Š์•„, ๊ฐ Fragment, Presenter ํด๋ž˜์Šค๋Š” ์ž์‹ ์˜ ์—ญํ• ์— ๋Œ€ํ•œ ๊ตฌํ˜„์—๋Œ€ํ•œ ๋‚ด์šฉ๋งŒ ์ž‘์„ฑ๋  ์ˆ˜ ์žˆ๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค.
    • Fragment๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์—ฌ๋Ÿฌ๊ฐœ์˜ UI, ๋ถ€๋ถ„์ ์ธ UI๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค.
  • Contract

    • View์™€ Presenter์˜ ๊ด€๊ณ„๋ฅผ ์ •์˜ํ•˜๋Š” ๊ณณ์ž…๋‹ˆ๋‹ค.
      • View ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ์ƒ์†๋ฐ›๋Š” BaseView ์ธํ„ฐํŽ˜์ด์Šค๋Š” Presenter๋ฅผ ๋ฉค๋ฒ„๋กœ ๊ฐ–๊ณ  ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋กœ์จ V์™€ P์˜ 1:1 ๊ด€๊ณ„์— ๋Œ€ํ•œ ์ •์˜๊ฐ€ ์ด๋ค„์ง‘๋‹ˆ๋‹ค.
    • Contract ์ธํ„ฐํŽ˜์ด์Šค ์•ˆ์—, View์™€ Presenter ์˜ ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ์ž‘์„ฑ๋ฉ๋‹ˆ๋‹ค.
  • Presenter

    • MVP ์ค‘ Presenter์— ํ•ด๋‹นํ•˜๋Š” ๊ณณ์ž…๋‹ˆ๋‹ค.
    • Contract.Presenter ์ธํ„ฐํŽ˜์ด์Šค์— ๋Œ€ํ•œ ๊ตฌํ˜„์ฒด์ž…๋‹ˆ๋‹ค.
  • Data ํด๋” : repository ( including local, remote ๋“ฑ๋“ฑ)

    • MVP ์ค‘ Model์— ํ•ด๋‹นํ•˜๋Š” ๊ณณ์ž…๋‹ˆ๋‹ค.

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


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

android/architecture-samples


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

  • Model๊ณผ View๋ฅผ ๋ถ„๋ฆฌ์‹œ์ผœ, ๊ฒฐํ•ฉ๋„๋ฅผ ์—†์•จ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    • Model ๊ด€๋ จ ์ฒ˜๋ฆฌ๋“ค์€ Presenter๋ฅผ ํ†ตํ•ด์„œ ์ด๋ค„์ง€๊ธฐ ๋•Œ๋ฌธ์—, View๋Š” Model์— ๋Œ€ํ•ด ์•Œํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.
  • MVP ๊ฐ ์—ญํ• ์— ๋Œ€ํ•œ ์ถ”์ƒํ™”๊ฐ€ ๊ฐ€๋Šฅํ•˜๊ณ  ์ด๋ฅผ ํ†ตํ•ด, ๋ถ„๋ฆฌ๊ฐ€ ๊น”๋”ํ•˜๊ฒŒ ์ด๋ค„์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    • ํ•œ ํด๋ž˜์Šค์˜ ์ฝ”๋“œ๊ฐ€ ๋งค์šฐ ๋ฐฉ๋Œ€ํ•ด์ง€๋Š” ๊ฒƒ์„ ๋ง‰์„ ์ˆ˜ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

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

  • Presenter์™€ View๋Š” 1:1 ๊ด€๊ณ„๋ฅผ ๊ฐ€์ ธ์•ผํ•˜๋ฏ€๋กœ, ๋‘˜ ๊ฐ„์˜ ๊ฒฐํ•ฉ๋„๊ฐ€ ๋†’์•„์ง‘๋‹ˆ๋‹ค.

๋ ˆํผ๋Ÿฐ์Šค

android/architecture-samples

Android MVP ๋ฌด์ž‘์ • ๋”ฐ๋ผํ•˜๊ธฐ - Presenter/View ์ƒ์„ฑํ•˜๊ธฐ

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

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