MVP Architecture

Updated:

MVP(Model View Presenter)

  • MVP : Model-View-Presenter 는 MVC(Model-View-Controller) 아키텍처 패턴에서 파생 된 것으로, 사용자 인터페이스를 개발하기 위해서 대부분 사용된다.

    자동화된 단위 테스트를 용이하게 하고, presentation 로직에서 SOC(관심의 분리)를 개선하도록 설계된 사용자 인터페이스 아키텍처 패턴이다.

    협업시 각각 분야에서만 작업을 할 수 있으며, 소스 파일 관리가 편해지고, 유닛테스트에 용이하여 전체적 유지보수가 쉽다.

  • Model : 내부적으로 쓰는 data를 저장, 처리 하는 등의 역할, 다른 어떤 요소에도 의존적이지 않은 독립적인 영역.

    public class Comment implements Parcelable {
        String id;
        String writer;
        String movieId;
        String timestamp;
        String contents;
      
        public Comment(String id, String writer, String movieId, String timestamp, String contents) {
            this.id = id;
            this.writer = writer;
            this.movieId = movieId;
            this.timestamp = timestamp;
            this.contents = contents;
        }
      ...
    }
    
  • View : 실제 View에 대한 직접적 접근 담당, View에서 발생하는 이벤트는 Presenter에 위임하여 처리. View interface를 구현하여 Presenter에서 코드를 만들 interface를 갖게 만듬. 이를 통해 특정 View와 결합되지 않고 가상의 View를 구현하여 간단히 Unit test를 실행 할 수 있다.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comment);
      
        ButterKnife.bind(this);
        commentPresenter = new CommentPresenter(this);
        commentPresenter.attachView(this);
    }
    
  • Presenter : MVC의 컨트롤러와 비슷하지만, View에 연결되는게 아니라 Interface에 연결된다. Presenter가 역할을 정의 하면, View와 Model 사이에서 data 전달 역할을 함. MVC가 가진 test 문제와 모듈화/유연성 문제 해결 가능

    public class commentPresenter implements CommentContractor.Presenter{
      
        private CommentContractor.View view;
        public MainPresenter(MainContractor.View view) {
            this.view = view;
        }
          
        @Override
        public void attachView(MainContractor.View view) {
            this.view = view;
        }
      
        @Override
        public void detachView() {view = null;}
    }
    
  • MVP 실행 순서 - 출처