This is MVP pattern testing with Swift.

Overview

sample-swift-mvp

MVP 패턴에 대한 이해를 높이기 위해서 샘플 코드 작성 및 기록

MVP 패턴

MVP(Model-View-Presenter) 패턴은 소프트웨어 아키텍처 패턴이다

M (Model)
- 데이터 모델
- 실질적인 데이터

V (View)
- MVC의 View와 ViewController를 모두 포함
- 사용자에게 보여지는 UI요소

P (Presenter)
- UIKit과 관련이 없는 로직들을 수행
- 사용자 응답에 반응하거나 UI를 업데이트하는 일을 담당
- View에서 요청한 정보를 Model로 부터 가공해서 View로 전달하는 부분

MVVM 상세

흐름도

  1. View로 사용자의 입력이 들어온다
  2. View는 Presenter에 작업 요청을 합니다
  3. Presenter에서 필요한 데이터를 Model에 요청한다
  4. Model은 Presenter에 필요한 데이터를 응답합니다
  5. Presenter는 View에 데이터를 응답합니다
  6. View는 Presenter로부터 받은 데이터로 화면에 보여주게 됩니다

스크린샷 2021-11-11 오전 1 45 31


샘플 코드 구성

첫번째 샘플의 경우 빨, 초, 노 버튼을 누르면 Label을 통해서 해당 버튼에 대한 Text를 출력

구조 실행 전 (첫번째 셈플)
스토리보드
실행 후 (첫번째 셈플)

생각

  1. Presenter 의 경우 View 에서 강한참조로 가지고 있으며, Presenter 에서는 Delegate를 통해서 약한 참조를 통해 가지고 있기 때문에 ViewPresenter 가 완벽히 독립되어있다고는 할 수 없다.

  2. MVVM 에서는 ViewModelView 에서 가지고 있으며, ViewModel 은 단지 Model 을 가지고 있으며 View 의 명령을 수행하며 Model 을 변경시키기만 하는데, 단방향이라고 했던 점을 MVP 를 학습하면서 완전하게 이해가 되었다.

  3. MVC 보다는 각 구성 간의 독립성이 높아져서, Test Coverage가 높아질 것 같다

  4. Presenter 에 대한 테스트를 어떻게 진행해야될지 고민이다. View의 역할을 하는 Mock 객체를 만들어줘야 하는 것인지 모르겠다. MVP 구조에서의 Test 예제가 너무 없다.


링크

  • MVC, MVP, MVVM 비교 : 기존에 적용해 본 경험이 있는 MVC, MVVM과 같이 봐서 이해가 더 빠르다. 좋은 글이다.
  • MVP 아키텍쳐 특징 : Raywenderich, youtube, 각종 블로그 글에 나오는 전형적인 MVP 패턴 예제이다. 깔끔하다
You might also like...
WebDomHandling - A Swift Package for handling JavaScript code between WebKit and Swift implemented by WebKit

WebDomHandling A Swift Package for handling JavaScript code between WebKit and S

A very simplistic state machine system for Swift while mainly used with Raylib on Swift

A very simplistic state machine system for Swift while mainly used with Raylib on Swift

A Swift SPM framework for running and managing Lua code from Swift

LuaKit A Swift Package for running and managing Lua code from Swift. Documentation For documentation, add this package as Swift Package Dependency, an

This is a Swift Package bundling different Train APIs into one simple Swift interface.

This is a Swift Package bundling different Train APIs into one simple Swift interface.

Beak 🐦 Peck into your Swift files from the command line

Beak 🐦 Peck into your Swift files from the command line Beak can take a standard Swift file and then list and run any public global functions in it v

A Collection of PropertyWrappers to make custom Serialization of Swift Codable Types easy

CodableWrappers Simplified Serialization with Property Wrappers Move your Codable and (En/De)coder customization to annotations! struct YourType: Coda

Turn your Swift data model into a working CRUD app.
Turn your Swift data model into a working CRUD app.

Model2App is a simple library that lets you quickly generate a CRUD iOS app based on just a data model defined in Swift. (CRUD - Create Read Update De

OpenAPI/Swagger 3.0 Parser and Swift code generator

SwagGen SwagGen is a library and command line tool for parsing and generating code for OpenAPI/Swagger 3.0 specs, completely written in Swift. Swagger

Homebrew for Swift packages

Swiftbrew A package manager that installs prebuilt Swift command line tool packages, or Homebrew for Swift packages. Installation Homebrew brew instal

Owner
Youngwoo Lee
iOS App developer 💻
Youngwoo Lee
Contacts is an iOS app based on MVP (Model View Presenter) software architectural pattern.

Contacts Description Contacts is an iOS app based on MVP (Model View Presenter) software architectural pattern. Run Requirements Xcode 10.2.1 Swift 5.

Tirupati Balan 4 Apr 26, 2022
iOS architectures - MVC, MVP, MVVM, MVVM-C, ReactorKit, VIPER, Clean Architecture

iOS architectures - MVC, MVP, MVVM, MVVM-C, ReactorKit, VIPER, Clean Architecture, RIBs; Repository Pattern, Rxflow, Swinject, Tuist, Xcodegen, Cocoapods, SPM, Carthage + Rome

null 123 Dec 21, 2022
A Swift package that provides convenient Lorem Ipsum text, images, colors and other placeholders for rapidly prototyping, building and testing your iOS applications.

Lorem Introducing Lorem, a placeholder generator library for iOS to help you rapidly prototype, build and test your iOS applications. By leveraging Sw

Thirdfort Limited 10 Dec 5, 2022
A testing MQTT react native library

react-native-awesome-testing abc Installation npm install react-native-awesome-testing Usage import { multiply } from "react-native-awesome-testing";

null 0 Nov 26, 2021
Repository to help people learning testing

Testing Training The purpose of this repository is to help people learn the basics of testing in Swift. I decided to prepare this repository because m

Riccardo 1 Dec 31, 2021
The most powerful Event-Driven Observer Pattern solution the Swift language has ever seen!

Event-Driven Swift Decoupling of discrete units of code contributes massively to the long-term maintainability of your project(s). While Observer Patt

Flowduino 4 Nov 14, 2022
A navigation frameword based on the Coordinator pattern and is a compact version from XCoordinator.

Coordinator A navigation frameword based on the Coordinator pattern and is a compact version from XCoordinator. Example To run the example project, cl

Duc Pham 3 Jul 9, 2022
Quizzler - iOS App Design Pattern(MVC) and Code Structuring

Quizzler iOS App Design Pattern(MVC) and Code Structuring What you will learn Ho

null 0 Jan 10, 2022
A Swift package for encoding and decoding Swift Symbol Graph files.

SymbolKit The specification and reference model for the Symbol Graph File Format. A Symbol Graph models a module, also known in various programming la

Apple 141 Dec 9, 2022
Delightful code generation for OpenAPI specs for Swift written in Swift

Create API Delightful code generation for OpenAPI specs for Swift written in Swi

Alexander Grebenyuk 286 Dec 23, 2022