public class BadRegExpMatcher { public BadRegExpMatcher(String regExp);
/** Attempts to match the specified regular expression against the input text, returning the matched text if possible or null if not */ public String match(String inputText);
}
这个BadRegExpMatche要求入口参数是String ,那么如果MailBot要调用他,必须自己做一个 character buffer到String的转换:
BadRegExpMatcher dateMatcher = new BadRegExpMatcher(...);
while (...) { ...
//产生新的String String headerLine = new String(myBuffer, thisHeaderStart, thisHeaderEnd-thisHeaderStart);
class BetterRegExpMatcher { public BetterRegExpMatcher(...);
/** 提供多个接口参数的match方法 Provide matchers for multiple formats of input -- String, character array, and subset of character array. Return -1 if no match was made; return offset of match start if a match was made. */
public int match(String inputText); public int match(char[] inputText); public int match(char[] inputText, int offset, int length);
/** Get the next match against the input text, if any */ public int getNextMatch();
public int getX() { return myBounds.x; } public int getY() { return myBounds.y; } public int getHeight() { return myBounds.height; } public int getWidth() { return myBounds.width; }
这样调用变成: int x = component.getX(); int y = component.getY(); int h = component.getHeight(); int w = component.getWidth();
public class Point { protected int x, y; public Point(int x, int y) { this.x = x; this.y = y; } public final int getX() { return x; } public final int getY() { return y; } }
public class MutablePoint extends Point { public final void setX(int x) { this.x = x; } public final void setY(int y) { this.y = y; } }
这样,可变的需求和不可变的需求各自满足,分别调用。
public class Shape { private MutablePoint myLocation;
//返回可变的 public Shape(int x, int y) { myLocation = new MutablePoint(x, y); }
//返回不变的 public Point getLocation() { return (Point) myLocation; } }