Android学习历程(3):用MVC进行重构

3,575 views
3,413 views

Published on

Published in: Technology, Business
1 Comment
2 Likes
Statistics
Notes
No Downloads
Views
Total views
3,575
On SlideShare
0
From Embeds
0
Number of Embeds
425
Actions
Shares
0
Downloads
105
Comments
1
Likes
2
Embeds 0
No embeds

No notes for slide

Android学习历程(3):用MVC进行重构

  1. 1. Android 学习历程(3):用 MVC 进行重构 Posted: October 2, 2009 | Author: WaterSky | Follow Me: My Profile 感谢:http://code.google.com/p/androidbmi/wiki/BmiRefactor 写的非常的好,通俗易懂而又不 显啰嗦,真是恰到好处啊! 本文仅以代码说话,主要是方便自己回想这样重构的好处,记录一下自己重构过程中的思路 逻辑,更多的文字介绍请看前面那个链接! Step1:抽取所有界面元件的声明和定义,整合到单独一个函数 findViews()中; // 声明 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); } 此部分即是 MVC 中的 V:View 视图。 Step2:抽取程序的逻辑(即界面元件的处理逻辑),整合到函数 setListensers()中; //Listen for button clicks private void setListensers() { button_calc.setOnClickListener(calcBMI); } 此部分即是 MVC 中的 C:Controller 控制器。 接着,onCreate()就显得非常简洁、明了了: public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); findViews(); setListensers(); }
  2. 2. Step3:修改按钮监听器 calcBMI 中相应的部分(主要是变量已经在视图部分定义了); private OnClickListener calcBMI = new OnClickListener() { @Override public void onClick(View v) { DecimalFormat nf = new DecimalFormat("0.00"); double height = Double.parseDouble(field_height.getText().toString())/100; double weight = Double.parseDouble(field_weight.getText().toString()); double BMI = weight / (height * height); //Present result view_result.setText("Your BMI is " + nf.format(BMI)); //Give health advice if(BMI>25){ view_suggest.setText(R.string.advice_heavy); } else if(BMI<20){ view_suggest.setText(R.string.advice_light); } else{ view_suggest.setText(R.string.advice_average); } } }; Step4:修剪一下“细枝末节”; 1) calcBMI 的修改: private OnClickListener calcBMI = new OnClickListener() { „„ } 改成如下: private Button.OnClickListener calcBMI = new Button.OnClickListener() { „„ } gasolin 的解释是: 同樣是「calcBMI」 函式,在完整程式中,改將「calcBMI」 函式從原本的「OnClickListener」宣告成 「Button.OnClickListener」。這個改變有什麼差別呢? 閱讀原本的程式碼,在匯入(import)的部分可以看到,「OnClickListener」是來自於 「android.view.View.OnClickListener」函式: import android.view.View.OnClickListener;
  3. 3. 改成 「Button.OnClickListener」後,「Button.OnClickListener」就變成來自於「android.widget.Button」 中的「OnClickListener」函式,在查閱程式時,整個「Button」與「OnClickListener」之間的關係變得更 清晰。 2) 字符串统一引用 XML 中的描述符: //Present result view_result.setText("Your BMI is " + nf.format(BMI)); 改成如下: //Present result view_result.setText(getText(R.string.bmi_result) + nf.format(BMI)); 总之,此重构的目的无非是使程序的脉络更加清晰,即让人一眼望去,就能很容易地分辨出 界面(View)应该写在哪里,程序逻辑(Controller)应该写在哪里,最终使维护和扩展代码 变得更加容易! 其实,重构很简单,通读代码,感觉哪边不太爽,就改那边吧!(我目前的感受) 一个良好的代码应该是能让人感到舒服的! 附录: 1) 重构前的代码 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.widget.Button; import android.widget.EditText; import android.widget.TextView; public class Bmi extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //Listen for button clicks Button button = (Button)findViewById(R.id.submit);
  4. 4. button.setOnClickListener(calcBMI); } private OnClickListener calcBMI = new OnClickListener() { @Override 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); } } }; } 2) 重构后的代码 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.widget.Button; import android.widget.EditText; import android.widget.TextView; public class Bmi extends Activity {
  5. 5. /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); findViews(); setListensers(); } // 声明 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 Button.OnClickListener calcBMI = new Button.OnClickListener() { @Override public void onClick(View v) { DecimalFormat nf = new DecimalFormat("0.00"); double height = Double.parseDouble(field_height.getText().toString())/100; double weight = Double.parseDouble(field_weight.getText().toString()); double BMI = weight / (height * height); //Present result view_result.setText(getText(R.string.bmi_result) + nf.format(BMI)); //Give health advice
  6. 6. if(BMI>25){ view_suggest.setText(R.string.advice_heavy); } else if(BMI<20){ view_suggest.setText(R.string.advice_light); } else{ view_suggest.setText(R.string.advice_average); } } }; }

×