1. Telescoping Pattern (텔레스코핑 패턴)
생성자와 오버로딩을 사용하는 패턴
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | package design_pattern.builder; public class CoordinateTelescoping {     //텔레스코핑 생성자 패턴     //Telescoping constructor     private final int x;     private final int y;     private int w;     private int h;     public CoordinateTelescoping(int x, int y) {         this(x, y, 0, 0);     }     public CoordinateTelescoping(int x, int y, int w, int h) {         this.x = x;         this.y = y;         this.w = w;         this.h = h;     } } | cs | 
매개변수 증가시 코드량이 많아지고 무거워지는 문제,
초기화가 필요 없는 값도 무조건적으로 넘겨야 하는 경우가 존재
2. JavaBean Pattern (자바빈 패턴)
getter, setter 사용하는 패턴
(일반적으로 많이 사용되는 Value Object)
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | package design_pattern.builder; public class CoordinateJavaBean {     //자바 빈 패턴     //java bean pattern     private int x=0;     private int y=0;     private int w=0;     private int h=0;     public void setX(int x) {         this.x = x;     }     public void setY(int y) {         this.y = y;     }     public void setW(int w) {         this.w = w;     }     public void setH(int h) {         this.h = h;     } } | cs | 
객체가 완전하게 생성되었는지 보장 불가(동결(freezing) 보장 불가)
CoordinateJavaBean b = CoordinateJavaBean();
b.setX(10);
위 경우 x 값만 set한 채 객체를 사용할 수 있으므로 객체가 완전하게 생성되지 않은 상태
3. Builder Pattern (빌더 패턴)
텔레스코핑패턴 + 자바빈 패턴의 형태
초기화가 반드시 필요한 멤버변수는 생성자로,
선택적으로 초기화를 해도 되는 필드는 setter 메소드로 작성
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | package design_pattern.builder; public class CoordinateBuilder {     //빌더 패턴     //Builder pattern : 텔레스코핑 패턴 + 자바빈 패턴     private final int x;     private final int y;     private final int w;     private final int h;     public static class Builder {         private final int x;         private final int y;         private int w;         private int h;         public Builder(int x, int y) {             this.x = x;             this.y = y;         }         public Builder setW(int w) {             this.w = w;             return this;         }         public Builder setH(int h) {             this.h = h;             return this;         }         public CoordinateBuilder build() {             return new CoordinateBuilder(this);         }     }     public CoordinateBuilder(Builder builder) {         x = builder.x;         y = builder.y;         w = builder.w;         h = builder.h;     } } | cs | 
[객체 생성]
CoordinateBuilder c = new CoordinateBuilder.Builder(10, 10).setW(5).setH(5).build();
생성자로 필수 맴버변수를 초기화 하는 객체 생성, 선택적으로 초기화가 필요한 멤버변수는 setter 사용,
마지막으로 build 를 호출하여 객체 리턴
참고 :
반응형
    
    
    
  'back > Design Pattern' 카테고리의 다른 글
| [Design pattern] Factory pattern (팩토리 패턴) (0) | 2020.01.30 | 
|---|---|
| [Design pattern] Adaptor pattern (어댑터 패턴) 그리고 캡슐화 (0) | 2020.01.26 | 
| [Design pattern] Proxy pattern (프록시 패턴) (2) | 2019.12.17 | 
| [Design pattern] Observer Pattern (옵저버패턴) 그리고 리스너 (0) | 2019.11.21 | 
| [Design pattern] Facade pattern (퍼사드패턴) (0) | 2019.11.18 |