莊周夢蝶

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

          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();
              }

          主站蜘蛛池模板: 东乡族自治县| 和林格尔县| 五河县| 阳原县| 永靖县| 都兰县| 淮滨县| 饶河县| 子长县| 吉林省| 舒城县| 中西区| 阜城县| 莆田市| 西乌珠穆沁旗| 玉树县| 通州区| 石河子市| 虹口区| 石楼县| 洱源县| 若尔盖县| 久治县| 宜阳县| 鄄城县| 故城县| 行唐县| 叙永县| 巴彦淖尔市| 法库县| 郓城县| 奎屯市| 南乐县| 合作市| 武清区| 闽清县| 青阳县| 五莲县| 潜江市| 博兴县| 揭阳市|