Java

์ดํŽ™ํ‹ฐ๋ธŒ ์ž๋ฐ” ํ†บ์•„๋ณด๊ธฐ - ์•„์ดํ…œ 1

91MS 2022. 8. 13. 10:14
๐Ÿ“–
๐Ÿ“Œ
3์ค„ ์š”์•ฝ
  1. ์ƒ์„ฑ์ž ๋Œ€์‹  ์ •์  ํŒฉํ„ฐ๋ฆฌ ๋ฉ”์„œ๋“œ๋ฅผ ๊ณ ๋ คํ•˜๋ผ
  1. ์ •์  ํŒฉํ„ฐ๋ฆฌ ๋ฉ”์„œ๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ์‹œ์ ์—๋Š” ๋ฐ˜ํ™˜ํ•  ๊ฐ์ฒด์˜ ํด๋ž˜์Šค๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค
  1. HashMap๊ณผ EnumMap์˜ ์ฐจ์ด, HashSet๊ณผ EnumSet์˜ ์ฐจ์ด


์•„์ดํ…œ 1. ์ƒ์„ฑ์ž ๋Œ€์‹  ์ •์  ํŒฉํ„ฐ๋ฆฌ ๋ฉ”์„œ๋“œ๋ฅผ ๊ณ ๋ คํ•˜๋ผ

ํ•ต์‹ฌ ์ •๋ฆฌ

์žฅ์ 

  • ์ด๋ฆ„์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค. (๋™์ผํ•œ ์‹œ๊ทธ๋‹ˆ์ฒ˜์˜ ์ƒ์„ฑ์ž๋ฅผ ๋‘๊ฐœ ๊ฐ€์งˆ ์ˆ˜ ์—†๋‹ค.)
public static Order createForPrime(boolean prime, Product product) {
    Order order = new Order();
    order.prime = prime;
    order.product = product;
    return order;
}

public static Order createForUrgent(boolean urgent, Product product) {
    Order order = new Order();
    order.urgent = urgent;
    order.product = product;
    return order;
}
  • ํ˜ธ์ถœ๋  ๋•Œ๋งˆ๋‹ค ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒˆ๋กœ ์ƒ์„ฑํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค. (Boolean.valueOf)
public static final Boolean TRUE = new Boolean(true);
public static final Boolean FALSE = new Boolean(false);

public static Boolean valueOf(boolean b) {
    return (b ? TRUE : FALSE);
}

// main
Boolean.valueOf(false);
  • ๋ฐ˜ํ™˜ ํƒ€์ž…์˜ ํ•˜์œ„ ํƒ€์ž… ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋Š” ๋Šฅ๋ ฅ์ด ์žˆ๋‹ค. (์ธํ„ฐํŽ˜์ด์Šค ๊ธฐ๋ฐ˜ ํ”„๋ ˆ์ž„์›Œํฌ, ์ธํ„ฐํŽ˜์ด์Šค์— ์ •์  ๋ฉ”์†Œ๋“œ)
  • ์ž…๋ ฅ ๋งค๊ฐœ๋ณ€์ˆ˜์— ๋”ฐ๋ผ ๋งค๋ฒˆ ๋‹ค๋ฅธ ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค. (EnumSet)
  • ์ •์  ํŒฉํ„ฐ๋ฆฌ ๋ฉ”์„œ๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ์‹œ์ ์—๋Š” ๋ฐ˜ํ™˜ํ•  ๊ฐ์ฒด์˜ ํด๋ž˜์Šค๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค. (์„œ๋น„์Šค ์ œ๊ณต์ž ํ”„๋ ˆ์ž„์›Œํฌ)
public interface HelloService {
    String hello();
}

public class ChineseHelloService implements HelloService {

    @Override
    public String hello() {
        return "Ni Hao";
    }
}

@Bean
public HelloService helloService() {
    return new ChineseHelloService();
}

// main
ServiceLoader<HelloService> loader = ServiceLoader.load(HelloService.class);
Optional<HelloService> helloServiceOptional = loader.findFirst();
helloServiceOptional.ifPresent(h -> {
    System.out.println(h.hello()); // Ni Hao
});

๋‹จ์ 

  • ์ƒ์†์„ ํ•˜๋ ค๋ฉด public์ด๋‚˜ protected ์ƒ์„ฑ์ž๊ฐ€ ํ•„์š”ํ•˜๋‹ˆ ์ •์  ํŒฉํ„ฐ๋ฆฌ ๋ฉ”์„œ๋“œ๋งŒ ์ œ๊ณตํ•˜๋ฉด ํ•˜์œ„ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์—†๋‹ค.
  • ์ •์  ํŒฉํ„ฐ๋ฆฌ ๋ฉ”์„œ๋“œ๋Š” ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ์ฐพ๊ธฐ ์–ด๋ ต๋‹ค.

