1. Class 초기화에 사용하는 일반적인 방법
1-1. Class 예시:
package init.pattern.example;
public class Person {
private String name = "CHAE";
private String id = "4600";
private int age = 20;
private int weight = 100;
private int height = 180;
private String phone = "02-1234-5678";
public Person()
{}
public Person(String name, String id, int age, int weight, int height, String phone) {
this.name = name;
this.id = id;
this.age = age;
this.weight = weight;
this.height = height;
this.phone = phone;
}
public void print()
{
System.out.println("NAME: "+ name + " ID: "+ id);
System.out.println("AGE: " + age + " WEIGHT: "+ weight + " HEIGHT: " + height + " PHONE: " + phone);
}
public void name(String name) {
this.name = name;
}
public void id(String id) {
this.id = id;
}
public void age(int age) {
this.age = age;
}
public void weight(int weight) {
this.weight = weight;
}
public void height(int height) {
this.height = height;
}
public void phone(String phone) {
this.phone = phone;
}
}
1-2. 사용 예시:
package init.pattern.example;
public class MainCommon {
public static void main(String[] args) {
Person p1 = new Person("CHAE", "1500", 50, 60, 150, "02-1111-2222");
p1.print();
Person p2 = new Person();
p2.name("KIM");
p2.id("3200");
p2.age(40);
p2.print();
}
}
1-3. 출력 예시:
NAME: CHAE ID: 1500
AGE: 50 WEIGHT: 60 HEIGHT: 150 PHONE: 02-1111-2222
NAME: KIM ID: 3200
AGE: 40 WEIGHT: 100 HEIGHT: 180 PHONE: 02-1234-5678
1-4. 불편한 점:
- 초기화 변수가 많아질 수록 코드가 길어짐.
- 필수 초기화 변수와 선택적 초기화 변수 구분 가독성이 떨어짐.
2. Class 초기화 변수가 많은 경우 유용한 Builder 패턴
2-1. Class 예시:
package builder.pattern.example;
public class Person {
private final String name;
private final String id;
private final int age;
private final int weight;
private final int height;
private final String phone;
private Person(Builder builder) {
name = builder.name;
id = builder.id;
age = builder.age;
weight = builder.weight;
height = builder.height;
phone = builder.phone;
}
public void print()
{
System.out.println("NAME: "+ name + " ID: "+ id);
System.out.println("AGE: " + age + " WEIGHT: "+ weight + " HEIGHT: " + height + " PHONE: " + phone);
}
public static class Builder{
//essential param.
private final String name;
private final String id;
//optional param.
private int age = 50;
private int weight = 100;
private int height = 175;
private String phone = "02-1234-5678";
public Builder(String name, String id){
this.name = name;
this.id = id;
}
public Builder age(int val){
age = val;
return this;
}
public Builder weight(int val){
weight = val;
return this;
}
public Builder height(int val){
height = val;
return this;
}
public Builder phone(String val){
phone = val;
return this;
}
public Person build(){
return new Person(this);
}
}
}
2-2. 사용 예시:
package builder.pattern.example;
public class MainBuilder {
public static void main(String[] args) {
Person p1 = new Person.Builder("CHAE", "8900").age(30).weight(80).height(190).phone("02-1111-2222").build();
p1.print();
Person p2 = new Person.Builder("KIM", "1200").age(28).build();
p2.print();
}
}
2-3. 출력 예시:
NAME: CHAE ID: 8900
AGE: 30 WEIGHT: 80 HEIGHT: 190 PHONE: 02-1111-2222
NAME: KIM ID: 1200
AGE: 28 WEIGHT: 100 HEIGHT: 175 PHONE: 02-1234-5678
2-4. 개인 의견:
- Class 초기화 변수가 많거나 늘어날 것 같은 경우, 유용한 패턴이 될 것 같음.
- Class 초기화 변수가 적은 경우, 불필요할 것 같음(오히려 구현하는 시간이 아까울 것 같음)
# CODE: https://gitlab.com/YuJungChae/design_pattern/tree/builder_pattern
'디자인패턴' 카테고리의 다른 글
인터페이스 및 인터페이스와 추상 클래스 비교, C++ (0) | 2012.11.28 |
---|---|
가상 함수 & 추상 클래스, C++ (2) | 2012.11.21 |
Observer Pattern, 옵저버 패턴, C++ (0) | 2012.06.23 |
Iterator Pattern, 이터레이터 패턴, C++ (1) | 2012.06.23 |
Command Pattern, 커맨드 패턴, C++ (2) | 2012.06.01 |