[keywords]:java,design pattern, design patterns, "Java and schema" learning, decorator, decoration mode, Unix
[Environment]:staruml5.0 + JDK6
[Author]:winty (wintys@gmail.com)
[Body]:
Package Pattern.decorator.grep Import java.io.*/** * Decorative mode (decorator pattern): grep * * grep is a command in UNIX, * use command
grep BMW File ", * you can find the line containing BMW in file, and show. * * @version 2009-6-6 * @author winty (wintys@gmail.com) */public class grep{public static void Main (string[) args
) throws exception{Grepview view = new Grepview ();
if (Args.length < 2) {view.println ("Usage:java Grep targetstring fileName");
View.println ("E.g.:java Grep BMW c:/sample.txt");
System.exit (0);
} grepreader grep;
FileReader file = new FileReader (args[1]);
grep = new Grepreader (file, args[0], view);
Grep.search ();
} class Grepreader extends filterreader{private LineNumberReader reader;
Private String target;
Private Grepview view; /** * @param in the input stream in to find the target string * @param target to find the destination string * @param view Lookup results Output/public Grepreader (Re Ader in, String Target, GRepview view) {super (in);
reader = new LineNumberReader (in);
This.target = target;
This.view = view;
public void Search () throws exception{String = null;
while (line = Reader.readline ())!=null {int index =-1;
if (index = Line.indexof (target))!=-1) {String str;
str = "line" + reader.getlinenumber () + "column" + (index + 1) + ":" + line;
View.println (str);
} reader.close ();
} class grepview{private printstream out;
Public Grepview () {this.out = System.out;
public void println (String str) {out.println (str); }
}