* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:重构BMI应用程序
* 作 者: 雷恒鑫
* 完成日期: 2012 年 08 月 08 日
* 版 本 号: V1.0
* 对任务及求解方法的描述部分
* 输入描述:
* 问题描述:
* 程序输出:
* 程序头部的注释结束
*/
“MVC”模式指的是将应用程序的组成分成3个主要部分。分别是存储内容的“Model”(模型)、负责显示的“View”(视图)、控制程序操作的“Controller”(控制器)。编写时将这3个相对独立又彼此相关的部分区隔开来,有助于程序的设计与维护。
以下是利用“MVC”模式修改前的完整的BMI程序:
Bmi.java
package com.demo.android.bmi;
import java.text.DecimalFormat;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Bmi extends Activity {
/**
* Called when the activity is first created.
*
* @param <calcBMI>
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Listen for button clicks
Button button = (Button) findViewById(R.id.submit);
button.setOnClickListener(calcBMI);
}
private OnClickListener calcBMI = new OnClickListener() {
public void onClick(View v) {
DecimalFormat nf = new DecimalFormat("0.00");
EditText fieldheight = (EditText) findViewById(R.id.height);
EditText fieldweight = (EditText) findViewById(R.id.weight);
double height = Double
.parseDouble(fieldheight.getText().toString()) / 100;
double weight = Double
.parseDouble(fieldweight.getText().toString());
double BMI = weight / (height * height);
TextView result = (TextView) findViewById(R.id.result);
result.setText("Your BMI is " + nf.format(BMI));
// Give health advice
TextView fieldsuggest = (TextView) findViewById(R.id.suggest);
if (BMI > 25) {
fieldsuggest.setText(R.string.advice_heavy);
} else if (BMI < 20) {
fieldsuggest.setText(R.string.advice_light);
} else {
fieldsuggest.setText(R.string.advice_average);
}
}
};
}
①首先,可以应用“MVC模式”,将声明界面组件(按钮、数字字段)、指定负责函数等动作提取出来,将“onCreate”函数改写如下:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViews();
setListensers();
// Listen for button clicks
//Button button = (Button) findViewById(R.id.submit);
//button.setOnClickListener(calcBMI);
}
②把声明与查找界面组件和为特定界面组件添加控制流程的两段程序代码,分开整理成两段函数。将“声明与查找界面组件”的部分写成一个独立的“findView”函数:
private Button button_calc;
private EditText field_height;
private EditText field_weight;
private void findViews(){
button_calc = (Button)findViewById(R.id.submit);
field_height = (EditText)findViewById(R.id.height);
field_weight = (EditText)findViewById(R.id.weight);
}
③也可以顺便将很没个性的按钮“button”改名为“calcbutton”,以后再程序中一看到“calcbutton”,就知道是一个按下后将开始进行计算工作的按钮。
④同样滴,也可以将“为特定界面组件添加控制流程”(按钮)的负责函数独立出来:
// Listen for button clicks
private void setListensers(){
button_calc.setOnClickListener(calcBMI);
}
通过以上4步,我们就将程序逻辑与界面组件的声明分离开来,达到重构的目的。
下面是通过“MVC模式”重构后的完整的Bmi.java程序代码:
package com.demo.android.bmi;
import java.text.DecimalFormat;
import android.app.Activity;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Bmi extends Activity {
/**
* Called when the activity is first created.
*
* @param <calcBMI>
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViews();
setListensers();
// Listen for button clicks
//Button button = (Button) findViewById(R.id.submit);
//button.setOnClickListener(calcBMI);
}
private Button button_calc;
private EditText field_height;
private EditText field_weight;
private TextView view_result;
private TextView view_suggest;
private void findViews(){
button_calc = (Button)findViewById(R.id.submit);
field_height = (EditText)findViewById(R.id.height);
field_weight = (EditText)findViewById(R.id.weight);
view_result = (TextView)findViewById(R.id.result);
view_suggest = (TextView)findViewById(R.id.suggest);
}
// Listen for button clicks
private void setListensers(){
button_calc.setOnClickListener(calcBMI);
}
private OnClickListener calcBMI = new OnClickListener() {
public void onClick(View v) {
DecimalFormat nf = new DecimalFormat("0.00");
EditText fieldheight = (EditText) findViewById(R.id.height);
EditText fieldweight = (EditText) findViewById(R.id.weight);
double height = Double
.parseDouble(fieldheight.getText().toString()) / 100;
double weight = Double
.parseDouble(fieldweight.getText().toString());
double BMI = weight / (height * height);
//TextView result = (TextView) findViewById(R.id.result);
//result.setText("Your BMI is " + nf.format(BMI));
//Present result
view_result.setText(getText(R.string.bmi_result)+nf.format(BMI));
// Give health advice
TextView fieldsuggest = (TextView) findViewById(R.id.suggest);
if (BMI > 25) {
fieldsuggest.setText(R.string.advice_heavy);
} else if (BMI < 20) {
fieldsuggest.setText(R.string.advice_light);
} else {
fieldsuggest.setText(R.string.advice_average);
}
}
};
}
运行结果: