๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
BackEnd๐ŸŒฑ/Java

์ผ๊ธ‰ ์ปฌ๋ ‰์…˜(First-Class Collection)์ด๋ž€?

by dkswnkk 2023. 4. 12.

์ผ๊ธ‰ ์ปฌ๋ ‰์…˜(First-Class Collection)์ด๋ž€?

Java์—์„œ ์ผ๊ธ‰ ์ปฌ๋ ‰์…˜(First-Class Collection)์€ ๋‹ค๋ฅธ ๊ฐ์ฒด์™€ ๋™๋“ฑํ•œ ์ง€์œ„๋ฅผ ๊ฐ–๋Š” ์ปฌ๋ ‰์…˜ ๊ฐ์ฒด๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค.

์ผ๊ธ‰ ์ปฌ๋ ‰์…˜์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํŠน์ง•์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค.

  • ์ปฌ๋ ‰์…˜ ๊ฐ์ฒด๋Š” ๋ณ€์ˆ˜๋‚˜ ๋งค๊ฐœ๋ณ€์ˆ˜์— ํ• ๋‹นํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์ปฌ๋ ‰์…˜ ๊ฐ์ฒด๋Š” ๋‹ค๋ฅธ ๊ฐ์ฒด์™€ ๋™๋“ฑํ•œ ์ง€์œ„๋ฅผ ๊ฐ€์ง„๋‹ค.
  • ์ปฌ๋ ‰์…˜ ๊ฐ์ฒด๋Š” ๋ฐ˜ํ™˜๊ฐ’์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์ปฌ๋ ‰์…˜ ๊ฐ์ฒด๋Š” ํ•„์š”ํ•œ ๊ฒฝ์šฐ ๋ฉ”์„œ๋“œ์—์„œ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

๊ทธ๋ƒฅ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•œ ๋ฌธ์žฅ์œผ๋กœ ๋งํ•˜๋ฉด Collection์„ Wrapping ํ•˜๋ฉด์„œ, ๊ทธ ์™ธ ๋‹ค๋ฅธ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๊ฐ€ ์—†๋Š” ์ƒํƒœ๋ฅผ ์ผ๊ธ‰ ์ปฌ๋ ‰์…˜์ด๋ผ ํ•ฉ๋‹ˆ๋‹ค.
 

 

์˜ˆ์‹œ ์ฝ”๋“œ

์ฝ”๋“œ๋กœ ์‚ดํŽด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

๋จผ์ € ์ผ๊ธ‰ ์ปฌ๋ ‰์…˜์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์€ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.

public class Student {
    private List<String> subjects;

    public Student(List<String> subjects) {
        this.subjects = subjects;
    }

    public List<String> getSubjects() {
        return subjects;
    }
}

์œ„ ์ฝ”๋“œ์—์„œ Student ํด๋ž˜์Šค์—์„œ List<String> ํƒ€์ž…์˜ subjects๋ฅผ ์ง์ ‘ ๋‹ค๋ฃจ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ Student ํด๋ž˜์Šค๋Š” List ํƒ€์ž…์— ์˜์กด์„ฑ์„ ๊ฐ–๊ฒŒ ๋˜๋ฉฐ, List์˜ ๊ตฌํ˜„์ด ๋ณ€๊ฒฝ๋  ๊ฒฝ์šฐ Student ํด๋ž˜์Šค๋„ ํ•จ๊ป˜ ๋ณ€๊ฒฝ๋˜์–ด์•ผ ํ•˜๋Š” ๋ฌธ์ œ์ ์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

๋‹ค์Œ์€ ์ผ๊ธ‰ ์ปฌ๋ ‰์…˜์„ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.

public class Student {
    private Subjects subjects;

    public Student(Subjects subjects) {
        this.subjects = subjects;
    }

    public Subjects getSubjects() {
        return subjects;
    }
}

public class Subjects {
    private List<String> subjects;

    public Subjects(List<String> subjects) {
        this.subjects = subjects;
    }

    public void addSubject(String subject) {
        subjects.add(subject);
    }

    public List<String> getSubjects() {
        return new ArrayList<>(subjects);
    }
}

