莊周夢蝶

          生活、程序、未來
             :: 首頁 ::  ::  :: 聚合  :: 管理

          org.springframework.core.styler包解讀

          Posted on 2007-04-06 15:18 dennis 閱讀(1136) 評論(0)  編輯  收藏 所屬分類: java源碼解讀
              這個包的說明是:Support for styling values as Strings, with ToStringCreator as central class.
          這個包簡單來說就是提供一個pretty-printing功能的輔助類,而ToStringCreator就是用于產(chǎn)生一個可以輸出經(jīng)過美化的value信息的toString()方法。使用方法參照spring的Test可以看到是這樣:   
                  int[] integers = new int[] { 01234 };
                  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());
              
          輸出:
          [@18558d2 array<Integer>[123456789]]

              如果你接觸過ruby,你應(yīng)該很熟悉Object.inpsect這個功能,這里通過ToStringCreator包裝的toString()方法也是產(chǎn)生類似的能夠清晰顯示對象內(nèi)部結(jié)構(gòu)信息的方法。spring應(yīng)該是使用這些輔助類來報告清晰的錯誤信息或者提示信息。
              看看這個包的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和它們的相應(yīng)實現(xiàn)DefaultToStringStyler、DefaultValueStyler又是策略模式(Strategy)的應(yīng)用體現(xiàn)。ValueStyler和DefaultValueStyler之間不僅僅是策略模式,同時也是visitor模式,請看DefaultValueStyler中一系列重載的visit方法,這些visit方法訪問不同類型Value的內(nèi)部結(jié)構(gòu)并構(gòu)造pretty格式的String返回,提供給ToStringStyler使用。
              ToStringCreator是ToStringStyler的客戶,它使用ToStringStyler調(diào)用產(chǎn)生優(yōu)美格式打印,而ToStringStyler 其實又是使用ValueStyler是訪問每個不同類型的子元素并返回優(yōu)美格式的String。實現(xiàn)的相當(dāng)精巧和靈活:
             
          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() {
          //結(jié)束,并返回優(yōu)美格式的String        this.styler.styleEnd(this.buffer, this.object);
                  return this.buffer.toString();
              }

          主站蜘蛛池模板: 庄浪县| 怀来县| 子洲县| 屏南县| 青田县| 墨玉县| 紫金县| 建阳市| 兴业县| 长子县| 沂南县| 福海县| 开原市| 天门市| 海南省| 格尔木市| 阿荣旗| 海盐县| 登封市| 公安县| 枣阳市| 屏南县| 蛟河市| 吉木萨尔县| 辽宁省| 拜泉县| 新和县| 浦县| 郁南县| 明溪县| 镇宁| 连城县| 石泉县| 萍乡市| 延安市| 惠安县| 丰顺县| 谢通门县| 涞源县| 金乡县| 邻水|