์™„๋ฒฝ ๊ณต๋žต

  • p9, ์—ด๊ฑฐ ํƒ€์ž…์€ ์ธ์Šคํ„ดํŠธ๊ฐ€ ํ•˜๋‚˜๋งŒ ๋งŒ๋“ค์–ด์ง์„ ๋ณด์žฅํ•œ๋‹ค.
  • p9, ๊ฐ™์€ ๊ฐ์ฒด๊ฐ€ ์ž์ฃผ ์š”์ฒญ๋˜๋Š” ์ƒํ™ฉ์ด๋ผ๋ฉด ํ”Œ๋ผ์ด์›จ์ดํŠธ ํŒจํ„ด์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • p10, ์ž๋ฐ” 8๋ถ€ํ„ฐ๋Š” ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ์ •์  ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์—†๋‹ค๋Š” ์ œํ•œ์ด ํ’€๋ ธ๊ธฐ ๋•Œ๋ฌธ์— ์ธ์Šคํ„ด์Šคํ™” ๋ถˆ๊ฐ€ ๋™๋ฐ˜ ํด๋ž˜์Šค๋ฅผ ๋‘˜ ์ด์œ ๊ฐ€ ๋ณ„๋กœ ์—†๋‹ค.
  • p11, ์„œ๋น„์Šค ์ œ๊ณต์ž ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ๋งŒ๋“œ๋Š” ๊ทผ๊ฐ„์ด ๋œ๋‹ค.
  • p12, ์„œ๋น„์Šค ์ œ๊ณต์ž ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ์—†๋‹ค๋ฉด ๊ฐ ๊ตฌํ˜„์ฒด๋ฅผ ์ธ์Šคํ„ด์Šค๋กœ ๋งŒ๋“ค ๋•Œ ๋ฆฌํ”Œ๋ ‰์…˜์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.
  • p12, ๋ธŒ๋ฆฌ์ง€ ํŒจํ„ด
  • p12, ์˜์กด ๊ฐ์ฒด ์ฃผ์ž… ํ”„๋ ˆ์ž„์›Œํฌ

์™„๋ฒฝ ๊ณต๋žต 1. ์—ด๊ฑฐ ํƒ€์ž…

Enumeration

  • ์ƒ์ˆ˜ ๋ชฉ๋ก์„ ๋‹ด์„ ์ˆ˜ ์žˆ๋Š” ๋ฐ์ดํ„ฐ ํƒ€์ž….
  • ํŠน์ •ํ•œ ๋ณ€์ˆ˜๊ฐ€ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” ๊ฐ’์„ ์ œํ•œํ•  ์ˆ˜ ์žˆ๋‹ค. ํƒ€์ž…-์„ธ์ดํ”„ํ‹ฐ (Type-Safety)๋ฅผ ๋ณด์žฅํ•  ์ˆ˜ ์žˆ๋‹ค. ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด์„ ๊ตฌํ˜„ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๊ธฐ๋„ ํ•œ๋‹ค.
  • ์งˆ๋ฌธ1) ํŠน์ • enum ํƒ€์ž…์ด ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๊ฐ’์„ ์ˆœํšŒํ•˜๋ฉฐ ์ถœ๋ ฅํ•˜๋ผ.
Arrays.stream(OrderStatus.values())
      .forEach(orderStatus1 -> System.out.println("orderStatus1 = " + orderStatus1));
  • ์งˆ๋ฌธ2) enum์€ ์ž๋ฐ”์˜ ํด๋ž˜์Šค์ฒ˜๋Ÿผ ์ƒ์„ฑ์ž, ๋ฉ”์†Œ๋“œ, ํ•„๋“œ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š”๊ฐ€?
public enum OrderStatus {
    PREPARING(0), SHIPPED(1), DELIVERING(2), DELIVERED(3);

    private int number;

    OrderStatus(int number) {
        this.number = number;
    }
}
  • ์งˆ๋ฌธ3) enum์˜ ๊ฐ’์€ == ์—ฐ์‚ฐ์ž๋กœ ๋™์ผ์„ฑ์„ ๋น„๊ตํ•  ์ˆ˜ ์žˆ๋Š”๊ฐ€?
Order order = new Order();
if (order.orderStatus == OrderStatus.DELIVERED) { // enum
    System.out.println("order = " + order);
}
  • ์งˆ๋ฌธ4) enum์„ key๋กœ ์‚ฌ์šฉํ•˜๋Š” Map์„ ์ •์˜ํ•˜์„ธ์š”.
