1 cho this answer
Bên cạnh đó tôi sẽ đề nghị việc thực hiện sau đây sử dụng đa hình:
Bằng cách này bạn có thể thêm nền tảng mới dễ dàng hơn bằng cách giảm khớp nối giữa các lớp.
Mã Chủ đầu tư:
Desktop desktop = Desktop.getDesktop();
desktop.open(aFile);
desktop.imaginaryAction(aFile);
Các impl Desktop:
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 code 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 platform
class 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>
}
}
Đây chỉ là một ví dụ, trong cuộc sống thực không phải là giá trị để tạo ra một lớp mới chỉ để parametrize một giá trị (chuỗi lệnh% s) Nhưng hãy tưởng tượng rằng mỗi phương thức thực hiện một bước khác theo cách cụ thể của nền tảng.
Thực hiện kiểu tiếp cận này, có thể loại bỏ không cần thiết nếu/elseif/else xây dựng với thời gian có thể đưa ra lỗi (nếu có 6 trong số mã này và thay đổi là cần thiết, bạn có thể quên cập nhật một trong số đó, hoặc bằng cách sao chép/dán, bạn có thể quên thay đổi lệnh để thực hiện)
nó không phải là giải pháp –
@KrzysztofCichocki bạn không giải thích tại sao bạn nghĩ nó không phải là giải pháp, theo cách đó, mọi người đọc câu trả lời này có thể đánh giá liệu có nên sử dụng Nó hay không. – OscarRyz
Bạn không hiển thị bất kỳ mã nào thực sự hoạt động và mở trình duyệt mặc định cho java 1.5, chỉ một số bộ khung cho nó mà không có bất kỳ triển khai hữu ích nào, đó là lý do tại sao đây không phải là giải pháp. –