์œ„ ์ฝ”๋“œ์—์„œ๋Š” Student ํด๋ž˜์Šค์—์„œ Subjects ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ๋ณ€๊ฒฝ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. Subjects ํด๋ž˜์Šค๋Š” List<Strring>ํƒ€์ž…์˜ Subjects๋ฅผ ๊ฐ์‹ธ๊ณ  ์žˆ์œผ๋ฉฐ, Subject ๊ฐœ์ฒด๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜, ๋ชจ๋“  Subject ๊ฐ์ฒด๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋„๋ก ๋ฉ”์„œ๋“œ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

์ด๋ ‡๊ฒŒ ํ•จ์œผ๋กœ์จ Student ํด๋ž˜์Šค๋Š” Subjects ํด๋ž˜์Šค์™€๋งŒ ์˜์กด์„ฑ์„ ๊ฐ–๊ฒŒ ๋˜๊ณ  ์ด์ „์— ์ง€๋…”๋˜ ๋ฌธ์ œ์ ์ธ List ํƒ€์ž…์— ๋Œ€ํ•œ ์˜์กด์„ฑ์„ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋‹ค๋ฅธ ์˜ˆ์‹œ๋ฅผ ํ•œ๋ฒˆ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋จผ์ €, ์ผ๊ธ‰ ์ปฌ๋ ‰์…˜์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์€ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.

public class Book {
    private Map<String, String> properties;

    public Book(Map<String, String> properties) {
        this.properties = properties;
    }

    public String getProperty(String key) {
        return properties.get(key);
    }

    public void setProperty(String key, String value) {
        properties.put(key, value);
    }
}

์œ„ ์ฝ”๋“œ์—์„œ๋Š” Book ํด๋ž˜์Šค์—์„œ Map<String, String> ํƒ€์ž…์˜ properties๋ฅผ ์ง์ ‘ ๋‹ค๋ฃจ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ๋ฌธ์ œ์ ์ด ๋ณด์ด์‹œ๋‚˜์š”? ์ด ๊ฒฝ์šฐ Book ํด๋ž˜์Šค๋Š” Map ํƒ€์ž…์— ์˜์กด์„ฑ์„ ๊ฐ–๊ฒŒ ๋˜๋ฉฐ, Map์˜ ๊ตฌํ˜„์ด ๋ณ€๊ฒฝ๋  ๊ฒฝ์šฐ Book ํด๋ž˜์Šค๋„ ํ•จ๊ป˜ ๋ณ€๊ฒฝ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๋ฐ˜๋ฉด์— ์ผ๊ธ‰ ์ปฌ๋ ‰์…˜์„ ์‚ฌ์šฉํ•œ ์˜ˆ์‹œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

public class Book {
    private Properties properties;

    public Book(Properties properties) {
        this.properties = properties;
    }

    public String getProperty(String key) {
        return properties.getProperty(key);
    }

    public void setProperty(String key, String value) {
        properties.setProperty(key, value);
    }
}

public class Properties {
    private Map<String, String> properties;

    public Properties() {
        this.properties = new HashMap<>();
    }

    public void setProperty(String key, String value) {
        properties.put(key, value);
    }

    public String getProperty(String key) {
        return properties.get(key);
    }

    public Map<String, String> getAllProperties() {
        return new HashMap<>(properties);
    }
}

์œ„ ์ฝ”๋“œ์—์„œ๋Š” Book ํด๋ž˜์Šค์—์„œ Properties ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ๋ณ€๊ฒฝ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. Properties ํด๋ž˜์Šค๋Š” Map<String, String> ํƒ€์ž…์˜ Properties๋ฅผ ๊ฐ์‹ธ๊ณ  ์žˆ์œผ๋ฉฐ, Property ๊ฐ์ฒด๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜, ๋ชจ๋“  Property ๊ฐ์ฒด๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋„๋ก ๋ฉ”์„œ๋“œ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

์ด๋ ‡๊ฒŒ ํ•จ์œผ๋กœ์จ Book ํด๋ž˜์Šค๋Š” Properties ํด๋ž˜์Šค์™€๋งŒ ์˜์กด์„ฑ์„ ๊ฐ–๊ณ  Map ํƒ€์ž…์— ๋Œ€ํ•œ ์˜์กด์„ฑ์„ ์ œ๊ฑฐํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
 

 

