使用Iterator模式将对象转成String.docxVIP

  • 48
  • 0
  • 约9.55千字
  • 约 13页
  • 2020-08-09 发布于天津
  • 举报
操纵JSOM、XML、Java bean等对象时你可能最先想到访问者模式。但是使用访问者模式很难从调 用代码控制回调。比如,不能有条件的从所有回调的子分支和叶子节点跳过某个分支。解决这个问题 就可以使用Iterator模式遍历整个对象,生成易于开发者阅读和调试的字符串。该迭代器具备一定的 通用性,我在使用 XPath查找Java对象和在StackHunter中记录异常的工具中都用到了它 API 本文主要介绍的两个类:StringGenerator 和ObjectIterator 。 字符串生成器 StringGenerator 工具类将对象转化为字符串,使对象可读性更好。可以用它来实现类的toString 方法或者把对象的字符串表达作为日志调试代码: package com.stackhunter.util.tostring.example; 2 import com.stackhunter.example.employee.Department; import com.stackhunter.example.employee.Employee; import com.stackhunter.example.employee.Manager; import com.stackhunter.example.people.Person; 7 8 import com.stackhunt

文档评论(0)

1亿VIP精品文档

相关文档