ITEM 4: Private Constructor

// Utilμ΄μ§€λ§Œ μƒμ„±μžκ°€ μ—†μŒ
public class ImageUtility {
    private static String IMAGE_DATE_FORMAT = "yyyyMMddHHmm";

    public static String makeImageFileNm(String imgFileNm) {
        return imgFileNm + "_" + new SimpleDateFormat(IMAGE_DATE_FORMAT).format(new Date());
    }
}

μƒμ„±μžλ₯Ό λͺ…μ‹œν•˜μ§€ μ•ŠμœΌλ©΄ μ»΄νŒŒμΌλŸ¬κ°€ μžλ™μœΌλ‘œ λ§€κ°œλ³€μˆ˜λ₯Ό 받지 μ•ŠλŠ” public μƒμ„±μžλ₯Ό λ§Œλ“ λ‹€. μ΄λ•Œ μ‚¬μš©μžλŠ” 이 μƒμ„±μžκ°€ μžλ™μœΌλ‘œ μƒμ„±λœ 것인지 ꡬ뢄할 수 μ—†μœΌλ©°,

// λ‹€μŒκ³Ό 같이 μ‚¬μš©ν•˜κΈ°λ₯Ό λ°”λž¬μœΌλ‚˜,
ImageUtility.makeImageFileNm("test", ".png");

// μƒμ„±μžλ₯Ό μƒμ„±ν•΄μ„œ μ‚¬μš©ν•  μˆ˜λ„ 있음
ImageUtility imageUtility = new ImageUtility();
String imageFileNm = imageUtility.makeImageFileNm("test", ".png");

이처럼 μ˜λ„μΉ˜ μ•Šκ²Œ μΈμŠ€ν„΄μŠ€ν™”ν•  수 있게된 ν΄λž˜μŠ€λ“€λ„ λ°œμƒν•œλ‹€.

// Utilμ΄μ§€λ§Œ μƒμ„±μžκ°€ μ—†μŒ
abstract class ImageUtility {
    private static String IMAGE_DATE_FORMAT = "yyyyMMddHHmm";

    public static String makeImageFileNm(String imgFileNm) {
        return imgFileNm + "_" + new SimpleDateFormat(IMAGE_DATE_FORMAT).format(new Date());
    }
}
public class ItemImageUtility extends ImageUtility {
  // ...
}
// μΆ”μƒν΄λž˜μŠ€μ΄κΈ° λ•Œλ¬Έμ— μƒμ„±μž 생성 λΆˆκ°€
// ImageUtility imageUtility = new ImageUtility();

// 상속받은 ν΄λž˜μŠ€μ—μ„œ μƒμ„±μž 호좜 κ°€λŠ₯
ItemImageUtility itemImageUtility = new ItemImageUtility();

좔상 클래슀둜 λ§Œλ“œλŠ” κ²ƒμœΌλ‘œλŠ” μΈμŠ€ν„΄μŠ€ν™”λ₯Ό 막을 수 μ—†μœΌλ©°, private μƒμ„±μžλ₯Ό μΆ”κ°€ν•˜λ©΄ 클래슀의 μΈμŠ€ν„΄μŠ€ν™”λ₯Ό 막을 수 μžˆλ‹€.

public class ImageUtility {
    // κΈ°λ³Έ μƒμ„±μžκ°€ λ§Œλ“€μ–΄μ§€λŠ” 것을 λ°©μ–΄(μΈμŠ€ν„΄μŠ€ν™” λ°©μ§€μš©)
    private ImageUtility(){
        throw new AssertionError();
    }
}

private μƒμ„±μžμ΄λ―€λ‘œ 클래슀 μ™ΈλΆ€μ—μ„œλŠ” μ ‘κ·Όν•  수 μ—†μœΌλ©°, λ‚΄λΆ€μ—μ„œ μ‹€μˆ˜λ‘œ μƒμ„±μžλ₯Ό ν˜ΈμΆœν•˜λŠ” κ²½μš°μ— λŒ€μ‘ν•˜κΈ° μœ„ν•΄ AssrtionError μ˜ˆμ™Έμ²˜λ¦¬λ₯Ό ν–ˆλ‹€. ν•˜μ§€λ§Œ, μƒμ„±μžκ°€ μžˆλŠ”λ° ν˜ΈμΆœν•  수 μ—†λŠ” 것은 직관적이지 μ•ŠμœΌλ―€λ‘œ, μ μ ˆν•œ 주석을 λ‹€λŠ” 것을 ꢌμž₯ν•œλ‹€.

