0
我是新來的「命令」設計模式,而且嘗試了它沒有真正知道我在做什麼。據我所知,這是不完全的堆棧溢出的適當的問題,但如果你看看我的源,它看起來像我接近它吧?我做了,因爲他們正在建造,執行任務的對象Java:作爲對象的行爲?繼承異常處理程序?
(編輯#1(而超類處理任何產生的異常。):這個來源是另一個類中,一個其字段包括「走出去」和「在」。)
public static interface Operations{
public void action(String filename)
throws FileNotFoundException, UnsupportedEncodingException, IOException;
}
public static abstract class Operator implements Operations{
public Operator(String filename){
try{
action(filename);
} catch(FileNotFoundException FNFE){
sessionLog.report(FNFE.toString());
} catch(UnsupportedEncodingException UEE){
sessionLog.report(UEE.toString());
} catch(IOException IOE){
sessionLog.report(IOE.toString());
} finally{
try{
out.close();
} catch(IOException IOE){
sessionLog.report("The file may not have closed properly. "+IOE.toString());
} catch(NullPointerException NPE){
//sessionLog.report("The file may be null.");
}
}
}
}
public static class Saver extends Operator{
public void action(String filename)
throws FileNotFoundException, UnsupportedEncodingException, IOException{
out = new OutputStreamWriter(new FileOutputStream(filename), ENCODE);
out.write("Spoons.");
}
public Saver(String filename){super(filename);}
}
public static class Opener extends Operator{
public void action(String filename)
throws FileNotFoundException, UnsupportedEncodingException, IOException{
in = new InputStreamReader(new FileInputStream(filename), ENCODE);
/* ... */
}
public Opener(String filename){super(filename);}
}
public static void save(String filename, ShoppingMutableTreeNode SMTN){
new Saver(filename);
}
public static void open(String filename){
new Opener(filename);
}
謝謝!這非常豐富,正是我需要的! – 2012-08-03 16:54:03