栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用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个错误,并且需要更改,您可能会忘记更新其中之一,或者通过复制/粘贴,您可能会忘记更改要执行的命令)



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/405819.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号