MVC pattern on Android

2018-12-31 23:29发布

Is it possible to implement the model–view–controller pattern in Java for Android?

Or is it already implemented through Activities? Or is there a better way to implement the MVC pattern for Android?

21条回答
公子世无双
2楼-- · 2018-12-31 23:40

Although this post seems to be old, I'd like to add the following two to inform about the recent development in this area for Android:

android-binding - Providing a framework that enabes the binding of android view widgets to data model. It helps to implement MVC or MVVM patterns in android applications.

roboguice - RoboGuice takes the guesswork out of development. Inject your View, Resource, System Service, or any other object, and let RoboGuice take care of the details.

查看更多
宁负流年不负卿
3楼-- · 2018-12-31 23:40

Model View Controller (MVC)

enter image description here


Description:

  • When we have to main large projects in the software development, MVC is generally used because it’s a universal way of organizing the projects.
  • New developers can quickly adapt to the project
  • Helps in development of big projects and cross platform too.

The MVC pattern is essentially this:

  • Model: What to display. This can be the data source (Ex: Server, Raw data in the app)
  • View: How it’s displayed. This can be the xml. It is thus acting as a presentation filter. A view is attached to its model (or model part) and gets the data necessary for the presentation.
  • Controller: Handling events like user input. This be the activity

Important feature of MVC: We can modify Either the Model or View or Controller still not affecting the other ones

  • Say we change the color in the view, size of the view or the position of the view. By doing so it won’t affect the model or the controller
  • Say we change the model (instead of data fetched from the server fetch data from assets ) still it won’t affect the view and controller
  • Say we change the Controller(Logic in the activity) it won’t affect the model and the view
查看更多
无与为乐者.
4楼-- · 2018-12-31 23:41

When we apply MVC, MVVM, or Presentation Model to an Android app, what we really want is to have a clear structured project and more importantly easier for unit tests.

At the moment, without a third-party framework, you usually have lots of code (like addXXListener(), findViewById(), etc.), which does not add any business value.

What's more, you have to run Android unit tests instead of normal JUnit tests, which take ages to run and make unit tests somewhat impractical. For these reasons, some years ago we started an open source project, RoboBinding - A data-binding Presentation Model framework for the Android platform.

RoboBinding helps you write UI code that is easier to read, test and maintain. RoboBinding removes the need of unnecessary code like addXXListener or so, and shifts UI logic to Presentation Model, which is a POJO and can be tested via normal JUnit tests. RoboBinding itself comes with more than 300 JUnit tests to ensure its quality.

查看更多
后来的你喜欢了谁
5楼-- · 2018-12-31 23:44

After some searching, the most reasonable answer is the following:

MVC is already implemented in Android as:

  1. View = layout, resources and built-in classes like Button derived from android.view.View.
  2. Controller = Activity
  3. Model = the classes that implement the application logic

(This by the way implies no application domain logic in the activity.)

The most reasonable thing for a small developer is to follow this pattern and not to try to do what Google decided not to do.

PS Note that Activity is sometimes restarted, so it's no place for model data (the easiest way to cause a restart is to omit android:configChanges="keyboardHidden|orientation" from the XML and turn your device).

EDIT

We may be talking about MVC, but it will be so to say FMVC, Framework--Model--View--Controller. The Framework (the Android OS) imposes its idea of component life cycle and related events, and in practice the Controller (Activity/Service/BroadcastReceiver) is first of all responsible for coping with these Framework-imposed events (such as onCreate()). Should user input be processed separately? Even if it should, you cannot separate it, user input events also come from Android.

Anyway, the less code that is not Android-specific you put into your Activity/Service/BroadcastReceiver, the better.

查看更多
低头抚发
6楼-- · 2018-12-31 23:44

The best resource I found to implement MVC on Android is this post:

I followed the same design for one of my projects, and it worked great. I am a beginner on Android, so I can't say that this is the best solution.

I made one modification: I instantiated the model and the controller for each activity in the application class so that these are not recreated when the landscape-portrait mode changes.

查看更多
听够珍惜
7楼-- · 2018-12-31 23:45

There is no universally unique MVC pattern. MVC is a concept rather than a solid programming framework. You can implement your own MVC on any platform. As long as you stick to the following basic idea, you are implementing MVC:

  • Model: What to render
  • View: How to render
  • Controller: Events, user input

Also think about it this way: When you program your model, the model should not need to worry about the rendering (or platform specific code). The model would say to the view, I don't care if your rendering is Android or iOS or Windows Phone, this is what I need you to render. The view would only handle the platform-specific rendering code.

This is particularly useful when you use Mono to share the model in order to develop cross-platform applications.

查看更多
登录 后发表回答