私は「コマンド」ソフトウェアデザインパターンに不慣れで、自分が何をしているのかをよく知らずにそれを試みました。これはStackOverflowにとって適切な質問ではないことは理解していますが、私のソースを見ると、私はそれに近づいているように見えますか?構築時にタスクを実行するオブジェクトを作成しました(スーパークラスは発生した例外を処理します)。
(編集#1:このソースは別のクラス内にあり、そのフィールドには「out」と「in」が含まれています。)
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);
}