[Effective Java] Item 02 μμ±μμ λ§€κ°λ³μκ° λ§λ€λ©΄ λΉλλ₯Ό κ³ λ €νλΌ

https://www.yes24.com/Product/Goods/65551284
μ΄νν°λΈ μλ° Effective Java 3/E - μμ€24
μλ° νλ«νΌ λͺ¨λ² μ¬λ‘ μλ²½ κ°μ΄λ - Java 7, 8, 9 λμμλ° 6 μΆμ μ§ν μΆκ°λ γμ΄νν°λΈ μλ° 2νγ μ΄νλ‘ μλ°λ 컀λ€λ λ³νλ₯Ό κ²ͺμλ€. κ·Έλμ μ‘ΈνΈμμ λΉλλ μ΄ μ± λ μλ° μΈμ΄μ λΌμ΄λΈ
www.yes24.com
μ μ ν¨ν°λ¦¬μ μμ±μλ λκ°μ μ μ½μ΄ μλ€.
“μ νμ λ§€κ°λ³μκ° λ§μ λ μ μ ν λμνκΈ° μ΄λ ΅λ€”
class Book{
private String title;
private String publisher;
private String author;
private String translator;
private String painter;
public Book(String title, String publisher, String author, String translator, String painter){
this.title = title;
this.publisher = publisher;
this.author = author;
this.translator = translator;
this.painter = painter;
}
public Book(String title, String author, String translator, String publisher){
this(title, author, translator, null, publisher);
}
public Book(String title, String author, String publisher){
this(title, author, null, null, publisher);
}
public Book(String title, String publisher){
this(title, "λ―Έμ", null, null, publisher);
}
}
μ κ²½μ°μμλ author, translatorλ μ νμ λ§€κ°λ³μμ΄λ€.
Book book = new ("λκ° λ΄ λ¨Έλ¦¬μ λ₯μμ΄?", "μ¬κ³μ ", "λ² λ₯΄λ νμΈ λ°λ₯΄νΈ", null, "λ³Όν μλ₯ΌλΈλ£¨ν");
λκ° μκ°κ³ κ·Έλ¦Όμκ°κ³ nullμΈ κ²μ 무μμΈμ§ μ μκ° μλ€. κ°μλ λͺ κ°μΈμ§ μκΈ° μ΄λ ΅λ€.
λ§€κ°λ³μμ κ°μκ° λ λ§μμ§λ©΄ ν΄λΌμ΄μΈνΈ μ½λλ₯Ό μμ±νκ±°λ μ½κΈ° μ΄λ ΅λ€.
μ΄λ₯Ό ν΄κ²°νκΈ° μν λ°©λ²
- μλ° λΉμ¦ ν¨ν΄
class Book{
private String title;
private String publisher;
private String author;
private String translator;
private String painter;
public void setTitle(String title){this.title = title;}
public void setPublisher(String publisher){this.publisher = publisher;}
public void setAuthor(String author){this.author = author;}
public void setTranslator(String translator){this.translator= translator;}
public void setPainter(String painter){this.painter= painter;}
}
μ₯μ : μμμμ λ¬Έμ λ λ μ΄μ 보μ΄μ§ μμ
λ¨μ : κ°μ²΄κ° μμ ν μμ±λκΈ° μ κΉμ§ μΌκ΄μ±μ΄ 무λμ§ μνμ λμΈλ€.
Book book = new Book();
book.setTitle("λκ° λ΄ λ¨Έλ¦¬μ λ₯ μμ΄?");
book.setPublisher("μ¬κ³μ ");
/*μ¬κΈ°κΉμ§λ§ μ€νλ μνλ μΌκ΄μ±μ΄ κΉ¨μ§ μνμ΄λ€.*/
book.setAuthor("λ² λ₯΄λ νμΈ λ°λ₯΄νΈ");
book.setPainter("λ³Όν μλ₯ΌλΈλ£¨ν");
- λΉλ ν¨ν΄
μ μΈ΅μ μμ±μ ν¨ν΄μ μμ μ±κ³Ό μλ° λΉμ¦ λ°°ν΄μ κ°λ μ± κ²ΈλΉ
class Book{
private final String title;
private final String publisher;
private final String author;
private final String translator;
private final String painter;
public static class Builder{
private final String title;
private final String publisher;
private String author;
private String translator;
private String painter;
public Builder(String title, String publisher){
this.title = title;
this.publisher = publisher;
}
public Builder author(String author){
this.author= author; return this;
}
public Builder translator(String translator){
this.translator= translator; return this;
}
public Builder painter(String painter){
this.painter= painter; return this;
}
public Book build(){
return new Book(this);
}
}
public Book(Builder builder){
title = builder.title;
publisher = builder.publisher;
author = builder.author;
translator = builder.translator;
painter = builder.painter;
}
}
Book book = new Book("λκ° λ΄ λ¨Έλ¦¬μ λ₯ μμ΄?", "μ¬κ³μ ")
.author("λ² λ₯΄λ νμΈ λ°λ₯΄νΈ")
.painter("λ³Όν μλ₯ΌλΈλ£¨ν").build();
Book book = new ("λκ° λ΄ λ¨Έλ¦¬μ λ₯μμ΄?", "μ¬κ³μ ", "λ² λ₯΄λ νμΈ λ°λ₯΄νΈ", null, "λ³Όν μλ₯ΌλΈλ£¨ν");
λͺ¨μ λλ¬Έμ fluent API or method chaining μ΄λΌκ³ λ νλ€.
μλͺ»λ λ§€κ°λ³μ κ°μ§(λΆλ³μ : νλ‘κ·Έλ¨μ΄ μ€νλλ λμ λ°λμ λ§μ‘±ν΄μΌνλ κ·μΉ)λ₯Ό μν΄ νλ κ²μ¬ μ½λλ μΆκ°νλ©΄ μ’λ€.
λΉλ ν¨ν΄μ νΉν κ³μΈ΅μ μΌλ‘ μ€κ³λ ν΄λμ€μ μ°κΈ° μ’μ
public abstract class Book{
private final String title;
private final String publisher;
final Set<String> authors;
abstract static class Builder<T extends Builder<T>>{
private final String title;
private final String publisher;
Set<String> authors = Set.noneOf(String.class);
public T addAuthors(String author){
authors.add(Objects.requireNonNull(author));
return self();
}
public Builder(String title, String publisher){
this.title = title;
this.publisher = publisher;
}
abstract Book build();
protected abstract T self();
}
Book(Builder<?> builder){
title = builder.title;
publisher = builder.publisher;
authors = builder.authors.clone();
}
}
λͺ¨μ λλ¬Έμ fluent API or method chaining μ΄λΌκ³ λ νλ€.λΉλ ν¨ν΄μ νΉν κ³μΈ΅μ μΌλ‘ μ€κ³λ ν΄λμ€μ μ°κΈ° μ’μ
public class PictureBook extends Book{
private final String painter;
public static class Builder extends Book.Builder<Builder>{
private final String painter;
public Builder(String title, String publisher, String painter){
super(title, publisher);
this.painter = painter;
}
@Override public PictureBook build(){
return new PictureBook(this);
}
@Override protected Builder self() {return this; }
}
private PictureBook(Builder builder){
super(builder);
painter = builder.painter;
}
}
PictureBook book = new PictureBook.Builder("μ λͺ©", "μΆνμ¬", "κ·Έλ¦Όμκ°")
.addAuthors("μκ°1").addAuthors("μκ°2").addAuthors("μκ°3").build();
ν΄λΌμ΄μΈνΈλ ν λ³νμ μ κ²½μ°μ§ μκ³ λ λΉλλ₯Ό μ¬μ©ν μ μλ€. μμ΄λ, μΌλ ¨λ²νΈ κ°μ κ²μ μμμ μ±μ°λλ‘ ν μλ μλ€.
μ₯μ : μ μ₯μ + λΉλ νλλ‘ μ¬λ¬ κ°μ²΄λ₯Ό μνν μλ, λ§€κ°λ³μμ λ°λΌ λ€λ₯Έ κ°μ²΄λ₯Ό λ§λ€ μλ μλ€.
λ¨μ : λΉλλ₯Ό λ§λ€μ΄μΌ νλλ° μ±λ₯μ λ―Όκ°ν μν©μμλ λ¬Έμ κ° λ μ μλ€.