这个包的说明是:Support for styling values as Strings, with ToStringCreator as central class.
这个包简单来说就是提供一个pretty-printing功能的辅助类,而ToStringCreator就是用于产生一个可以输出经过美化的value信息的toString()方法。使用方法参照spring的Test可以看到是这样:
输出:
如果你接触过ruby,你应该很熟悉Object.inpsect这个功能,这里通过ToStringCreator包装的toString()方法也是产生类似的能够清晰显示对象内部结构信息的方法。spring应该是使用这些辅助类来报告清晰的错误信息或者提示信息。
看看这个包的UML类图:
首先,你需要理解ToStringStyler和ValueStyle两个接口,ToStringStyler定义了描述一个输入的Value信息的基本模板方法:
ToStringCreator是ToStringStyler的客户,它使用ToStringStyler调用产生优美格式打印,而ToStringStyler 其实又是使用ValueStyler是访问每个不同类型的子元素并返回优美格式的String。实现的相当精巧和灵活:
这个包简单来说就是提供一个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));
}
一系列不同类型的append方法
public String toString() {
// 结束,并返回优美格式的String this.styler.styleEnd(this.buffer, this.object);
return this .buffer.toString();
}
文章转自庄周梦蝶 ,原文发布时间5.17