package com.sgdata.singleton;
public class Triple {
private static Triple[] triple = new Triple[ ] {
new Triple(0) ,
new Triple(1) ,
new Triple(2) ,
};
private int id;
private Triple(int id){
System.out.println("The instance " + id + " is created. ");
this.id = id;
}
public static Triple getInstance(int id){
return triple[id];
}
public String toString(){
return "[Triple id=" + id + "]";
}
}
Main.java
package com.sgdata.singleton;
public class Main {
public static void main(String[] args) {
System.out.println("Start");
for(int i=0;i<9;i++){
Triple triple = Triple.getInstance(i%3);
System.out.println(i+":"+triple);
}
System.out.println("End.");
}
}
댓글 없음:
댓글 쓰기