//本示例来自Jakarta Commons Cookbook public class PoliticalCandidate { private String lastName; private String firstName; private Date dateOfBirth; private BigDecimal moneyRaised; private State homeState; // get/set methods 省略 public void toString( ) { return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE) .append("lastName", lastName) .append("firstName", firstName) .toString( ); } } //结果输出 com.discursive.jccook.lang.builders.PoliticalCandidate@1cd2e5f[ lastName=Jefferson firstName=Thomas ] //下面是几个默认的style的打印结果 ToStringStyle.DEFAULT_STYLE com.discursive.jccook.lang.builders. PoliticalCandidate@1cd2e5f[lastName=Jefferson,firstName=Thomas] ToStringStyle.MULTI_LINE_STYLE com.discursive.jccook.lang.builders.PoliticalCandidate@1cd2e5f[ lastName=Jefferson firstName=Thomas ] ToStringStyle.NO_FIELD_NAMES_STYLE com.discursive.jccook.lang.builders.PoliticalCandidate@1cd2e5f[Jefferson,Thomas] ToStringStyle.SIMPLE_STYLE Jefferson,Thomas
像NO_FIELD_NAMES_STYLE,SIMPLE_STYLE这些都是ToStringTyle的内部类,继承自ToStringStyle,重写了一些属性。
下面看一下ToStringBuilder的源码:
package org.apache.commons.lang.builder; import org.apache.commons.lang.BooleanUtils; import org.apache.commons.lang.ObjectUtils; public class ToStringBuilder { //默认的样式 private static ToStringStyle defaultStyle = ToStringStyle.DEFAULT_STYLE; public static ToStringStyle getDefaultStyle() { return defaultStyle; } //下面几个方法都是简单的调用ReflectionToStringBuilder的toString方法 public static String reflectionToString(Object object) { return ReflectionToStringBuilder.toString(object); } public static String reflectionToString(Object object, ToStringStyle style) { return ReflectionToStringBuilder.toString(object, style); } public static String reflectionToString(Object object, ToStringStyle style, boolean outputTransients) { return ReflectionToStringBuilder.toString(object, style, outputTransients, false, null); } public static String reflectionToString( Object object, ToStringStyle style, boolean outputTransients, Class reflectUpToClass) { return ReflectionToStringBuilder.toString(object, style, outputTransients, false, reflectUpToClass); } public static void setDefaultStyle(ToStringStyle style) { if (style == null) { throw new IllegalArgumentException("The style must not be null"); } defaultStyle = style; } //最后输出的字符串缓存 private final StringBuffer buffer; private final Object object; private final ToStringStyle style; public ToStringBuilder(Object object) { this(object, getDefaultStyle(), null); } public ToStringBuilder(Object object, ToStringStyle style) { this(object, style, null); } public ToStringBuilder(Object object, ToStringStyle style, StringBuffer buffer) { if (style == null) { style = getDefaultStyle(); } if (buffer == null) { buffer = new StringBuffer(512); } this.buffer = buffer; this.style = style; this.object = object; //这里append上开始字符 style.appendStart(buffer, object); } //ToStringBuilder中的append方法都是委托给ToStringStyle进行处理的 //并且返回一个ToStringBuilder实例,这样就可以进行链式操作了。 //例如:new ToStringBuilder().append().append().append()...... public ToStringBuilder append(boolean value) { style.append(buffer, null, value); return this; } //---------------------------------------------------------------------------- public ToStringBuilder append(boolean[] array) { style.append(buffer, null, array, null); return this; } //---------------------------------------------------------------------------- public ToStringBuilder append(byte value) { style.append(buffer, null, value); return this; } //---------------------------------------------------------------------------- public ToStringBuilder append(byte[] array) { style.append(buffer, null, array, null); return this; } //---------------------------------------------------------------------------- public ToStringBuilder append(char value) { style.append(buffer, null, value); return this; } //---------------------------------------------------------------------------- public ToStringBuilder append(char[] array) { style.append(buffer, null, array, null); return this; } //---------------------------------------------------------------------------- public ToStringBuilder append(double value) { style.append(buffer, null, value); return this; } //---------------------------------------------------------------------------- public ToStringBuilder append(double[] array) { style.append(buffer, null, array, null); return this; } //---------------------------------------------------------------------------- public ToStringBuilder append(float value) { style.append(buffer, null, value); return this; } //---------------------------------------------------------------------------- public ToStringBuilder append(float[] array) { style.append(buffer, null, array, null); return this; } //---------------------------------------------------------------------------- public ToStringBuilder append(int value) { style.append(buffer, null, value); return this; } //---------------------------------------------------------------------------- public ToStringBuilder append(int[] array) { style.append(buffer, null, array, null); return this; } //---------------------------------------------------------------------------- public ToStringBuilder append(long value) { style.append(buffer, null, value); return this; } //---------------------------------------------------------------------------- public ToStringBuilder append(long[] array) { style.append(buffer, null, array, null); return this; } //---------------------------------------------------------------------------- public ToStringBuilder append(Object obj) { style.append(buffer, null, obj, null); return this; } //---------------------------------------------------------------------------- public ToStringBuilder append(Object[] array) { style.append(buffer, null, array, null); return this; } //---------------------------------------------------------------------------- public ToStringBuilder append(short value) { style.append(buffer, null, value); return this; } //---------------------------------------------------------------------------- public ToStringBuilder append(short[] array) { style.append(buffer, null, array, null); return this; } public ToStringBuilder append(String fieldName, boolean value) { style.append(buffer, fieldName, value); return this; } public ToStringBuilder append(String fieldName, boolean[] array) { style.append(buffer, fieldName, array, null); return this; } public ToStringBuilder append(String fieldName, boolean[] array, boolean fullDetail) { style.append(buffer, fieldName, array, BooleanUtils.toBooleanObject(fullDetail)); return this; } public ToStringBuilder append(String fieldName, byte value) { style.append(buffer, fieldName, value); return this; } public ToStringBuilder append(String fieldName, byte[] array) { style.append(buffer, fieldName, array, null); return this; } public ToStringBuilder append(String fieldName, byte[] array, boolean fullDetail) { style.append(buffer, fieldName, array, BooleanUtils.toBooleanObject(fullDetail)); return this; } public ToStringBuilder append(String fieldName, char value) { style.append(buffer, fieldName, value); return this; } public ToStringBuilder append(String fieldName, char[] array) { style.append(buffer, fieldName, array, null); return this; } public ToStringBuilder append(String fieldName, char[] array, boolean fullDetail) { style.append(buffer, fieldName, array, BooleanUtils.toBooleanObject(fullDetail)); return this; } public ToStringBuilder append(String fieldName, double value) { style.append(buffer, fieldName, value); return this; } public ToStringBuilder append(String fieldName, double[] array) { style.append(buffer, fieldName, array, null); return this; } public ToStringBuilder append(String fieldName, double[] array, boolean fullDetail) { style.append(buffer, fieldName, array, BooleanUtils.toBooleanObject(fullDetail)); return this; } public ToStringBuilder append(String fieldName, float value) { style.append(buffer, fieldName, value); return this; } public ToStringBuilder append(String fieldName, float[] array) { style.append(buffer, fieldName, array, null); return this; } public ToStringBuilder append(String fieldName, float[] array, boolean fullDetail) { style.append(buffer, fieldName, array, BooleanUtils.toBooleanObject(fullDetail)); return this; } public ToStringBuilder append(String fieldName, int value) { style.append(buffer, fieldName, value); return this; } public ToStringBuilder append(String fieldName, int[] array) { style.append(buffer, fieldName, array, null); return this; } public ToStringBuilder append(String fieldName, int[] array, boolean fullDetail) { style.append(buffer, fieldName, array, BooleanUtils.toBooleanObject(fullDetail)); return this; } public ToStringBuilder append(String fieldName, long value) { style.append(buffer, fieldName, value); return this; } public ToStringBuilder append(String fieldName, long[] array) { style.append(buffer, fieldName, array, null); return this; } public ToStringBuilder append(String fieldName, long[] array, boolean fullDetail) { style.append(buffer, fieldName, array, BooleanUtils.toBooleanObject(fullDetail)); return this; } public ToStringBuilder append(String fieldName, Object obj) { style.append(buffer, fieldName, obj, null); return this; } public ToStringBuilder append(String fieldName, Object obj, boolean fullDetail) { style.append(buffer, fieldName, obj, BooleanUtils.toBooleanObject(fullDetail)); return this; } public ToStringBuilder append(String fieldName, Object[] array) { style.append(buffer, fieldName, array, null); return this; } public ToStringBuilder append(String fieldName, Object[] array, boolean fullDetail) { style.append(buffer, fieldName, array, BooleanUtils.toBooleanObject(fullDetail)); return this; } public ToStringBuilder append(String fieldName, short value) { style.append(buffer, fieldName, value); return this; } public ToStringBuilder append(String fieldName, short[] array) { style.append(buffer, fieldName, array, null); return this; } public ToStringBuilder append(String fieldName, short[] array, boolean fullDetail) { style.append(buffer, fieldName, array, BooleanUtils.toBooleanObject(fullDetail)); return this; } //appendIdentityToString是一个被deprecated的方法,它的作用就是返回一个对象的类型+hashcode的字符串 public ToStringBuilder appendAsObjectToString(Object object) { ObjectUtils.appendIdentityToString(this.getStringBuffer(), object); return this; } //---------------------------------------------------------------------------- public ToStringBuilder appendSuper(String superToString) { if (superToString != null) { style.appendSuper(buffer, superToString); } return this; } public ToStringBuilder appendToString(String toString) { if (toString != null) { style.appendToString(buffer, toString); } return this; } public Object getObject() { return object; } public StringBuffer getStringBuffer() { return buffer; } //---------------------------------------------------------------------------- public ToStringStyle getStyle() { return style; } //最后append上结束字符,并返回最后的格式字符串 public String toString() { if (this.getObject() == null) { this.getStringBuffer().append(this.getStyle().getNullText()); } else { style.appendEnd(this.getStringBuffer(), this.getObject()); } return this.getStringBuffer().toString(); } }
我们可以看到非常清楚的SRP(单一职责原则)的应用。ToStringBuilder就是负责存储字符串,至于应该以什么格式存储字符串,则交给ToStringStyle去完成。所以这个类的大部分工作都委托给了ToStringStyle