私は将来のクラスへの静的参照を保持するFutureContentクラスを持っています。なぜこのようにしたのか分かりません。すべてのメソッド呼び出しが静的である必要はありません。このようにして別の理由がありますか?このように「静的」クラスを使用する理由は何ですか?
//One possible use of Future class
FutureContent.future.deleteContent("test");
public class FutureContent {
public static Future future = new Future();
}
public class Future{
private final Object lock = new Object();
private String str;
private Hashtable content = new Hashtable();
public void addContent(Object key, Object value){
synchronized(lock){
if(content.containsKey(key)){
content.remove(key);
content.put(key, value);
}
else {
content.put(key, value);
}
}
}
public void clearContent(){
content.clear();
}
public void deleteContent(Object key){
if(content.containsKey(key)){
content.remove(key);
}
}
public boolean getBoolean(Object key){
if(!content.contains(key)){
return false;
}
else {
return ((Boolean)content.get(key)).booleanValue();
}
}
public void addBoolean(Object key , boolean value){
Boolean b = new Boolean(value);
synchronized(lock){
if(content.containsKey(key)){
content.remove(key);
content.put(key, b);
}
else {
content.put(key, b);
}
}
}
public Object getContent(Object key){
return content.get(key);
}
public void setString(String str){
synchronized(lock){
this.str = str;
lock.notifyAll();
}
}
public String getString(){
synchronized(lock){
while(this.str == null)
try {
lock.wait();
} catch (InterruptedException e) {
return null;
}
return this.str;
}
}
private JSONObject value;
public void set(JSONObject t){
synchronized(lock){
value = t;
lock.notifyAll();
}
}
public JSONObject get(){
synchronized(lock){
while(value == null)
try {
lock.wait();
} catch (InterruptedException e) {
return null;
}
return value;
}
}
}
すべての厄介なアクセス制御なしで、すべての楽しいシングルトン! – cHao
はい、これはシングルトン(スレッドセーフでないもの、BTW) –