在接受採訪時是問的問題:哪個設計模式使用
您必須編寫採取目錄名稱的程序(如d:\ XYZ) 和正則表達式(如「Olivea」)作爲參數。然後, 用正則表達式列出給定目錄中名稱匹配爲 的所有文件。
我從這裏做一個簡單的程序:
package temp;
import java.io.File;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class FileSearch1 {
static ArrayList l = new ArrayList();
public static void main(String[] args) {
String folderName = "D:\\"; // the folder path in which you want to
// search
String strPattern = "Olivea"; // Pattern what you want to search
final boolean searchinFile = true; // if you want to search in file keep
// it true for folder search keep it
// false
File f = new File("D:\\");
Pattern pattern1 = Pattern.compile(strPattern);
sunny(f, pattern1, searchinFile);
}
public static void sunny(File f, Pattern pattern1,
final boolean searchinFileOnly) {
File[] f1 = f.listFiles();
if (f1 == null)
return;
int k = f1.length;
// System.out.println(k);
int i = 0;
while (i < k) {
File f2 = f1[i];
if (f2.isDirectory() && !searchinFileOnly) {
Matcher match1 = pattern1.matcher(f2.getName());
while (match1.find()) {
l.add("");
System.out.println(f2.getName());
}
} else if (f2.isFile() && searchinFileOnly) {
Matcher match1 = pattern1.matcher(f2.getName());
while (match1.find()) {
l.add("");
System.out.println(f2.getName());
}
}
sunny(f2, pattern1, searchinFileOnly);
i++;
}
}
}
筆試後,面試官問我你能在這個使用其設計 模式。你能否給我建議我們可以在這個代碼中實現的各種 設計模式。
請幫我一下如何在這裏應用複合和迭代器模式?真的很感謝幫助
@丹尼斯感謝丹尼斯的評論,我該如何解決這個問題?請建議。 – 2012-01-14 05:10:35
每個人都應該來自元 – 2012-01-14 05:15:00
如果我正在接受採訪,並被問及採用哪種模式,我會走出面試。如果我正在採訪你,我會在看到「陽光明媚」的方法後結束採訪。 – 2012-01-14 05:18:50