์žฅ์ 

๊ทธ๋ ‡๋‹ค๋ฉด ์ด๋ ‡๊ฒŒ ์˜์กด์„ฑ์„ ์ œ๊ฑฐํ•˜๊ฒŒ ๋จ์œผ๋กœ์จ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ์žฅ์ ๋“ค์„ ๋ฌด์—‡์ผ๊นŒ์š”? ์žฅ์ ์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  • ๊ฐ€๋…์„ฑ๊ณผ ์œ ์ง€๋ณด์ˆ˜์„ฑ์ด ํ–ฅ์ƒ๋œ๋‹ค.
  • ์œ ํšจ์„ฑ ๊ฒ€์ฆ์ด ์šฉ์ดํ•ด์ง„๋‹ค.
  • ๋ถˆ๋ณ€์„ฑ์„ ๋ณด์žฅํ•˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

๊ฐ๊ฐ์˜ ์žฅ์ ๋“ค์„ ์ฝ”๋“œ๋กœ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

1. ๊ฐ€๋…์„ฑ๊ณผ ์œ ์ง€๋ณด์ˆ˜์„ฑ์ด ํ–ฅ์ƒ๋œ๋‹ค.

์ผ๊ธ‰ ์ปฌ๋ ‰์…˜์„ ์‚ฌ์šฉํ•˜๋ฉด ํ•ด๋‹น ์ปฌ๋ ‰์…˜์„ ์‚ฌ์šฉํ•˜๋Š” ํด๋ž˜์Šค์—์„œ ํ•ด๋‹น ์ปฌ๋ ‰์…˜์˜ ์—ญํ• ๊ณผ ์˜๋ฏธ๋ฅผ ๋ฐ”๋กœ ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ๊ณ , ์œ ์ง€๋ณด์ˆ˜์„ฑ์ด ํ–ฅ์ƒ๋ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ์€ ์ผ๊ธ‰ ์ปฌ๋ ‰์…˜์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์€ ์˜ˆ์‹œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.

public class Order {
    private List<String> items;
    private String paymentMethod;

    public Order() {
        this.items = new ArrayList<>();
    }

    public void setPaymentMethod(String paymentMethod) {
        this.paymentMethod = paymentMethod;
    }
}

Orderํด๋ž˜์Šค๊ฐ€ ์ฃผ๋ฌธ๊ณผ ๊ด€๋ จ๋œ ๋ฐ์ดํ„ฐ์™€ ์ฃผ๋ฌธ์˜ ๊ฒฐ์ œ ๋ฐฉ๋ฒ•์„ ๋ชจ๋‘ ๊ด€๋ฆฌํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๊ฒฝ์šฐ ๊ฒฐ์ œ ๋ฐฉ๋ฒ•๊ณผ ๊ด€๋ จ๋œ ๋กœ์ง์ด ์ถ”๊ฐ€๋  ๋•Œ๋งˆ๋‹ค Order ํด๋ž˜์Šค์˜ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ๊ฒฐ์ œ ๋ฐฉ๋ฒ•๊ณผ ๊ด€๋ จ๋œ ๋ฐ์ดํ„ฐ์™€ ์ฃผ๋ฌธ๊ณผ ๊ด€๋ จ๋œ ๋ฐ์ดํ„ฐ๊ฐ€ ์„ž์—ฌ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ฝ”๋“œ๋ฅผ ์ดํ•ดํ•˜๊ธฐ๊ฐ€ ์–ด๋ ค์šธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ผ๊ธ‰ ์ปฌ๋ ‰์…˜์„ ์‚ฌ์šฉํ•˜๋ฉด ์œ„์™€ ๊ฐ™์€ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜๋Š” ์ผ๊ธ‰ ์ปฌ๋ ‰์…˜์œผ๋กœ ๋ฆฌํŒฉํ† ๋งํ•œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.

public class Order {
    private OrderItems items;
    private PaymentMethod paymentMethod;

    public Order() {
        this.items = new OrderItems();
    }

