λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
개발/JAVA

[JAVA] 정적 νŒ©ν† λ¦¬ λ©”μ†Œλ“œλž€? (static factory method)

by ynzu🀍 2022. 1. 10.
λ°˜μ‘ν˜•

 

정적 νŒ©ν† λ¦¬ λ©”μ„œλ“œ

정적(static), νŒ©ν† λ¦¬(factory), λ©”μ„œλ“œ(method)

GoFλΌλŠ” λ””μžμΈ νŒ¨ν„΄ 쀑 νŒ©ν† λ¦¬ νŒ¨ν„΄μ—μ„œ μœ λž˜ν•œ λ‹¨μ–΄λ‘œ 객체λ₯Ό μƒμ„±ν•˜λŠ” 역할을 λΆ„λ¦¬ν•˜κ² λ‹€λŠ” 것을 λ§ν•œλ‹€.

(ν•˜μ§€λ§Œ 정적 νŒ©ν† λ¦¬ λ©”μ„œλ“œμ™€ νŒ©ν† λ¦¬ νŒ¨ν„΄μ€ λͺ…ν™•ν•˜κ²Œ λ‹€λ₯΄κ³ , λ””μžμΈ νŒ¨ν„΄ 쀑에 이와 μΌμΉ˜ν•˜λŠ” νŒ¨ν„΄μ΄ μ—†λ‹€κ³  ν•œλ‹€. 이 뢀뢄에 λŒ€ν•΄μ„  곡뢀λ₯Ό 해봐야겠닀..)

μ’€ 더 μ‰½κ²Œ μ„€λͺ…ν•˜μžλ©΄ μƒμ„±μžλ₯Ό 톡해 객체λ₯Ό μƒμ„±ν•˜λŠ”κ²Œ μ•„λ‹ˆλΌ 별도 λ©”μ†Œλ“œλ₯Ό μ •μ˜ν•΄μ„œ 객체λ₯Ό 생성할 수 μžˆλ„λ‘ 객체 내에 정적 λ©”μ†Œλ“œλ₯Ό μ •μ˜ν•΄λ†“μ€ 것!


μ •ν™•νžˆ μ–΄λ–€ λ©”μ†Œλ“œλ₯Ό μ–˜κΈ°ν•˜λŠ” 건지 μ˜ˆμ‹œλ₯Ό 듀어보겠닀.

java.time νŒ¨ν‚€μ§€μ— ν¬ν•¨λœ LocalDateTime 클래슀의 정적 νŒ©ν† λ¦¬ λ©”μ„œλ“œλ₯Ό μ‚΄νŽ΄ 보면 μƒμ„±μžλ₯Ό 톡해 객체λ₯Ό μƒμ„±ν•˜λŠ” 것이 μ•„λ‹ˆλΌ of λ©”μ†Œλ“œλ₯Ό μ‚¬μš©ν•˜κ³ , of λ©”μ†Œλ“œ 내뢀에 객체λ₯Ό μƒμ„±ν•˜λŠ” μ½”λ“œκ°€ μžˆλŠ” 것을 λ³Ό 수 μžˆλ‹€.

LocalDateTime ldt = LocalDateTime.of(2022, 1, 1, 1, 30, 0);
public static LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute, int second) {
        LocalDate date = LocalDate.of(year, month, dayOfMonth);
        LocalTime time = LocalTime.of(hour, minute, second);
        return new LocalDateTime(date, time);
}

 

 

그리고 java.math νŒ¨ν‚€μ§€μ— ν¬ν•¨λœ BigInteger클래슀의 κ²½μš°λ„ valueOf, smallPrime, largePrime λ“±μ˜ λ©”μ†Œλ“œλ₯Ό μ œκ³΅ν•˜λŠ”λ° ν•΄λ‹Ή λ©”μ†Œλ“œλ₯Ό μ‚΄νŽ΄λ³΄λ©΄ λ©”μ†Œλ“œ λ‚΄λΆ€μ—μ„œ 객체λ₯Ό μƒμ„±ν•˜κ³  μžˆλ‹€.