EnumMap<OrderStatus, String> enumMap = new EnumMap(OrderStatus.class);
enumMap.put(OrderStatus.PREPARING, "PRE");
enumMap.put(OrderStatus.DELIVERING, "DEL");
enumMap.put(OrderStatus.SHIPPED, "SHIPP");
enumMap.put(OrderStatus.DELIVERED, "DELIVERED");

for (Map.Entry<OrderStatus, String> orderStatusStringEntry : enumMap.entrySet()) {
    System.out.println("orderStatusStringEntry.getKey() = " + orderStatusStringEntry.getKey());
    System.out.println("orderStatusStringEntry.getValue() = " + orderStatusStringEntry.getValue());
}
  • ์งˆ๋ฌธ5) enum์„ ๋‹ด๊ณ  ์žˆ๋Š” Set์„ ๋งŒ๋“ค์–ด ๋ณด์„ธ์š”.
EnumSet<OrderStatus> allOfOrderStatuses = EnumSet.allOf(OrderStatus.class);
System.out.println("allOfOrderStatuses = " + allOfOrderStatuses);

EnumSet<OrderStatus> noneOfOrderStatuses = EnumSet.noneOf(OrderStatus.class);
System.out.println("noneOfOrderStatuses = " + noneOfOrderStatuses);

EnumSet<OrderStatus> ofOrderStatuses = EnumSet.of(OrderStatus.SHIPPED, OrderStatus.PREPARING);
System.out.println("ofOrderStatuses = " + ofOrderStatuses);

HashMap vs EnumMap

  • HashMap์€ key๋ฅผ bucket์— ์ €์žฅํ•˜๊ณ  ๊ฐ bucket์ด linked list๋ฅผ ์ฐธ์กฐ ํ•˜๊ณ  ์žˆ์Œ. (LinkedList์—๋Š” hash(key)๊ฐ€ ๊ฐ™์€ element๊ฐ€ ๋“ค์–ด๊ฐ)
  • EnumMap์˜ ๊ฒฝ์šฐ key๋กœ ์‚ฌ์šฉํ•  ๊ฐ’์ด ์ œํ•œ๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ, ๊ทธ ๊ฐฏ์ˆ˜๋งŒํผ ๊ธธ์ด๋ฅผ ๊ฐ€์ง„ array๋ฅผ ์„ ์–ธํ•จ. ํ•ด๋‹น index์— ๊ฐ’์„ ๋„ฃ์œผ๋ฉด ๋จ.

HashSet vs EnumSet

  • HashSet์€ HashMap๊ณผ ๊ฐ™์€๋ฐ map์˜ value๊ฐ€ ์žˆ๋‹ค ์—†๋‹ค๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ์ง€์‹œ์ž ๊ฐ™์€ ๊ฐ’์ด ๋“ค์–ด๊ฐ.
  • EnumSet์€ ๊ฐ’์ด ์žˆ๋‹ค ์—†๋‹ค๋งŒ ํ‘œ์‹œํ•˜๋ฉด ๋˜๋‹ˆ๊นŒ EnumMap์ฒ˜๋Ÿผ array๋กœ ๊ตฌํ˜„ํ•˜์ง€ ์•Š๊ณ  10101011 ๊ฐ™์€ bit vector๋กœ ๊ตฌํ˜„์ด ๊ฐ€๋Šฅ.

์™„๋ฒฝ ๊ณต๋žต 2. ํ”Œ๋ผ์ด์›จ์ดํŠธ ํŒจํ„ด

Flyweight (๊ฐ€๋ฒผ์šด ์ฒด๊ธ‰)

  • ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ณ๊ฒŒ ๋งŒ๋“ค์–ด ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ์„ ์ค„์ด๋Š” ํŒจํ„ด.
  • ์ž์ฃผ ๋ณ€ํ•˜๋Š” ์†์„ฑ(๋˜๋Š” ์™ธ์ ์ธ ์†์„ฑ, extrinsit)๊ณผ ๋ณ€ํ•˜์ง€ ์•Š๋Š” ์†์„ฑ(๋˜๋Š” ๋‚ด์ ์ธ ์†์„ฑ, intrinsit)์„ ๋ถ„๋ฆฌํ•˜๊ณ  ์žฌ์‚ฌ์šฉํ•˜์—ฌ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ์„ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค.

์™„๋ฒฝ ๊ณต๋žต 3. ์ธํ„ฐํŽ˜์ด์Šค์— ์ •์  ๋ฉ”์†Œ๋“œ

