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:54

I think the most useful simplified explanation is here: http://www.cs.otago.ac.nz/cosc346/labs/COSC346-lab2.2up.pdf

From everything else I've seen and read here, implementing all these things makes it harder and does not fit in well with other parts of android.

Having an activity implement other listeners is already the standard Android way. The most harmless way would be to add the Java Observer like the slides describe and group the onClick and other types of actions into functions that are still in the Activity.

The Android way is that the Activity does both. Fighting it doesn't really make extending or doing future coding any easier.

I agree with the 2nd post. It's sort of already implemented, just not the way people are used to. Whether or not it's in the same file or not, there is separation already. There is no need to create extra separation to make it fit other languages and OSes.

查看更多
何处买醉
3楼-- · 2018-12-31 23:54

I have seen that many people are saying MVC is already implemented in Android, but it's not true. Android follows no MVC by default.

Because i don't Google will ever forcefully impose the restrictions of an MVC implementation like iPhone, but its upto the developers which patteren or technique they want in their project, In small or simple applications use of MVC is not required, but as the application grows and get complicated and require modification's of its code in later years, then there comes a need of the MVC pattern in Android.

It provides an easy way to modify code and also helps in reduction of issues. If you would like to implement MVC on Android, then follow this below given link and enjoy the MVC implementation in your project.

http://www.therealjoshua.com/2011/11/android-architecture-part-1-intro/

But nowadays i think MVP along with Android Architectural Pattern is one of the best option developers should use for a clean and robust android applications.

查看更多
泪湿衣
4楼-- · 2018-12-31 23:56

Android UI creation using layouts, resources, activities and intents is an implementation of the MVC pattern. Please see the following link for more on this - http://www.cs.otago.ac.nz/cosc346/labs/COSC346-lab2.2up.pdf

mirror for the pdf

查看更多
牵手、夕阳
5楼-- · 2018-12-31 23:59

In Android you don't have MVC, but you have the following:

  • You define your user interface in various XML files by resolution, hardware, etc.
  • You define your resources in various XML files by locale, etc.
  • You extend clases like ListActivity, TabActivity and make use of the XML file by inflaters.
  • You can create as many classes as you wish for your business logic.
  • A lot of Utils have been already written for you - DatabaseUtils, Html.
查看更多
姐姐魅力值爆表
6楼-- · 2019-01-01 00:00

MVC model

Model-View-Controller in Android In around 2011, when Android started to become more and more popular, architecture questions naturally appeared. Since MVC was one of the most popular UI patterns at that time, developers tried to apply it to Android too.

Model The Model represents a set of classes that describe the business logic i.e. business model as well as data access operations i.e. data model. It also defines business rules for data means how the data can be changed and manipulated.

View The View represents the UI components. It is only responsible for displaying the data that is received from the controller as the result. This also transforms the model(s) into UI.

Controller The Controller is responsible to process incoming requests. It receives input from users via the View, then process the user’s data with the help of Model and passing the results back to the View. Typically, it acts as the coordinator between the View and the Model.

in other word

Models: Content Providers. Data Managers that are the recommended form of inter-application data sharing.

Views: Activities. This is the application's primary user interface component. Every individual screen of an Android application is derived from the Activity Java class (android.app.Activity). They are containers for Views (android.view.View).

Controllers: Services. These are background components that behave like UNIX daemons and Windows services. They run invisibly and perform ongoing unattended processing.

查看更多
余欢
7楼-- · 2019-01-01 00:01

Being tired of the MVx disaster on Android I've recently made a tiny library that provides unidirectional data flow and is similar to the concept of MVC: https://github.com/zserge/anvil

Basically, you have a component (activity, fragment, and viewgroup). Inside you define the structure and style of the view layer. Also you define how data should be bound to the views. Finally, you can bind listeners in the same place.

Then, once your data is changed - the global "render()" method will be called, and your views will be smartly updated with the most recent data.

Here's an example of the component having everything inside for code compactness (of course Model and Controller can be easily separated). Here "count" is a model, view() method is a view, and "v -> count++" is a controller which listens to the button clicks and updates the model.

public MyView extends RenderableView {
  public MyView(Context c) {
      super(c);
  }

  private int count = 0;

  public void view() {
    frameLayout(() -> {              // Define your view hierarchy
      size(FILL, WRAP);
      button(() -> {
          textColor(Color.RED);      // Define view style
          text("Clicked " + count);  // Bind data
          onClick(v -> count++);     // Bind listeners
      });
    });
  }

With the separated model and controller it would look like:

button(() -> {
   textColor(Color.RED);
   text("Clicked " + mModel.getClickCount());
   onClick(mController::onButtonClicked);
});

Here on each button click the number will be increased, then "render()" will be called, and button text will be updated.

The syntax becomes more pleasant if you use Kotlin: http://zserge.com/blog/anvil-kotlin.html. Also, there is alternative syntax for Java without lambdas.

The library itself is very lightweight, has no dependencies, uses no reflection, etc.

(Disclaimer: I'm the author of this library)

查看更多
登录 后发表回答