设计模式----建造者模式

引用了

https://blog.csdn.net/top_code/article/details/41894351
https://blog.csdn.net/top_code/article/details/51841552

用途

用于解决以下这种情况,参数很多,但是并不是都需要,①于是你选择写出很多构造函数,②或者一次性让使用者输入很多参数,这些参数中很多为null。③这个时候就需要建造者builder模式。
public class Person {
    private final String firstName;
    private final String lastName;
    private final String middleName;
    private final int age;
    private final String street;
    private final String district;
    private final String city;
    private final String province;
    private final boolean isFemale;
    private final boolean isEmployed;

    public Person(String firstName, String lastName, String middleName, int age, String street,
                  String district, String city, String province, boolean isFemale, boolean isEmployed) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.middleName = middleName;
        this.age = age;
        this.street = street;
        this.district = district;
        this.city = city;
        this.province = province;
        this.isFemale = isFemale;
        this.isEmployed = isEmployed;
    }

    @Override
    public String toString() {
        return "Person{" +
                "firstName='" + firstName + '\'' +
                ", lastName='" + lastName + '\'' +
                ", middleName='" + middleName + '\'' +
                ", age=" + age +
                ", street='" + street + '\'' +
                ", district='" + district + '\'' +
                ", city='" + city + '\'' +
                ", province='" + province + '\'' +
                ", isFemale=" + isFemale +
                ", isEmployed=" + isEmployed +
                '}';
    }
}

代码示例

HttpClient_4.3中部分源码

/**
 * builder pattern
 * @author Ricky Feng
 * @version 2015-02-27 10:42
 */
public class IvrConfig {
    private final String apiKey;
    private final String secret;
    private final int interval;
    private final File ivrFileDir;
    
    IvrConfig(final String apiKey,final String secret,final int interval,final File ivrFileDir){
        this.apiKey = apiKey;
        this.secret = secret;
        this.interval = interval;
        this.ivrFileDir = ivrFileDir;
    }
    
    public String getApiKey() {
        return apiKey;
    }
 
    public String getSecret() {
        return secret;
    }
 
    public int getInterval() {
        return interval;
    }
 
    public File getIvrFileDir() {
        return ivrFileDir;
    }
    
    /**create Builder method**/
    public static IvrConfig.Builder custom() {
        return new Builder();
    }
    
    @Override
    public String toString() {
        return "IvrConfig [apiKey=" + apiKey + ", secret=" + secret
                + ", interval=" + interval + ", ivrFileDir=" + ivrFileDir + "]";
    }
 
    
    public static class Builder {
        private String apiKey;
        private String secret;
        private int interval;
        private File ivrFileDir;
        
        Builder(){
            this.apiKey = "null";
            this.secret = "null";
            this.interval = -1;
            this.ivrFileDir = new File("./");
        }
        
        public Builder setApiKey(final String apiKey) {
            this.apiKey = apiKey;
            return this;
        }
        
        public Builder setSecret(final String secret) {
            this.secret = secret;
            return this;
        }
        
        public Builder setinterval(final int interval) {
            this.interval = interval;
            return this;
        }
        
        public Builder setIvrFileDir(final File ivrFileDir) {
            this.ivrFileDir = ivrFileDir;
            return this;
        }
        
        public IvrConfig build(){
            
            return new IvrConfig(apiKey, secret, interval, ivrFileDir);
        }
    }
    
}
IvrConfig config = IvrConfig.custom().setApiKey("abc").setSecret("bb").build();
        
        System.out.println(config);

另外一种写法

public class PermissionAsker {

    private Activity activity;//activity
    private Fragment fragment;//fragment
    private OnAskAppearListener listener;//申请权限时要执行的事件
    private String[] permissions;//权限
    private int permissionCode;//权限申请码,不同权限,不能冲突
    private int activityCode;//设置界面权限申请码,不同权限不能冲突
    private String askReason;//权限跳转提示语句
    private boolean must;

    private AlertDialog alertDialog;

    private boolean isAsking;

    /**
     * 构造一个asker
     * @param activity activity
     * @param listener 申请权限时要执行的事件
     * @param permisstions 权限
     * @param permissionCode 权限申请的requestCode
     * @param activityCode 跳转到设置界面的requestCode
     * @param askReason 是否弹出dialog跳转到权限设置
     */
    public PermissionAsker(Activity activity, Fragment fragment, OnAskAppearListener listener, String[] permisstions, int permissionCode, int activityCode, String askReason, boolean must){
        this.activity = activity;
        this.fragment = fragment;
        this.listener = listener;
        this.permissions = permisstions;
        this.permissionCode = permissionCode;
        this.activityCode = activityCode;
        this.askReason = askReason;
        this.must = must;
    }


    public static class Builder{
        private Activity activity;
        private Fragment fragment;
        private OnAskAppearListener listener;
        private String[] permissions;
        private int permisstionCode;
        private int activityCode;
        private String askReason;
        private boolean must;

        public Builder(){
        }

        public Builder setActivity(Activity activity) {
            this.activity = activity;
            return this;
        }

        public Builder setFragment(Fragment fragment){
            this.fragment = fragment;
            return this;
        }

        public Builder setListener(OnAskAppearListener listener) {
            this.listener = listener;
            return this;
        }

        public Builder setPermissions(String[] permissions) {
            this.permissions = permissions;
            return this;
        }

        public Builder setPermisstionCode(int permisstionCode) {
            this.permisstionCode = permisstionCode;
            return this;
        }

        public Builder setActivityCode(int activityCode) {
            this.activityCode = activityCode;
            return this;
        }

        public Builder setAskReason(String askReason) {
            this.askReason = askReason;
            return this;
        }

        public Builder setMust(boolean must){
            this.must = must;
            return this;
        }

        public PermissionAsker build(){
            return new PermissionAsker(activity, fragment, listener, permissions, permisstionCode, activityCode, askReason, must);
        }
    }
}
initAsker = new PermissionAsker.Builder()
                .setActivity(this)
                .setActivityCode(2)
                .setPermisstionCode(2)
                .setAskReason("为了测试,我们需要你的这些权限")
                .setPermissions( new String[]{
                        Manifest.permission.RECORD_AUDIO,
                        Manifest.permission.CAMERA
                })
                .setMust(true)
                .setListener(new OnAskAppearListener() {
                    @Override
                    public void onAppear() {
                        Toast.makeText(MainActivity.this, "成功啦", Toast.LENGTH_SHORT).show();
                    }
                })
                .build();

你可能感兴趣的:(设计模式----建造者模式)