    public void setPaymentMethod(PaymentMethod paymentMethod) {
        this.paymentMethod = paymentMethod;
    }
}

public class OrderItems {
    private List<String> items;

    public OrderItems() {
        this.items = new ArrayList<>();
    }

    // ์•„์ดํ…œ ๊ด€๋ จ ๋กœ์ง
}

public abstract class PaymentMethod {
    // ๊ฒฐ์ œ ๋ฐฉ๋ฒ• ๊ด€๋ จ ์ถ”์ƒ ๋ฉ”์„œ๋“œ
}

public class CardPayment extends PaymentMethod {
    // ์นด๋“œ ๊ฒฐ์ œ ๊ด€๋ จ ๋กœ์ง
}

public class CashPayment extends PaymentMethod {
    // ํ˜„๊ธˆ ๊ฒฐ์ œ ๊ด€๋ จ ๋กœ์ง
}

์œ„์˜ ์ฝ”๋“œ์—์„œ๋Š” Order ํด๋ž˜์Šค๊ฐ€ OrderItems ํด๋ž˜์Šค์™€ PaymentMethod ์ถ”์ƒ ํด๋ž˜์Šค๋ฅผ ๊ฐ๊ฐ ํฌํ•จํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

OrderItems ํด๋ž˜์Šค๋Š” ์ฃผ๋ฌธ์— ๋Œ€ํ•œ ์•„์ดํ…œ์„ ๊ด€๋ฆฌํ•˜๋Š” ํด๋ž˜์Šค์ด๋ฉฐ, PaymentMethod ์ถ”์ƒ ํด๋ž˜์Šค๋Š” ๊ฒฐ์ œ ๋ฐฉ๋ฒ• ๊ด€๋ จ ๋กœ์ง์„ ๊ด€๋ฆฌํ•˜๋Š” ์ถ”์ƒ ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค. ์ด ์ถ”์ƒ ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์€ CardPayment์™€ CashPayment ํด๋ž˜์Šค๋Š” ๊ฐ๊ฐ ์นด๋“œ ๊ฒฐ์ œ์™€ ํ˜„๊ธˆ ๊ฒฐ์ œ์— ๋Œ€ํ•œ ๋กœ์ง์„ ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

์ด๋ ‡๊ฒŒ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋ฉด, ์ฃผ๋ฌธ๊ณผ ๊ด€๋ จ๋œ ๋ฐ์ดํ„ฐ์™€ ๊ฒฐ์ œ ๋ฐฉ๋ฒ•๊ณผ ๊ด€๋ จ๋œ ๋ฐ์ดํ„ฐ๊ฐ€ ๊ฐ๊ฐ์˜ ํด๋ž˜์Šค์— ์บก์Šํ™”๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ๊ณผ ์œ ์ง€๋ณด์ˆ˜์„ฑ์ด ํ–ฅ์ƒ๋ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ์ƒˆ๋กœ์šด ๊ฒฐ์ œ ๋ฐฉ๋ฒ•์„ ์ถ”๊ฐ€ํ•  ๊ฒฝ์šฐ PaymentMethod ์ถ”์ƒ ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์•„ ํ•ด๋‹น ๊ฒฐ์ œ ๋ฐฉ๋ฒ•์˜ ๋กœ์ง์„ ๊ตฌํ˜„ํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋˜๋ฏ€๋กœ ์ฝ”๋“œ ์ˆ˜์ •์ด ๊ฐ„ํŽธํ•ด์ง‘๋‹ˆ๋‹ค.

2. ์œ ํšจ์„ฑ ๊ฒ€์ฆ์ด ์šฉ์ดํ•ด์ง„๋‹ค.

