디자인 모델 - 건설 자 모델

8053 단어
인용
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();

좋은 웹페이지 즐겨찾기