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