์ผ๊ธ‰ ์ปฌ๋ ‰์…˜์„ ์‚ฌ์šฉํ•˜๋ฉด ํ•ด๋‹น ์ปฌ๋ ‰์…˜์˜ ๋‚ด๋ถ€ ๊ตฌํ˜„์„ ์บก์Šํ™”ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์œ ํšจ์„ฑ ๊ฒ€์ฆ ์ฝ”๋“œ๋ฅผ ํ•ด๋‹น ์ปฌ๋ ‰์…˜ ํด๋ž˜์Šค ๋‚ด๋ถ€์— ๊ตฌํ˜„ํ•˜์—ฌ ์ปฌ๋ ‰์…˜์˜ ์‚ฌ์šฉ์ž๋“ค์ด ํ•ด๋‹น ์œ ํšจ์„ฑ ๊ฒ€์ฆ ๋กœ์ง์„ ํ˜ธ์ถœํ•˜๋Š” ๋ฒˆ๊ฑฐ๋กœ์›€์„ ์ค„์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด OrderItems ํด๋ž˜์Šค์—์„œ add() ๋ฉ”์„œ๋“œ์— null์ด๋‚˜ ๋นˆ ๋ฌธ์ž์—ด์„ ์ถ”๊ฐ€ํ•˜๋ ค๋Š” ๊ฒฝ์šฐ ์˜ˆ์™ธ๋ฅผ ํ„ฐํŠธ๋ฆฌ๋Š” ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

public class Order {
    private OrderItems items;

    public Order() {
        this.items = new OrderItems();
    }

    public OrderItems getItems() {
        return items;
    }

    public void addItem(String item) {
        this.items.add(item);
    }

    public void removeItem(String item) {
        this.items.remove(item);
    }
}

public class OrderItems {
    private List<String> items;

    public OrderItems() {
        this.items = new ArrayList<>();
    }

    public List<String> getItems() {
        return items;
    }

    public void add(String item) {
        if (item == null || item.isEmpty()) {
            throw new IllegalArgumentException("Item cannot be null or empty.");
        }
        this.items.add(item);
    }

    public void remove(String item) {
        this.items.remove(item);
    }
}

์œ„ ์ฝ”๋“œ์—์„œ๋Š” OrderItems ํด๋ž˜์Šค์˜ add() ๋ฉ”์„œ๋“œ์—์„œ item์ด null์ด๊ฑฐ๋‚˜ ๋นˆ ๋ฌธ์ž์—ด("") ์ผ ๊ฒฝ์šฐ IllegalArgumentException์„ ๋˜์ง€๋„๋ก ์ฒ˜๋ฆฌํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ํ•ด๋‹น ์ปฌ๋ ‰์…˜์„ ์‚ฌ์šฉํ•˜๋Š” ์ฝ”๋“œ์—์„œ๋Š” add() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ์œ ํšจ์„ฑ ๊ฒ€์ฆ์„ ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š์•„๋„ ๋ฉ๋‹ˆ๋‹ค(Order ํด๋ž˜์Šค์—์„œ ์œ ํšจ์„ฑ ๊ฒ€์ฆ ๋กœ์ง์„ ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š์•„๋„ ๋จ) ๋”ฐ๋ผ์„œ OrderItems ํด๋ž˜์Šค ๋‚ด๋ถ€์—์„œ ํ•ด๋‹น ๊ฒ€์ฆ์„ ์ˆ˜ํ–‰ํ•˜๋ฏ€๋กœ ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ๊ณผ ์œ ์ง€๋ณด์ˆ˜์„ฑ์ด ์ข‹์•„์ง‘๋‹ˆ๋‹ค.

3. ๋ถˆ๋ณ€์„ฑ์„ ๋ณด์žฅํ•˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

ํ•ด๋‹น ์ปฌ๋ ‰์…˜์„ ๋ถˆ๋ณ€(immutable)์œผ๋กœ ๋งŒ๋“ค์–ด์„œ ๋ณ€๊ฒฝ์„ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

public class Order {
    private final OrderItems items;

    public Order(OrderItems items) {
        this.items = items;
    }

    public OrderItems getItems() {
        return items;
    }
}

public class OrderItems {
    private final List<String> items;

    public OrderItems(List<String> items) {
        if (items == null) {
            throw new IllegalArgumentException("Items cannot be null.");
        }
        this.items = Collections.unmodifiableList(new ArrayList<>(items));
    }

    public List<String> getItems() {
        return items;
    }
}

