public class CacheImmutale {
/**
* @param args
*/
private static String name ;
private static CacheImmutale[] Cache = new CacheImmutale[10];
private static int pos = 0 ;
private CacheImmutale(String name){
this.name = name ;
}
public String getName() {
return name;
}
public static CacheImmutale vlaueOf(String name){
for(int i = 0 ; i < 10 ; i++){
if(Cache[i] != null && Cache[i].getName().equals(name)){
return Cache[i];
}
}
if(pos == 10 ){
Cache[0] = new CacheImmutale(name);
pos = 1 ;
return Cache[0] ;
}else{
Cache[pos++] = new CacheImmutale(name);
return Cache[pos-1];
}
}
public boolean equals(Object obj){
if(this == obj){
return true ;
}
if(obj != null && obj.getClass() == this.getClass()){
CacheImmutale ci = (CacheImmutale) obj;
if(this.getName().equals(ci.getName())){
return true ;
}
}
return false ;
}
public int hashCode(){
return this.name.hashCode();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
CacheImmutale ci1 = CacheImmutale.vlaueOf("zxx");
CacheImmutale ci2 = CacheImmutale.vlaueOf("zxx");
System.out.println(ci1 == ci2);
}
}
缓存实例的不可变类
最新推荐文章于 2021-11-17 08:50:10 发布