2012년 7월 2일 월요일

싱글톤 패턴


싱글 패턴 예제.

new는 사우론만이 할 수 있다.

class Ring {
private static Ring onlyRing; // singleton!
private Ring() {
System.out.println("Ring(): Only Sauron can make.");
}
public static Ring makeRing(String s) {
if(s.equals("Sauron") && onlyRing == null)
onlyRing = new Ring();
return onlyRing;
}
public void access() {
System.out.println("My Lord!");
}
}
public class LordOfTheRing {
public static void main(String[] args) {
Ring gollum = new Ring(); // private, error!
Ring sauron = Ring.makeRing("Sauron");
sauron.access();
}
}

댓글 없음:

댓글 쓰기

국정원의 댓글 공작을 지탄합니다.

UPBIT is a South Korean company, and people died of suicide cause of coin investment.

 UPBIT is a South Korean company, and people died of suicide cause of coin. The company helps the people who control the market price manipu...