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
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
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
java.util.Collection
νΉμ μΈν°νμ΄μ€λ₯΄ ꡬννλ κ°μ²΄λ₯Ό μμ±ν΄μ£Όλ μ μ λ©μλ(ν©ν°λ¦¬)λ₯Ό λͺ¨μλμ μ μλ€. (java8λΆν° μ΄λ° λ©μλλ₯Ό μΈν°νμ΄μ€μ λ£μ μ μμ)
final ν΄λμ€μ κ΄λ ¨ λ©μλ
final classλ₯Ό μμν΄μ νμ ν΄λμ€μ λ©μλλ₯Ό λ£λ κ²μ λΆκ°λ₯νλ―λ‘, final ν΄λμ€μ κ΄λ ¨ λ©μλλ€μ λͺ¨μλμλλ μ¬μ©νλ€.
Last updated
Was this helpful?