[JAVA ๋์์ธ ํจํด] static class์ Builder Pattern(๋น๋ ํจํด)
1. static class๋?
Static class๋ inner class(๋ด๋ถ ํด๋์ค)์ ํ ์ข ๋ฅ์ด๋ค.
inner class ์ค์์ outer class(์ธ๋ถ ํด๋์ค)์ ๋ฉค๋ฒ๋ณ์ ์ ์ธ ์์น์ static ํค์๋๋ฅผ ๋ถ์ฌ ์ ์ธํ ํด๋์ค์ด๋ค.
์ฃผ๋ก outer class์ static ๋ฉ์๋์์ ์ฌ์ฉ๋๋ค.
2. static class์ ์ ์ธ ๋ฐฉ๋ฒ
// Outer class
public class Outer {
// Static inner class
public static class Inner {
}
}
3. static class์ ๋ฉค๋ฒ์ ์ ๊ทผ
// Outer class
public class Outer {
// Instance inner class
public class InstanceInner {
private int var1;
private static int var2; // ๋ถ๊ฐ - ์ปดํ์ผ ์๋ฌ
}
// Static inner class
public static class StaticInner {
private int var1;
private static int var2; // ๊ฐ๋ฅ
}
}
Inner ํด๋์ค ์ค static class๋ง static ๋ฉค๋ฒ(static ๋ณ์, static ๋ฉ์๋)๋ฅผ ๊ฐ์ง ์ ์๋ค.
// Outer class
public class Outer {
private int outerInstanceVar;
private static int outerStaticVar;
// Static inner class
public static class StaticInner {
private int instanceVar = outerInstanceVar; // ๋ถ๊ฐ - ์ปดํ์ผ ์๋ฌ
private static int staticVar = outerStaticVar; // ๊ฐ๋ฅ - Outer class์ private static ๋ฉค๋ฒ ์ ๊ทผ
}
// Static inner class
public static class AnotherStaticInner {
private static int staticVar = StaticInner.staticVar; // ๊ฐ๋ฅ - ๋ค๋ฅธ static class์ private static ๋ฉค๋ฒ ์ ๊ทผ
}
}
๋ํ outer class์ ๋ค๋ฅธ inner class์ instance ๋ฉค๋ฒ์๋ ์ ๊ทผํ ์ ์์ผ๋ฉฐ static ๋ฉค๋ฒ๋ ์ ๊ทผ ์ ์ด์๊ฐ private์ด์ฌ๋ ์ ๊ทผํ ์ ์๋ค.
4. static class ์์ฑ
// Outer class
public class Outer {
// Static inner class
public static class StaticInner { }
public static void main(String[] args) {
// Static inner class ์์ฑ
Outer.StaticInner inner = new Outer.StaticInner();
}
}
Static inner class๋ฅผ ์์ฑํ ๋๋ ์ผ๋ฐ์ ์ธ static ๋ฉค๋ฒ์ ์ ๊ทผํ ๋์ ๊ฐ์ด ํด๋์ค๋ช .๋ฉค๋ฒ๋ช ์ผ๋ก ์ ๊ทผํด์ ์์ฑํ๋ค.
Instance inner class์ ๋ฌ๋ฆฌ outer class์ ์ธ์คํด์ค๋ฅผ ์์ฑํ์ง ์์๋ ๋๋ค.
5. Builder Pattern(๋น๋ ํจํด)
Static class๊ฐ ํจ๊ณผ์ ์ผ๋ก ์ฐ์ด๋ ๊ณณ ์ค ํ๋๋ ๋น๋ ํจํด(Builder Pattern)์ด๋ผ๋ ๋์์ธ ํจํด์ด๋ค.
// ์ฑ
๋๋ฉ์ธ ๊ฐ์ฒด
public class Book {
// Book์ ํ๋
private String title;
private String author;
private String publishing;
private int price;
public static class Builder {
// Book๊ณผ ๋์ผํ ํ๋๋ฅผ ๊ฐ๋ Builder
private String title;
private String author;
private String publishing;
private int price;
// ํ๋ ๋ณ Setter
public Builder title(String title) {
this.title = title;
return this;
}
public Builder author(String author) {
this.author = author;
return this;
}
public Builder publishing(String publishing) {
this.publishing = publishing;
return this;
}
public Builder price(int price) {
this.price = price;
return this;
}
// Book์ ๋ฐํํ๋ ๋ฉ์๋
public Book build() {
return new Book(this);
}
}
// Book ์์ฑ์
public Book(Builder builder) {
this.title = builder.title;
this.author = builder.author;
this.publishing = builder.publishing;
this.price = builder.price;
}
}
์์ ๊ฐ์ ํํ์ static inner class์ ์์ฑ์๋ฅผ ๊ฐ๋ ํด๋์ค๋ ๋ค์๊ณผ ๊ฐ์ด ๊ฐ์ฒด๋ฅผ ์์ฑํ ์ ์๋ค.
// Builder pattern ์ฌ์ฉ1
Book.Builder builder = new Book.Builder();
builder.title("์ฑ์์ฃผ์์");
builder.author("ํ๊ฐ");
builder.publishing("์ฐฝ๋น");
builder.price(12000);
Book book1 = builder.build();
// Builder pattern ์ฌ์ฉ2
Book book2 = new Book
.Builder()
.title("์๋
์ด ์จ๋ค")
.author("ํ๊ฐ")
.publishing("์ฐฝ๋น")
.price(9900)
.build();
๐ Builder pattern์ ์ฅ์
- ๊ฐ ์ธ์์ ์๋ฏธ๋ฅผ ์๊ธฐ ์ฝ๋ค.
- setter ๋ฉ์๋๊ฐ ์์ผ๋ฏ๋ก ๋ณ๊ฒฝ ๋ถ๊ฐ๋ฅ(immutable) ๊ฐ์ฒด๋ฅผ ๋ง๋ค ์ ์๋ค.
- ํ ๋ฒ์ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ฏ๋ก ๊ฐ์ฒด ์ผ๊ด์ฑ(consistency)์ด ๊นจ์ง์ง ์๋๋ค.
- build() ๋ฉ์๋์์ ์๋ชป๋ ๊ฐ์ด ์ ๋ ฅ๋์๋์ง ๊ฒ์ฆํ ์ ์๋ค.
'Javaยท๏ปฟServletยท๏ปฟJSP' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
SQL ์์กด ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ฌธ์ ์ ๊ณผ JPA (0) | 2020.03.18 |
---|---|
try-with-resource ๋ฌธ๋ฒ์ ์ฌ์ฉํ JDBC ํ๋ก๊ทธ๋๋ฐ ์์ ์ฝ๋ (0) | 2020.03.11 |
[JAVA/์น] Annotation๊ณผ Reflections ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ก ์๋ฐ ๋น ๊ด๋ฆฌํ๊ธฐ (0) | 2020.02.21 |
JAVA ์น ํ๋ก์ ํธ์์ Properties ํ์ผ ํ์ฉํ๊ธฐ (0) | 2020.02.20 |
๋ฆฌํ๋์ API๋ฅผ ์ด์ฉํ Front Controller ์๋ํ (0) | 2020.02.20 |
๋๊ธ