์ž๋ฐ” 8๊ณผ 9์—์„œ ์ฃผ์š” ์ธํ„ฐํŽ˜์ด์Šค์˜ ๋ณ€ํ™”

  • ๊ธฐ๋ณธ ๋ฉ”์†Œ๋“œ (default method)์™€ ์ •์  ๋ฉ”์†Œ๋“œ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค.
  • ๊ธฐ๋ณธ ๋ฉ”์†Œ๋“œ
    • ์ธํ„ฐํŽ˜์ด์Šค์—์„œ ๋ฉ”์†Œ๋“œ ์„ ์–ธ ๋ฟ ์•„๋‹ˆ๋ผ, ๊ธฐ๋ณธ์ ์ธ ๊ตฌํ˜„์ฒด๊นŒ์ง€ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ๋‹ค.
    • ๊ธฐ์กด์˜ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ํด๋ž˜์Šค์— ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์ •์  ๋ฉ”์†Œ๋“œ
    • ์ž๋ฐ” 9๋ถ€ํ„ฐ private static ๋ฉ”์†Œ๋“œ๋„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค.
    • ๋‹จ, private ํ•„๋“œ๋Š” ์•„์ง๋„ ์„ ์–ธํ•  ์ˆ˜ ์—†๋‹ค.
  • ์งˆ๋ฌธ1) ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๋Š” Comparator๋ฅผ ๋งŒ๋“ค๊ณ  List<Integer>๋ฅผ ์ •๋ ฌํ•˜๋ผ.
List<Integer> numbers = new ArrayList();
numbers.add(10);
numbers.add(100);
numbers.add(20);
numbers.add(44);
numbers.add(3);

Comparator<Integer> desc = (o1, o2) -> o2 - o1;
numbers.sort(desc);
  • ์งˆ๋ฌธ2) ์งˆ๋ฌธ1์—์„œ ๋งŒ๋“  Comparator๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๋ผ.
numbers.sort(desc.reversed());

์™„๋ฒฝ ๊ณต๋žต 4. ์„œ๋น„์Šค ์ œ๊ณต์ž ํ”„๋ ˆ์ž„์›Œํฌ

ํ™•์žฅ ๊ฐ€๋Šฅํ•œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•

  • ์ฃผ์š” ๊ตฌ์„ฑ ์š”์†Œ
    • ์„œ๋น„์Šค ์ œ๊ณต์ž ์ธํ„ฐํŽ˜์ด์Šค (SPI)์™€ ์„œ๋น„์Šค ์ œ๊ณต์ž (์„œ๋น„์Šค ๊ตฌํ˜„์ฒด)
    • ์„œ๋น„์Šค ์ œ๊ณต์ž ๋“ฑ๋ก API (์„œ๋น„์Šค ์ธํ„ฐํŽ˜์ด์Šค์˜ ๊ตฌํ˜„์ฒด๋ฅผ ๋“ฑ๋กํ•˜๋Š” ๋ฐฉ๋ฒ•)
    • ์„œ๋น„์Šค ์ ‘๊ทผ API (์„œ๋น„์Šค์˜ ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์„œ๋น„์Šค ์ธํ„ฐํŽ˜์ด์Šค์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€์ ธ์˜ฌ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” API)

์™„๋ฒฝ ๊ณต๋žต 5. ๋ฆฌํ”Œ๋ ‰์…˜

reflection

  • ํด๋ž˜์Šค๋กœ๋”๋ฅผ ํ†ตํ•ด ์ฝ์–ด์˜จ ํด๋ž˜์Šค ์ •๋ณด(๊ฑฐ์šธ์— ๋ฐ˜์‚ฌโ€๋œ ์ •๋ณด)๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ธฐ์ˆ 
  • ๋ฆฌํ”Œ๋ ‰์…˜์„ ์‚ฌ์šฉํ•ด ํด๋ž˜์Šค๋ฅผ ์ฝ์–ด์˜ค๊ฑฐ๋‚˜, ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค๊ฑฐ๋‚˜, ๋ฉ”์†Œ๋“œ๋ฅผ ์‹คํ–‰ํ•˜๊ฑฐ๋‚˜, ํ•„๋“œ์˜ ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๊ฑฐ๋‚˜ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
  • ์–ธ์ œ ์‚ฌ์šฉํ• ๊นŒ?
    • ํŠน์ • ์• ๋…ธํ…Œ์ด์…˜์ด ๋ถ™์–ด์žˆ๋Š” ํ•„๋“œ ๋˜๋Š” ๋ฉ”์†Œ๋“œ ์ฝ์–ด์˜ค๊ธฐ (JUnit, Spring)
    • ํŠน์ • ์ด๋ฆ„ ํŒจํ„ด์— ํ•ด๋‹นํ•˜๋Š” ๋ฉ”์†Œ๋“œ ๋ชฉ๋ก ๊ฐ€์ ธ์™€ ํ˜ธ์ถœํ•˜๊ธฐ (getter, setter)


Uploaded by N2T