Upgrade to Pro — share decks privately, control downloads, hide ads and more …

MVVM

 MVVM

Takuma Shimizu

June 24, 2023
Tweet

More Decks by Takuma Shimizu

Other Decks in Programming

Transcript

  1. ͜Ε͔ΒMVVMʹ͍ͭͯΘ͍Θ͍͢Δલʹ

    MVVM͕ੜΈग़͞ΕͨܦҢΛ௥͍

    ͦΕ͕੒͠ಘͨ͜ͱΛ஌Γ

    Έͳ͞Μͷٞ࿦ͷϕʔεΛͭ͘Γ͍ͨ

    View full-size slide

  2. MVVMʹࢸΔ·Ͱ
    w 5.7& ݪॳ.7$

    w .7$
    w .7$"QQMJDBUJPO.PEFM
    w ݪॳ.71
    w .71
    w .7$1SFTFOUBUJPO.PEFM
    w.77.
    w .717.
    w ʜ
    Cocoa MVC͸ग़ͯ͜ͳ͍Αʂ
    👉

    View full-size slide

  3. TMVE (ݪॳMVC)
    w ϓϨθϯςʔγϣϯ (6*දݱ
    ͱϞσϧ σʔλͱͦͷৼΔ෣͍
    ͷ෼཭
    w ύλʔϯԽ͞Εͨ.7$ͱޙଓͷύλʔϯ (6*ΞʔΩςΫνϟʔ
    ʹڧ͘Ө
    ڹΛ༩͑ͨ

    View full-size slide

  4. MVC͕੒͠ಘͨ͜ͱ
    w ϓϨθϯςʔγϣϯͱϞσϧͷ෼཭Λ͞Βʹൃల
    w 0CTFSWFSύλʔϯʹΑΔσʔλͷಉظํ๏Λཱ֬

    View full-size slide

  5. MVC͕࢒ͨ͠՝୊
    w 7JFXͷϩδοΫͱঢ়ଶͷஔ͖৔ॴ͕ෆ໌ྎ
    w ౰࣌ͷ(6*͸୯७Ͱݱ୅΄ͲϦονͰ͸ͳ͔ͬͨͨΊɺେ͖ͳ໰୊ʹ͸
    ͳΒͳ͔ͬͨ
    w ˠ"QQMJDBUJPO.PEFM

    View full-size slide

  6. MVC + Application Model
    • View - ModelؒͰͷ௚઀తͳมߋͷ؂ࢹɾ௨஌͔Β

    • View - Application Model - ModelʹΑΔؒ઀తͳ؂ࢹɾ௨஌΁

    • Application Model͸Modelͱͯ͠ͷଆ໘͕ڧ͍

    • View - Modelؒͷσʔλͷදݱม׵΋ߦ͏

    • → ViewͷϩδοΫͱঢ়ଶͷஔ͖৔ॴͱͯ͠ػೳ

    View full-size slide

  7. Application Modelͷ՝୊
    • Observerύλʔϯ͕ద༻Ͱ͖Δͷ͸ɺModelʹଘࡏ͢ΔσʔλͷΈ

    • Viewಠࣗͷঢ়ଶ͸ɺObserverύλʔϯʹΑΔ؂ࢹɾ௨஌Λద༻Ͱ͖ͳ͍

    • ModelతͳଘࡏͰ͋ΔApplication Model͕௚઀ViewΛ͍͡Δ͜ͱʹ…

    • ྫɿview.textField.textColor = .red

    Λ௚઀࣮ߦ͢ΔΑ͏ͳ΋ͷ

    • ObserverύλʔϯʹΑΔ੍໿ΛಀΕɺApplication Model͕ංେԽ

    View full-size slide

  8. ݪॳMVP
    • MVCͷਖ਼౷ਐԽͷงғؾ

    • ݱ୅ͷMVPͱ͸ҟͳΔඇৗʹڵຯਂ͍ߏ੒

    • Application Modelͷ՝୊ղܾͱ͸͋·Γؔ܎ͳ͍ͷͰׂѪ

    • ౰વɺݱ୅ͷMVPʹ͸େ͖ͳӨڹΛ༩͍͑ͯΔ

    View full-size slide

  9. MVP
    • MVVMͱฒΜͰApplication Modelͷ՝୊Λղܾͨ͠Ұͭͷύλʔϯ

    • MVVMʹӨڹΛ༩͑ͨΘ͚Ͱ͸ͳͦ͞͏ͳͷͰׂѪ

    View full-size slide

  10. MVP
    • View - Modelؒͷ؂ࢹɾ௨஌ͷؔ܎͕෮׆

    • ౰࣌͸Passive Viewͷ֓೦͸ͳ͔ͬͨͱࢥΘΕΔ

    • Application Model͔ΒModelدΓͷཁૉ͕ෆཁʹ

    • → Application Model͔ΒObserverύλʔϯͷద༻͕ͳ͘ͳΓPresenter΁
    ͱมԽ

    • Presenter͸ɺViewϩδοΫͱঢ়ଶ؅ཧʹઐ೦Ͱ͖ΔΑ͏ʹͳΓɺViewͱ
    ͷ࿈ܞ͕ີʹऔΕΔΑ͏ʹͳͬͨ

    View full-size slide

  11. MVPͷิ଍
    • Controllerͷ໾ׂ͸Viewʹٵऩ

    • MVC͕ߟҊ͞Εͨ౰࣌͸ɺϢʔβʔೖྗ͸௿ϨϕϧͳσʔλදݱͰ͋
    ΓɺController͕ҙຯͷ͋ΔΞΫγϣϯʹม׵͢Δඞཁ͕͋ͬͨ

    • iOSͰྫ͑ΔͱɺUIPanGestureRecognizerͷΠϕϯτΛࡹ͘Α͏ͳ΋
    ͷʁ

    • ·ͨ͸ɺϨεϙϯμʔνΣʔϯͷࢧԉ͕ͳ͍ঢ়ଶʁ

    View full-size slide

  12. • ViewͷϩδοΫͱঢ়ଶΛอ࣋͢ΔϞσϧ

    • Application Modelͱ͸ҟͳΓɺView͸௚઀อ࣋ͨ͠Γߋ৽ͨ͠Γ͠ͳ͍

    • Model(υϝΠϯϨΠϠʔ)ͱ΍ΓͱΓΛߦ͏

    • ViewͷมԽ͠͏Δશঢ়ଶΛอ࣋͠ɺData BindingΛ௨ͯ͡ಉظ͢Δ

    • Data Binding΁۩ମతͳݴٴ͸͋·Γ͞Ε͍ͯͳ͍

    • which I'm hoping will happen some day with technologies like .NET's
    data binding.
    Presentation Model

    View full-size slide

  13. Presentation Model͕੒͠ಘͨ͜ͱ
    w 0CTFSWFSύλʔϯʹΑΔ7JFX.PEFMؒͷͭͳ͕ΓΛઈͬͨ
    w "QQMJDBUJPO.PEFM͔Β0CTFSWFSύλʔϯͷద༻͕ͳ͘ͳΓɺ

    %BUB#JOEJOHΛ༻͍Δ7JFX.PEFM΁ͱมԽ
    w "QQMJDBUJPO.PEFMͷஔ͖׵͕͑ओ໨త

    View full-size slide

  14. MVVM
    w 1SFTFOUBUJPO.PEFMΛΑΓ۩ݱԽͨ͠ύλʔϯ
    w 7JFX.PEFM͸ɺ
    w 6*ύʔπΛ࠶ར༻͢ΔͨΊͷ7JFXͷந৅දݱ
    w %BUB#JOEJOHΛ௨ͯ͡7JFX͔Βར༻͞ΕΔઐ༻ͷ.PEFM ૒ํ޲

    w .PEFMͷσʔλΛ7JFX༻ʹม׵
    w 7JFX͕.PEFMͱؒ઀తʹ΍ΓͱΓ͢Δ$PNNBOEΛඋ͑Δ

    View full-size slide

  15. MVVM͕࢒ͨ͠՝୊
    w 81'Λલఏͱ͍ͯ͠ΔͨΊɺ%BUB#JOEJOHʹؔ͢Δݴٴ͕9".-͚ͩͳ
    Ͳݶఆత
    w .JDSPTPGUͷதͷਓ͕ߟҊͨ͠ύλʔϯͳͷͰ౰વͱ͍͑͹౰વ

    View full-size slide

  16. MVVMͷิ଍
    • Viewͷ։ൃ͸ɺσβΠφʔͷ੹೚Ͱߦ͏

    • UIͷϏδϡΞϧ໘͕ԡ͠ग़͞Ε࢝Ίͨ࣌୅എܠ͕ؔ܎͔

    • Controllerͷ໾ׂ͸Viewʹٵऩ

    • MVC͕ߟҊ͞Εͨ౰࣌͸ɺϢʔβʔೖྗ͸௿ϨϕϧͳσʔλදݱͰ͋Γɺ
    Controller͕ҙຯͷ͋ΔΞΫγϣϯʹม׵͢Δඞཁ͕͋ͬͨ

    • iOSͰྫ͑ΔͱɺUIPanGestureRecognizerͷΠϕϯτΛࡹ͘Α͏ͳ΋ͷʁ

    • ·ͨ͸ɺϨεϙϯμʔνΣʔϯͷࢧԉ͕ͳ͍ঢ়ଶʁ

    View full-size slide

  17. ·ͱΊ
    w .7$ʮϓϨθϯςʔγϣϯͱϞσϧͷ෼཭ʯ
    w "QQMJDBUJPO.PEFMʮ7JFXͷϩδοΫͱঢ়ଶͷஔ͖৔ॴʯ
    w 1SFTFOUBUJPO.PEFMʮ%BUB#JOEJOHͷख๏ʯ
    w .77.ʮ1SFTFOUBUJPO.PEFMΛൃలɺύλʔϯԽʯ
    w ʮ7JFXͷϩδοΫͱঢ়ଶͷஔ͖৔ॴʯΛΊ͙Δྺ࢙
    w ಉظํ๏ͱͯ͠%BUB#JOEJOHΛݟग़ͨ͠

    View full-size slide