본문 바로가기

Language/Java

[Effective Java] Item4. Item4. Enforce noninstantiability with a private constructor - factory method로 static method들을 그룹화할 수 있다.(item 1)- Utility class들은 instance로 만들어 지기 위해 설계되지 않는다. - 그러나 명시적인 생성자가 없으면 compiler가 default 생성자를 만들어준다. # attempting to enforce noninstantiability if) by making a class abstract - 문제점 : 해당 클래스가 상속을 위한 것으로 오해할 수도 있다. - 문제점 : subclass가 만들어질 수 있고 해당 subclass가 인스턴스화 될 수 있다. - 생성자가 없으면 compiler가.. 더보기
[Effective Java] Item 3 Item 3. - Singleton + serializable일 떄에는 추가적인 작업이 필요하다. - single-element enum type is the best way to implement a singleton. [Singleton 구현 방법]- 일반적인 singleton 구현은, 인스턴스가 사용되는 시점에 생성되지 않는다. private static xxx = new xxx();로 인하여 클래스가 로드될 때 생성된다. - 이를 극복하기 위해 lazy initiailzation을 사용한다.-> private static xxx;만 해놓고 getInstance()에서 생성자 호출. -> 하지만 이러한 방법은 multi thread일 경우 안전하지 않다. -> 동시에 getInstance()를 호출.. 더보기
[Effective Java] item 2 Item 2 : consider a builder when faced with many constructor parameters - static factories, constructor는 같은 한계를 가진다. => large number of optional parameters에 대응하기 어렵다. => 대안 1) - parameter마다 생성자를 달리 할 수 있지만, 늘어나는 parameter에 대응하기 어렵고, 가독성도 매우 떨어진다. => 대안 2) - parameterless 생성자를 호출 후, setter를 통해서 parameter setting.- 대응에는 용이하지만, 생성 방식이 일관되지 못해서 (여기저기서 각자 생성하므로) state가 불안정함.- immutable class를 불가능하게 만.. 더보기
[Effective Java] item 1 생성자 대신 static factory method를 고려하라updated 2016.12.19 [static factory method]- 해당 class의 instance를 반환. - design pattern의 factory method와는 다르다! - 단순히 생성자의 매개변수 list를 다르게 하여 여러 생성자를 두는 것은 좋지 못하다! 그러면 코드를 읽는 사람은 단순히 코드만 보고서는 이 생성자가 무엇을 의미하는지 알지 못한다.이럴때에 추가적으로 class documentation을 봐야 알 수 있다. - immutable class를 가능케함. - **장점**- 생성자와는 달리 static factory method는 이름을 가진다. - 생성자와 달리 해당 class가 필요할 때 마다 새로운 객.. 더보기
attach source javadoc in eclipse Right click over the project -> Build path -> Configure build pathIn the new window, go to the "Libraries" tab.Select the library and expand it.There are 4 child options. Select "Javadoc location" and click the "Edit" button on the right.Now you can add a jar file containing the docs. This would be just a zip file with the extension changed to jar. Make sure inside the jar the index.html is in t.. 더보기