andorid 建造者模式

建造者模式会让你的代码整洁,工厂化。

private void openFilePicker() { 
     new MaterialFilePicker()         
             .withSupportFragment(this)   
             .withRequestCode(1)            
             .withFilter(Pattern.compile(".*\\.txt$")) // Filtering files and directories by file name using regexp           
             .withFilterDirectories(false)             // Set directories filterable (false by default)
             .withHiddenFiles(true)                    // Show hidden files and folders   
             .withRootPath(Environment.getRootDirectory().getPath())        
             .start();         
     }

这就是建造者模式基本式样
那么在 MaterialFilePicker 类里面。一些类是这样的方法就是添加条件

public MaterialFilePicker withSelectType(int selectType) { 
   mSelectType = selectType;  
    return this;
}
.....

而往往最后一个 start()方法则是执行关键。

public void start() {  
    if (mActivity == null && mFragment == null && mSupportFragment == null) {
        throw new RuntimeException("You must pass Activity/Fragment by calling withActivity/withFragment/withSupportFragment method"); 
      }    if (mRequestCode == null) {        
              throw new RuntimeException("You must pass request code by calling withRequestCode method");  
      }    Intent intent = getIntent();   
           if (mActivity != null) {       
                   mActivity.startActivityForResult(intent, mRequestCode);  
      } else if (mFragment != null) {   
           mFragment.startActivityForResult(intent, mRequestCode);  
      } else {     
           mSupportFragment.startActivityForResult(intent, mRequestCode);   
   }
}

你可能感兴趣的:(andorid 建造者模式)