public static BigInteger valueOf(long val) {
        // If -MAX_CONSTANT < val < MAX_CONSTANT, return stashed constant
        if (val == 0)
            return ZERO;
        if (val > 0 && val <= MAX_CONSTANT)
            return posConst[(int) val];
        else if (val < 0 && val >= -MAX_CONSTANT)
            return negConst[(int) -val];

        return new BigInteger(val);
}

 

private static BigInteger smallPrime(int bitLength, int certainty, Random rnd) {
        int magLen = (bitLength + 31) >>> 5;
        int temp[] = new int[magLen];
        int highBit = 1 << ((bitLength+31) & 0x1f);  // High bit of high int
        int highMask = (highBit << 1) - 1;  // Bits to keep in high int

        while (true) {
            // Construct a candidate
            for (int i=0; i < magLen; i++)
                temp[i] = rnd.nextInt();
            temp[0] = (temp[0] & highMask) | highBit;  // Ensure exact length
            if (bitLength > 2)
                temp[magLen-1] |= 1;  // Make odd if bitlen > 2

            BigInteger p = new BigInteger(temp, 1);

            // Do cheap "pre-test" if applicable
            if (bitLength > 6) {
                long r = p.remainder(SMALL_PRIME_PRODUCT).longValue();
                if ((r%3==0)  || (r%5==0)  || (r%7==0)  || (r%11==0) ||
                    (r%13==0) || (r%17==0) || (r%19==0) || (r%23==0) ||
                    (r%29==0) || (r%31==0) || (r%37==0) || (r%41==0))
                    continue; // Candidate is composite; try another
            }

            // All candidates of bitLength 2 and 3 are prime by this point
            if (bitLength < 4)
                return p;

            // Do expensive test if we survive pre-test (or it's inapplicable)
            if (p.primeToCertainty(certainty, rnd))
                return p;
        }
    }

 

μ΄λ ‡κ²Œ 정적 νŒ©ν† λ¦¬ λ©”μ„œλ“œκ°€ 무엇인지 μ•Œκ²Œ 되면 μƒμ„±μžκ°€ μžˆλŠ”λ° μ™œ 정적 νŒ©ν† λ¦¬ λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•΄μ•Ό ν•˜λŠ” 건지 의문이 생긴닀!  'μƒμ„±μž λŒ€μ‹  정적 νŒ©ν† λ¦¬ λ©”μ„œλ“œλ₯Ό κ³ λ €ν•˜λΌ'λΌλŠ” 말도 있고..

μƒμ„±μžμ™€ μ–΄λ–€ 차이가 있고, 정적 νŒ©ν† λ¦¬ λ©”μ„œλ“œμ˜ μž₯점이 λ¬΄μ—‡μΈμ§€λŠ” λ‹€μŒ ν¬μŠ€νŒ…μ—μ„œ~!

 

μœ„ λ‚΄μš© ν¬μŠ€νŒ… μž‘μ„± μ™„λ£Œ!

 

[JAVA] μƒμ„±μž λŒ€μ‹  정적 νŒ©ν† λ¦¬ λ©”μ†Œλ“œλ₯Ό κ³ λ €ν•΄μ•Ό ν•˜λŠ” 이유!

λ¨Όμ € 정적 νŒ©ν† λ¦¬ λ©”μ†Œλ“œκ°€ 뭔지 잘 λͺ¨λ₯΄κ² μœΌλ©΄ μ•„λž˜ ν¬μŠ€νŒ…μ„ 보고 였자! [Java] 정적 νŒ©ν† λ¦¬ λ©”μ†Œλ“œλž€? (static factory method) 정적 νŒ©ν† λ¦¬ λ©”μ„œλ“œ 정적(static), νŒ©ν† λ¦¬(factory), λ©”μ„œλ“œ(method) GoFλΌλŠ” λ””.

ynzu-dev.tistory.com

 

728x90
λ°˜μ‘ν˜•

λŒ“κΈ€