官术网_书友最值得收藏!

MVC (Model View Controller)

This is one of the most widely used approaches in software development. The MVC consists of three main components:

  • Model: The model represents the object in the application. This has the logic of where the data is to be fetched from. This can also have the logic by which the controller can update the view. In Android, the model is mostly represented by object classes.
  • View: The view consists of the components that can interact with the user and is responsible for how the model is displayed in the application. In Android, the view is mostly represented by the XML where the layouts can be designed.
  • Controller: The controller acts as a mediator between the model and the view. It controls the data flow into the model object and updates the view whenever data changes. In Android, the controller is mostly represented by the activities and fragments.

All of these components interact with each other and perform specific tasks, as shown in the following figure:

Figure 2.2.1

It has been noticed that Android is not able to follow the MVC architecture completely, as activity/fragment can act as both the controller and the view, which makes all the code cluttered in one place. Activity/fragment can be used to draw multiple views for a single screen in an app, thus all different data calls and views are populated in the same place. Therefore, to solve this problem, we can use different design patterns or can implement MVC carefully by taking care of conventions and following proper programming guidelines.

The following shows a basic example of how MVC is used in Android :

Model:

public class LocationItem {
String name;
String address;
public LocationItem(String name, String address) {
this.name = name;
this.address = address;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
}

View:

<TextView
android:id="@+id/name"
style="@style/HorizontalPlaceTitleTxtStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

Controller:

TextView name = (TextView)findViewById(R.id.name);
name.setText(LocationUtil.getLocationName(list.get(position)));
//Function In LocationUtil(Controller Class)
public class LocationUtil {
public static String getLocationName(LocationItem item) {
return item.getName();
}
}

To explain the MVC pattern components shown in the preceding code; the model here is LocationItem, which holds the name and address of the location. The view is an XML file, which contains a TextView, through which the name of the location can be displayed. The activity, which is the controller, contains a LocationItem. It gets the name of the LocationItem at a specific position in the list and sets it up in the view, which displays it.

主站蜘蛛池模板: 长治县| 吉木萨尔县| 高淳县| 买车| 乌兰浩特市| 达拉特旗| 金沙县| 云安县| 高阳县| 桂林市| 永新县| 湘潭市| 江阴市| 四会市| 夏邑县| 济南市| 樟树市| 禄劝| 贺州市| 黄冈市| 无棣县| 治县。| 安阳市| 民丰县| 道孚县| 四子王旗| 驻马店市| 磴口县| 德江县| 安康市| 远安县| 辽中县| 长沙县| 札达县| 临江市| 万州区| 苗栗县| 拜泉县| 湖州市| 龙陵县| 石林|