使用Java 1.5跨平台打开文件的方法

使用Java 1.5跨平台打开文件的方法,第1张

使用Java 1.5跨平台打开文件的方法

另外,我建议使用多态性进行以下实现:

这样,您可以通过减少类之间的耦合来更轻松地添加新平台。

客户端代码:

 Desktop desktop = Desktop.getDesktop(); desktop.open( aFile ); desktop.imaginaryAction( aFile );

桌面展示:

package your.pack.name;import java.io.File;public class Desktop{    // hide the constructor.    Desktop(){}    // Created the appropriate instance    public static Desktop getDesktop(){        String os = System.getProperty("os.name").toLowerCase();        Desktop desktop = new Desktop();         // This uf/elseif/else pre is used only once: here        if ( os.indexOf("windows") != -1 || os.indexOf("nt") != -1){ desktop = new WindowsDesktop();        } else if ( os.equals("windows 95") || os.equals("windows 98") ){ desktop = new Windows9xDesktop();        } else if ( os.indexOf("mac") != -1 ) { desktop = new OSXDesktop();        } else if ( os.indexOf("linux") != -1 && isGnome() ) { desktop = new GnomeDesktop();        } else if ( os.indexOf("linux") != -1 && isKde() ) { desktop = new KdeDesktop();        } else { throw new UnsupportedOperationException(String.format("The platform %s is not supported ",os) );        }        return desktop;    }    // default implementation :(     public void open( File file ){        throw new UnsupportedOperationException();    }    // default implementation :(     public void imaginaryAction( File file  ){        throw new UnsupportedOperationException();    }}// One subclass per platform below:// Each one knows how to handle its own platformclass GnomeDesktop extends Desktop{    public void open( File file ){        // Runtime.getRuntime().exec: execute gnome-open <file>    }    public void imaginaryAction( File file ){        // Runtime.getRuntime().exec:gnome-something-else <file>    }}class KdeDesktop extends Desktop{    public void open( File file ){        // Runtime.getRuntime().exec: kfmclient exec <file>    }    public void imaginaryAction( File file ){        // Runtime.getRuntime().exec: kfm-imaginary.sh  <file>    }}class OSXDesktop extends Desktop{    public void open( File file ){        // Runtime.getRuntime().exec: open <file>    }    public void imaginaryAction( File file ){        // Runtime.getRuntime().exec: wow!! <file>    }}class WindowsDesktop extends Desktop{    public void open( File file ){        // Runtime.getRuntime().exec: cmd /c start <file>    }    public void imaginaryAction( File file ){        // Runtime.getRuntime().exec: ipconfig /relese /c/d/e    }}class Windows9xDesktop extends Desktop{    public void open( File file ){        //Runtime.getRuntime().exec: command.com /C start <file>    }    public void imaginaryAction( File file){       //Runtime.getRuntime().exec: command.com /C otherCommandHere <file>    }}

这只是一个例子,在现实生活中不值得仅通过参数化值(命令字符串%s)来创建一个新类,但是让我们想象一下每种方法都以平台特定的方式执行另一个步骤。

采取这种方法,可能会删除不必要的if / elseif /
else结构,这些结构会随着时间的推移而引入错误(如果代码中有6个错误,并且需要更改,您可能会忘记更新其中之一,或者通过复制/粘贴,您可能会忘记更改要执行的命令)



欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5015336.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-14
下一篇2022-11-14

发表评论

登录后才能评论

评论列表(0条)

    保存