InAndroidObject, We Need To refactor an instance. Reconstruction is to adjust the program code to improve the quality and performance of the software without changing the existing functions of the software, so that the program design mode and architecture become more reasonable, improve software scalability and maintainability.
1. What are the benefits of refactoring a program: when a program is written, what we need to do is to modify it and maintain it. Once the program grows and becomes more complex, it is difficult to maintain the state of chaos, so you have to delete and rewrite the program. So if we can re-organize or partially rewrite the code in some ways to make the code easy to maintain again, we can save a lot of time for ourselves, * meet more challenges.
2. Definition of refactoring: the process of modifying the program code to increase readability or simplify the program structure without affecting the output result. A proprietary term is refactoring.
3. demo
Java code:
- package eoe.demo;
- 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 classBMIextends Activity{
- @Override
- public void onCreate(Bundle savedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- findView();
- setListensers();
- }
- private Button btn_calc;
- private EditText field_height;
- private EditText field_weight;
- private TextView view_result;
- private TextView view_suggest;
- private void findView(){
- btn_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);
- private void setListensers(){
- btn_calc.setOnClickListener(clacBMI);
- }
- private Button.OnClickListener clacBMI = new Button.OnClickListener(){
- public void onClick(View v){
- DecimalFormat nf = new DecimalFormat("0.0");
- double height = Double.parseDouble(field_height.getText().toString())/100;
- double weight = Double.parseDouble(field_weight.getText().toString());
- doubleBMI = weight / (height*height);
- view_result.setText(getText(R.string.bmi_result)+nf.format(BMI));
- 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);
- }
- }
- };
- }
4. in the above Code: Make sure to unify the naming method of the Identity parameter: add the "btn _" prefix before the button's identity parameter, you can add the "field _" prefix before the field identification parameter and the "view _" prefix before the displayed parameter. Benefit: understand the meaning of variables and make the program better read.
Android message mechanism
Programmers should know that refactoring
Build an Android Development Environment
Object-oriented reconstruction of C Language
Android Network Connection processing learning notes