如何通过减少重复代码来优化程序?

时间:2013-11-21 06:48:14

标签: android

在我的程序代码中,我总是使用以下结构,但它非常重复。你能告诉我如何优化吗?谢谢!

switch(PublicPar.StoredNumberTypeForUI){
  case NumberOfReceiver:
    break;

  case NumberOfOnly:
    break;

  case NumberOfExcept:
      finish(); 
    break;
}

我的程序代码

public class StepList extends ListActivity{

    private ListNumberAdapter mListNumberAdapter=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sms_step_list); 
        SetTitle();
        IniControl();   
        IniListView();      
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
        case 50:
            if (resultCode == RESULT_OK) {

                List<String> s=null;

                switch(PublicPar.StoredNumberTypeForUI){
                  case NumberOfReceiver:
                      s=PublicPar.SharedRule.receiverNumberList;     
                    break;

                  case NumberOfOnly:
                      s=PublicPar.SharedRule.onlyNumberList;    
                    break;

                  case NumberOfExcept:
                      s=PublicPar.SharedRule.exceptNumberList;  
                    break;
                }                                   

            }
            break;
        default:
            break;
        }
    }   

    private void SetTitle(){
        TextView tvTitle=((TextView)findViewById(R.id.tvTitle));
        switch(PublicPar.StoredNumberTypeForUI){
          case NumberOfReceiver:
              tvTitle.setText(getString(R.string.TitleOfReceiverNumber));
            break;

          case NumberOfOnly:
              tvTitle.setText(getString(R.string.TitleOfOnlyNumber));
            break;

          case NumberOfExcept:
              tvTitle.setText(getString(R.string.TitleOfExceptNumber));
            break;
        }
    }


    private void AddTelNumber(){
        switch(PublicPar.StoredNumberTypeForUI){
          case NumberOfReceiver:
              PublicPar.SharedRule.receiverNumberList.add(" ");  
            break;

          case NumberOfOnly:
              PublicPar.SharedRule.onlyNumberList.add(" "); 
            break;

          case NumberOfExcept:
              PublicPar.SharedRule.exceptNumberList.add(" ");   
            break;
        }   

        mListNumberAdapter.notifyDataSetChanged();
    }


    private void FillListView(){
        switch(PublicPar.StoredNumberTypeForUI){
          case NumberOfReceiver:
              if (PublicPar.SharedRule.receiverNumberList.size()<1){
                  PublicPar.SharedRule.receiverNumberList.add(" ");
                  mListNumberAdapter=new ListNumberAdapter(this,PublicPar.SharedRule.receiverNumberList);
              }
            break;

          case NumberOfOnly:
              if (PublicPar.SharedRule.onlyNumberList.size()<1){
                  PublicPar.SharedRule.onlyNumberList.add(" ");
                  mListNumberAdapter=new ListNumberAdapter(this,PublicPar.SharedRule.onlyNumberList);
              }           
            break;

          case NumberOfExcept:
              if (PublicPar.SharedRule.exceptNumberList.size()<1){
                  PublicPar.SharedRule.exceptNumberList.add(" ");
                  mListNumberAdapter=new ListNumberAdapter(this,PublicPar.SharedRule.exceptNumberList);
              }               
            break;
        }           

    }

    private void HandleNext(){
        switch(PublicPar.StoredNumberTypeForUI){
          case NumberOfReceiver:
                Intent intent = new Intent().setClass(getApplicationContext(),ui.StepOption.class);
                startActivity(intent);
                finish();
            break;

          case NumberOfOnly:
             finish();
            break;

          case NumberOfExcept:
              finish(); 
            break;
        }               
    }


    private void HanleBack(){
        switch(PublicPar.StoredNumberTypeForUI){
          case NumberOfReceiver:            
                Intent intent = new Intent().setClass(getApplicationContext(),ui.StepName.class);
                startActivity(intent);
                finish();
            break;

          case NumberOfOnly:
              Intent intent1 = new Intent().setClass(getApplicationContext(),ui.StepOption.class);
              startActivity(intent1);
              finish();
            break;

          case NumberOfExcept:
              Intent intent2 = new Intent().setClass(getApplicationContext(),ui.StepOption.class);
              startActivity(intent2);
              finish(); 
            break;
        }       
    }


    private void IniControl(){  

        findViewById(R.id.btnBack).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                HanleBack();
            }
        }); 

        findViewById(R.id.btnNext).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {   
                HandleNext();
            }
        });

        findViewById(R.id.btnCancel).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {               
                finish();
            }
        });             
    }


}

1 个答案:

答案 0 :(得分:1)

原则上,枚举可能有方法。

enum MyEnum {
    FIRST{String xyz(){return "x";}}, SECOND{String xyz(){return "y";}}, THIRD{String xyz(){return "z";}};
    abstract String xyz();
}

public class A {
    public static void main(String[] p) {
        for (MyEnum m : MyEnum.values()) {
            System.out.println(" "+m+" ==> "+m.xyz());
        }
    }
}

而不是写

void doThis() {
    switch(x) {
    case FIRST: doX(); break;
    case SECOND: doY(); break;
    case THIRD: doZ(); break;
    }
}
你可以写

void doThis() { x.doThis(MyActivity.this); } // or whatever data you really need there

前提是您在每个枚举值中定义doThis(MyActivity a)。但这不会使代码更短,它会提供不同的观点。

如果您只想让代码看起来更短,请删除不必要的换行符,就像我在这里做的那样。 (我必须指出,在实践中,人们更倾向于使用工具重新格式化代码,然后将其归咎于不可读而不是假设可能存在异常格式背后的原因。)