본문 바로가기
Java

Java Builder 패턴

by 무대포 개발자 2017. 11. 24.
728x90
반응형

Builder 패턴 - config 설정할 때 종종 쓰임.


package builder;

import java.util.HashMap;

public class BuilderPatternExample
{
 public static void main(String [] args)
 {
  Builder builder = $for("constructor")
        .with("configuration")
        .withAttribute("key", "value");
 }

 public static Builder $for(String configuration)
 {
  return new Builder();
 }


 public static class Builder
 {
  String configuration;
  HashMap attributes = new HashMap();
  public Builder with(String configuration)
  {
   this.configuration = configuration;
   return this;
  }

  public Builder withAttribute(String key, Object value)
  {
   attributes.put(key, value);
   return this;
  }
 }
}

댓글