我是“命令”软件设计模式的新手,我在不知道自己在做什么的情况下尝试了它。我知道这对于 Stack Overflow 来说并不完全是一个合适的问题,但是如果您查看我的来源,看起来我正在接近它吗?我已经制作了在构造它们时执行它们的任务的对象(而超类处理任何引发的异常。)
(编辑#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);
}