λ˜ν•œ, private μƒμ„±μžλŠ” 상속도 λΆˆκ°€λŠ₯ν•˜λ‹€. λͺ¨λ“  μƒμ„±μžλŠ” μƒμœ„ 클래슀의 μƒμ„±μžλ₯Ό ν˜ΈμΆœν•˜κ²Œ λ˜λŠ”λ°, 이λ₯Ό private μ„ μ–ΈμœΌλ‘œ ν•˜μœ„ν΄λž˜μŠ€κ°€ μƒμœ„ 클래슀의 μƒμ„±μžμ— 접근을 λͺ»ν•΄ 상속이 λΆˆκ°€λŠ₯ν•˜λ‹€.

μ‚¬μš©λ˜λŠ” Utility

java.util.Arrays

public class Arrays {

    /**
     * The minimum array length below which a parallel sorting
     * algorithm will not further partition the sorting task. Using
     * smaller sizes typically results in memory contention across
     * tasks that makes parallel speedups unlikely.
     */
    private static final int MIN_ARRAY_SORT_GRAN = 1 << 13;

    // Suppresses default constructor, ensuring non-instantiability.
    private Arrays() {}

    /**
     * A comparator that implements the natural ordering of a group of
     * mutually comparable elements. May be used when a supplied
     * comparator is null. To simplify code-sharing within underlying
     * implementations, the compare method only declares type Object
     * for its second argument.
     *
     * Arrays class implementor's note: It is an empirical matter
     * whether ComparableTimSort offers any performance benefit over
     * TimSort used with this comparator.  If not, you are better off
     * deleting or bypassing ComparableTimSort.  There is currently no
     * empirical case for separating them for parallel sorting, so all
     * public Object parallelSort methods use the same comparator
     * based implementation.
     */
    static final class NaturalOrder implements Comparator<Object> {
        @SuppressWarnings("unchecked")
        public int compare(Object first, Object second) {
            return ((Comparable<Object>)first).compareTo(second);
        }
        static final NaturalOrder INSTANCE = new NaturalOrder();
    }
   ...

μΈμŠ€ν„΄μŠ€ν™”λ₯Ό ν•˜μ§€ μ•ŠκΈ° μœ„ν•΄ private μƒμ„±μžλ₯Ό μ„ μ–Έν•œ 것을 λ³Ό 수 있으며, λ°°μ—΄ κ΄€λ ¨ λ©”μ„œλ“œλ“€μ„ λͺ¨μ•„ λ†“λ‘μ—ˆλ‹€.

java.lang.Math

public final class Math {

    /**
     * Don't let anyone instantiate this class.
     */
    private Math() {}

    /**
     * The {@code double} value that is closer than any other to
     * <i>e</i>, the base of the natural logarithms.
     */
    public static final double E = 2.7182818284590452354;

    /**
     * The {@code double} value that is closer than any other to
     * <i>pi</i>, the ratio of the circumference of a circle to its
     * diameter.
     */
    public static final double PI = 3.14159265358979323846;

    /**
     * Returns the trigonometric sine of an angle.  Special cases:
     * <ul><li>If the argument is NaN or an infinity, then the
     * result is NaN.
     * <li>If the argument is zero, then the result is a zero with the
     * same sign as the argument.</ul>
     *
     * <p>The computed result must be within 1 ulp of the exact result.
     * Results must be semi-monotonic.
     *
     * @param   a   an angle, in radians.
     * @return  the sine of the argument.
     */
    public static double sin(double a) {
        return StrictMath.sin(a); // default impl. delegates to StrictMath
    }

Math에 λŒ€ν•œ κΈ°λ³Έ νƒ€μž…(PI, E)μ΄λ‚˜ κ΄€λ ¨ λ©”μ„œλ“œλ“€μ„ λͺ¨μ•„λ‘μ—ˆλ‹€.

java.util.Collection

νŠΉμ • μΈν„°νŽ˜μ΄μŠ€λ₯΄ κ΅¬ν˜„ν•˜λŠ” 객체λ₯Ό μƒμ„±ν•΄μ£ΌλŠ” 정적 λ©”μ„œλ“œ(νŒ©ν„°λ¦¬)λ₯Ό λͺ¨μ•„놓을 수 μžˆλ‹€. (java8λΆ€ν„° 이런 λ©”μ„œλ“œλ₯Ό μΈν„°νŽ˜μ΄μŠ€μ— 넣을 수 있음)

final ν΄λž˜μŠ€μ™€ κ΄€λ ¨ λ©”μ„œλ“œ

final classλ₯Ό μƒμ†ν•΄μ„œ ν•˜μœ„ ν΄λž˜μŠ€μ— λ©”μ„œλ“œλ₯Ό λ„£λŠ” 것은 λΆˆκ°€λŠ₯ν•˜λ―€λ‘œ, final ν΄λž˜μŠ€μ™€ κ΄€λ ¨ λ©”μ„œλ“œλ“€μ„ λͺ¨μ•„λ†“μ„λ•Œλ„ μ‚¬μš©ν•œλ‹€.

Last updated

Was this helpful?