์œ„ ์ฝ”๋“œ์—์„œ๋Š” OrderItems ํด๋ž˜์Šค์—์„œ ์ƒ์„ฑ์ž์—์„œ ๋ฐ›์€ List ๊ฐ์ฒด๋ฅผ ์ˆ˜์ •ํ•  ์ˆ˜ ์—†๋„๋ก Collections.unmodifiableList()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ถˆ๋ณ€ ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€ํ™˜ํ•œ ๋’ค, ์ด๋ฅผ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋กœ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ์ด๋กœ ์ธํ•ด OrderItems ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ ํ›„์—๋Š” ํ•ด๋‹น ๊ฐ์ฒด ๋‚ด๋ถ€์˜ List ์ปฌ๋ ‰์…˜์„ ์ˆ˜์ •ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ Order ํด๋ž˜์Šค์—์„œ OrderItems ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•˜๋Š” ๊ฒฝ์šฐ, ํ•ด๋‹น ๊ฐ์ฒด ๋‚ด๋ถ€์˜ List ์ปฌ๋ ‰์…˜์„ ์ˆ˜์ •ํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ ๋ถˆ๋ณ€์„ฑ์ด ๋ณด์žฅ๋ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ OrderItems ํด๋ž˜์Šค ๋‚ด๋ถ€์—์„œ List ๊ฐ์ฒด๋ฅผ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๋Š” add()๋‚˜ remove()์™€ ๊ฐ™์€ ๋ฉ”์„œ๋“œ๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ํ•ด๋‹น ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ ํ›„์—๋Š” ๋‚ด๋ถ€์˜ List ์ปฌ๋ ‰์…˜์— ์ƒˆ๋กœ์šด ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ์ œ๊ฑฐํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋•๋ถ„์— OrderItems ๊ฐ์ฒด์˜ ๋ถˆ๋ณ€์„ฑ๋„ ๋ณด์žฅ๋ฉ๋‹ˆ๋‹ค.

ํ•˜์ง€๋งŒ ์ผ๊ธ‰์ปฌ๋ ‰์…˜์„ ํ•ญ์ƒ ๋ถˆ๋ณ€ํ•˜๊ฒŒ ๋งŒ๋“ค ํ•„์š”๋Š” ์—†๊ณ , ์ƒํ™ฉ์— ๋งž๊ฒŒ ์ ์šฉํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
 

 

๋งˆ๋ฌด๋ฆฌ

์ตœ์ข…์ ์œผ๋กœ ์ผ๊ธ‰ ์ปฌ๋ ‰์…˜์„ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ์—์„œ๋Š” OrderItems ํด๋ž˜์Šค๊ฐ€ ์ง์ ‘ ๋ฆฌ์ŠคํŠธ์— ๋Œ€ํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ Order ํด๋ž˜์Šค๋Š” ๋‹จ์ˆœํžˆ OrderItems ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๋ฐ˜ํ™˜ํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋˜๋ฏ€๋กœ OrderItems ํด๋ž˜์Šค์˜ ๋‚ด๋ถ€ ๊ตฌ์กฐ๋ฅผ ์•Œ ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ๊ฒฐํ•ฉ๋„๊ฐ€ ๋‚ฎ์•„์ง€๊ณ  ์ฝ”๋“œ์˜ ์œ ์ง€๋ณด์ˆ˜์„ฑ์ด ํ–ฅ์ƒ๋ฉ๋‹ˆ๋‹ค.

์ผ๊ธ‰ ์ปฌ๋ ‰์…˜์€ ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ, ์œ ์ง€๋ณด์ˆ˜์„ฑ, ํ…Œ์ŠคํŠธ ์šฉ์ด์„ฑ ๋“ฑ์„ ๊ฐœ์„ ํ•  ์ˆ˜ ์žˆ๋Š” ๋งค์šฐ ์œ ์šฉํ•œ ๊ฐœ๋…์ž…๋‹ˆ๋‹ค. ์ผ๊ธ‰ ์ปฌ๋ ‰์…˜์˜ ๊ฐœ๋…์„ ์ดํ•ดํ•˜๊ณ , ์ ์ ˆํ•˜๊ฒŒ ํ™œ์šฉํ•˜๋ฉด ์ข€ ๋” ๋‚˜์€ ํด๋ฆฐ ํ•œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค.



์ฐธ๊ณ 

๋Œ“๊ธ€