Android Builder 디자인 모드 읽 고 실전
복잡 한 대상 의 구축 과 그 표 시 를 분리 시 켜 같은 구축 과정 에서 서로 다른 표 시 를 만 들 수 있 습 니 다.
Builder 모드 의 사용 장면
    package com.softtanck;
    /** * * @author Tanck * * @date 2015-11-30 14:08:39 * */
    public class CarConfig {
        int wheels;//    
        int color;//     
        int maxSeat;//     
        int maxSpeed;//     
        //   ...
        private CarConfig() {
        }
        public static class Builder {
            private int wheels;//    
            private int color;//     
            private int maxSeat;//     
            private int maxSpeed;//     
            //   ...
            /** *      * * @param wheels * @return */
            public Builder setWheels(int wheels) {
                if (1 >= wheels)
                    wheels = 2;//    2
                this.wheels = wheels;
                return this;
            }
            /** *      * * @param color */
            public Builder setColor(int color) {
                this.color = color;
                return this;
            }
            /** *      * * @param maxSeat */
            public Builder setMaxSeat(int maxSeat) {
                if (4 <= maxSeat)
                    maxSeat = 4;//    4
                this.maxSeat = maxSeat;
                return this;
            }
            /** *      * * @param maxSpeed */
            public Builder setMaxSpeed(int maxSpeed) {
                this.maxSpeed = maxSpeed;
                return this;
            }
            /** *      * * @param carConfig */
            private void applyConfig(CarConfig carConfig) {
                carConfig.color = this.color;
                carConfig.maxSeat = this.maxSeat;
                carConfig.maxSpeed = this.maxSpeed;
                carConfig.wheels = this.wheels;
            }
            public CarConfig create() {
                CarConfig carConfig = new CarConfig();
                applyConfig(carConfig);
                return carConfig;
            }
        }
    }
사용 방식
CarConfig carConfig = new CarConfig.Builder()
                .setColor(1)
                .setMaxSeat(4)
                .setWheels(4)
                .setMaxSpeed(200).create();장점.
여분의 Builder 대상 과 Director 대상 이 생 겨 메모리 가 소 모 됩 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
디자인 모델 의 공장 모델, 단일 모델자바 는 23 가지 디자인 모델 (프로 그래 밍 사상/프로 그래 밍 방식) 이 있 습 니 다. 공장 모드 하나의 공장 류 를 만들어 같은 인 터 페 이 스 를 실현 한 일부 종 류 를 인 스 턴 스 로 만 드 는 것...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.