GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Builder Pattern

Builder Pattern

Jump to bottom
Namgyu Park edited this page Jul 11, 2020 · 13 revisions

Builder Pattern 이란?

생성자 (constructor) 의 인자값들을 다양하게 조합해서 사용해야 할 때,
생성자 (constructor) 가 쓸데없이 많아지는 코드를 방지하기 위해 사용하는 Design Pattern

클래스 내부에 Builder 클래스가 존재하여 Builder 를 이용해 필요한 값들만 받을 수 있도록 하며, Builder 인스턴스를 클래스의 확장자 인자로 받아 생성된 클래스 인스턴스로만 접근 가능하도록 하는 디자인 패턴

예를 들어, A 서비스에 접근할 수 있는 access token 을 생성하는 라이브러리를 만들어 외부에 배포한다고 가정시 외부 3rd party 의 상황에 따라서 필요한 인자값들이 다 동일하지 않을 수 있다.

  1. 나는 custom domain url을 설정해서 테스트 하고 싶어.
  2. 나는 A 서버스 앱으로 부터 access token 을 받는 것이 아니라 웹브라우저를 통해서 access token 을 가져오고 싶은데?
  3. 우리 서비스에는 필요없는 값들을 왜 생성자에 넣도록 요구하는거야? 헷갈려..

코드 예시

public class FrameworkSettings {

    private String clientId;
    private String clientSecret;
    private String firmwareVersion;
    private String extraInfo;
    private String modelId;

    FrameworkSettings(Builder builder) {
        this.clientId = builder.clientId;
        this.clientSecret = builder.clientSecret;
        this.firmwareVersion = builder.firmwareVersion;
        this.extraInfo = builder.extraInfo;
        this.modelId = builder.modelId;
    }

    public String getClientId() {
        return clientId;
    }

    public String getClientSecret() {
        return clientSecret;
    }

    public String getFirmwareVersion() {
        return firmwareVersion;
    }

    public String getExtraInfo() {
        return extraInfo;
    }

    public String getModelId() {
        return modelId;
    }

    public static class Builder {

        private String clientId;
        private String clientSecret;
        private String firmwareVersion;
        private String extraInfo;
        private String modelId;

        public Builder setClientId(String clientId) {
            this.clientId = clientId;
            return this;
        }

        public Builder setClientSecret(String clientSecret) {
            this.clientSecret = clientSecret;
            return this;
        }

        public Builder setFirmwareVersion(String firmwareVersion) {
            this.firmwareVersion = firmwareVersion;
            return this;
        }

        public Builder setExtraInfo(String extraInfo) {
            this.extraInfo = extraInfo;
            return this;
        }

        public Builder setModelId(String modelId) {
            this.modelId = modelId;
            return this;
        }

        public FrameworkSettings build() {
            if (clientId == null)
                throw new RuntimeException("ClientId can't be null!");

            return new FrameworkSettings(this);
        }
    }
}

Clone this wiki locally

You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.