๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Java·๏ปฟServlet·๏ปฟJSP

[JAVA ๋””์ž์ธ ํŒจํ„ด] static class์™€ Builder Pattern(๋นŒ๋” ํŒจํ„ด)

by Leica 2020. 3. 9.
๋ฐ˜์‘ํ˜•

[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() ๋ฉ”์†Œ๋“œ์—์„œ ์ž˜๋ชป๋œ ๊ฐ’์ด ์ž…๋ ฅ๋˜์—ˆ๋Š”์ง€ ๊ฒ€์ฆํ•  ์ˆ˜ ์žˆ๋‹ค.
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€