org.springframework.core.styler包解讀
這個包的說明是:Support for styling values as Strings, with ToStringCreator as central class.這個包簡單來說就是提供一個pretty-printing功能的輔助類,而ToStringCreator就是用於產生一個可以輸出經過美化的value信息的toString()方法。使用方法參照spring的Test可以看到是這樣:
int[] integers = new int[] { 0, 1, 2, 3, 4 };
String str = new ToStringCreator(integers).toString();
assertEquals("[@" + ObjectUtils.getIdentityHexString(integers) + " array<Integer>[0, 1, 2, 3, 4]]", str);
或者寫個簡單例子感受下:String str = new ToStringCreator(integers).toString();
assertEquals("[@" + ObjectUtils.getIdentityHexString(integers) + " array<Integer>[0, 1, 2, 3, 4]]", str);
int [] a={1,2,3,4,5,6,7,8,9};
System.out.println(new ToStringCreator(a).toString());
System.out.println(new ToStringCreator(a).toString());
輸出:
[@18558d2 array<Integer>[1, 2, 3, 4, 5, 6, 7, 8, 9]]
如果你接觸過ruby,你應該很熟悉Object.inpsect這個功能,這裏通過ToStringCreator包裝的toString()方法也是產生類似的能夠清晰顯示對象內部結構信息的方法。spring應該是使用這些輔助類來報告清晰的錯誤信息或者提示信息。
看看這個包的UML類圖:

首先,你需要理解ToStringStyler和ValueStyle兩個接口,ToStringStyler定義了描述一個輸入的Value信息的基本模板方法:
public interface ToStringStyler {
/**
* Style a <code>toString()</code>'ed object before its fields are styled.
* @param buffer the buffer to print to
* @param obj the object to style
*/
void styleStart(StringBuffer buffer, Object obj);
/**
* Style a <code>toString()</code>'ed object after it's fields are styled.
* @param buffer the buffer to print to
* @param obj the object to style
*/
void styleEnd(StringBuffer buffer, Object obj);
/**
* Style a field value as a string.
* @param buffer the buffer to print to
* @param fieldName the he name of the field
* @param value the field value
*/
void styleField(StringBuffer buffer, String fieldName, Object value);
/**
* Style the given value.
* @param buffer the buffer to print to
* @param value the field value
*/
void styleValue(StringBuffer buffer, Object value);
/**
* Style the field separator.
* @param buffer buffer to print to
*/
void styleFieldSeparator(StringBuffer buffer);
}
這是典型的Template Method模式,而兩個接口ToStringStyler、ValueStyler和它們的相應實現DefaultToStringStyler、DefaultValueStyler又是策略模式(Strategy)的應用體現。ValueStyler和DefaultValueStyler之間不僅僅是策略模式,同時也是visitor模式,請看DefaultValueStyler中一係列重載的visit方法,這些visit方法訪問不同類型Value的內部結構並構造pretty格式的String返回,提供給ToStringStyler使用。/**
* Style a <code>toString()</code>'ed object before its fields are styled.
* @param buffer the buffer to print to
* @param obj the object to style
*/
void styleStart(StringBuffer buffer, Object obj);
/**
* Style a <code>toString()</code>'ed object after it's fields are styled.
* @param buffer the buffer to print to
* @param obj the object to style
*/
void styleEnd(StringBuffer buffer, Object obj);
/**
* Style a field value as a string.
* @param buffer the buffer to print to
* @param fieldName the he name of the field
* @param value the field value
*/
void styleField(StringBuffer buffer, String fieldName, Object value);
/**
* Style the given value.
* @param buffer the buffer to print to
* @param value the field value
*/
void styleValue(StringBuffer buffer, Object value);
/**
* Style the field separator.
* @param buffer buffer to print to
*/
void styleFieldSeparator(StringBuffer buffer);
}
ToStringCreator是ToStringStyler的客戶,它使用ToStringStyler調用產生優美格式打印,而ToStringStyler 其實又是使用ValueStyler是訪問每個不同類型的子元素並返回優美格式的String。實現的相當精巧和靈活:
public ToStringCreator(Object obj, ToStringStyler styler) {
Assert.notNull(obj, "The object to be styled is required");
this.object = obj;
this.styler = (styler != null ? styler : DEFAULT_TO_STRING_STYLER);
//開始 this.styler.styleStart(this.buffer, this.object);
}
public ToStringCreator append(String fieldName, byte value) {
return append(fieldName, new Byte(value));
}

一係列不同類型的append方法
public String toString() {
//結束,並返回優美格式的String this.styler.styleEnd(this.buffer, this.object);
return this.buffer.toString();
}
Assert.notNull(obj, "The object to be styled is required");
this.object = obj;
this.styler = (styler != null ? styler : DEFAULT_TO_STRING_STYLER);
//開始 this.styler.styleStart(this.buffer, this.object);
}
public ToStringCreator append(String fieldName, byte value) {
return append(fieldName, new Byte(value));
}


public String toString() {
//結束,並返回優美格式的String this.styler.styleEnd(this.buffer, this.object);
return this.buffer.toString();
}
文章轉自莊周夢蝶 ,原文發布時間5.17
最後更新:2017-05-17 13:35:04