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]]</integer> " , str);
或者写个简单例子感受下:
int  [] a = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 };
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 toString()'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 toString()'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使用。
    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();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值