裝飾者模式以對客戶端透明的方式動態(tài)的為對象增加責任。此模式提供了一個比繼承更為靈活的替代方案來擴展對象的功能,避免了繼承方法產(chǎn)生的類激增問題,而且更方便更改對象的責任。 
  我們經(jīng)常要為某一些個別的對象增加一些新的職責,并不是全部的類。例如我們系統(tǒng)留言反饋板塊中可能需要過濾用戶輸入留言中的一些詞匯(例如政治敏感詞匯、色情詞匯等)、還可能對用戶輸入留言進行一些修飾(例如對用戶輸入的url自動加上超鏈接、對用戶輸入的ubb代碼進行轉(zhuǎn)換的)、還可能將用戶輸入的內(nèi)容定時發(fā)送的網(wǎng)管的郵箱中等等。如果使用類繼承的方式進行設計,我們可能要設計一個接口
  bodycontentfilterintf,然后在由bodycontentfilterintf派生出sensitivewordcontentfilter、htmlcontentfilter、sendemailcontentfilter等類。但是如果還要要求同時能過濾敏感詞匯并能進行修飾、或者過濾敏感詞匯之后把用戶輸入的留言發(fā)送到網(wǎng)管郵箱等等,這樣就要增加sensitivewordhtmlcontentfilter、sensitivewordsendemaillcontentfilter等類,這種方式導致了子類瀑發(fā)式的產(chǎn)生。
  一個靈活的方法是將過濾器嵌入另一個過濾器中,由這個過濾器來負責調(diào)用被嵌入過濾器的方法并執(zhí)行自己的過濾器方法。我們稱這個嵌入的過濾器為裝飾(decorator)。這個裝飾與過濾器接口一致。裝飾將請求向前轉(zhuǎn)到到另一個過濾器,并且可能能轉(zhuǎn)發(fā)前后執(zhí)行一些額外的動作(如修飾、發(fā)送郵件),透明性使你可以遞歸的嵌套多個裝飾,從面可以添加任意多的功能。
  其實java中的過濾器模式應用非常多,典型的就是io的stream操作。在io處理中,java將數(shù)據(jù)抽象為流(stream)。在io庫中,最基本的是inputstream和outputstream兩個分別處理輸出和輸入的對象,但是在inputstream和outputstream中之提供了最簡單的流處理方法,只能讀入/寫出字符,沒有緩沖處理,無法處理文件,等等。
  linenumberinputstream、bufferinputstream、stringbufferinputstream等提供各種不同服務的類只要組合起來就可以實現(xiàn)很多功能,如下:
filterinputstream mystream=new linenumberinputstream
( new bufferinputstream( new stringbufferinputstream( mystringbuffer)));
  多個的decorator被層疊在一起,最后得到一個功能強大的流。既能夠被緩沖,又能夠得到行數(shù),這就是decorator的威力!
  下面是我們的類靜態(tài)圖

  我們定義一個接口bodycontentfilterintf 來定義所有過濾器要實現(xiàn)的方法:
public interface bodycontentfilterintf {
 public string filtcontent(string acontent) throws contentfilterexception;
}
  這個接口中只有一個方法filtcontent,將要過濾的留言傳給acontent參數(shù),filtcontent對acontent進行一些處理(如裝飾url、ubb等),然后將處理后的字符串做為返回值返回;如果留言沒有通過過濾(如含有敏感詞匯等),只要拋出自定義contentfilterexception異常即可。
  下面是一個可能的一個過濾器(保證輸入的字數(shù)多于50):
public class lengthcontentfilter
implements bodycontentfilterintf {
 private bodycontentfilterintf bodycontentfilterintf = null;
 public htmlcontentfilter(bodycontentfilterintf afilter)
 {
  bodycontentfilterintf = afilter;
 }
 public string filtcontent(string acontent) throws contentfilterexception {
  string l_content = acontent;
  if (bodycontentfilterintf!=null) 
   _content = bodycontentfilterintf .filtcontent(l_content); 
  if (acontent.length()<=50)
   throw new contentfilterexception (“輸入的字數(shù)不能少于50!”);
    return acontext;
 }
}
  這是另一個過濾器(偽碼,用來實現(xiàn)向網(wǎng)管郵箱發(fā)送郵件) public class sendemailcontentfilter
implements bodycontentfilterintf {
 private bodycontentfilterintf bodycontentfilterintf = null;
 public sendemailcontentfilter(bodycontentfilterintf afilter)
 {
  bodycontentfilterintf = afilter;
 }
 public string filtcontent(string acontent) throws contentfilterexception {
  string l_content = acontent;
  if (bodycontentfilterintf!=null) 
   l_content = bodycontentfilterintf .filtcontent(l_content); 
   sendemail(“[email protected]”,l_content)
  return acontext;
 }
}
  當然還有sensitivewordcontextfilter(過濾敏感詞匯),htmlcontentfilter(修飾用戶輸入留言中的超級鏈接)等。
  有了這些過濾器,我們就可以很方便的為留言版添加各種復合的過濾器。例如我們想對輸入的留言進行超鏈接修飾和過濾敏感詞匯,那么我們只要如下調(diào)用即可:
try {
 l_content = new htmlcontentfilter(new sensitivewordcontextfilter(null)).
 filtcontent(bodycontext);
}
catch (contentfilterexception ex) {
 bbscommon.showmsginresponse(response, ex.getmessage());
 return;
}
  我們甚至可以動態(tài)的添加不同的過濾器,例如對于會員我們要對輸入的留言進行超鏈接修飾并且將他的留言發(fā)送到網(wǎng)管郵箱,而對于非會員我們則要過濾他輸入的敏感詞匯并且保證輸入的字數(shù)不少于50,我們只要如下調(diào)用即可:
try {
 bodycontentfilterintf bodycontentfilterintf = null;
 bodycontentfilterintf = new htmlcontentfilter(null);
 if(ismember==true)
  bodycontentfilterintf = new sendemailcontentfilter(bodycontentfilterintf);
 else
  bodycontentfilterintf = new sensitivewordcontextfilter(bodycontentfilterintf); 
 l_content = bodycontentfilterintf.filtcontent(bodycontext);
}
catch (contentfilterexception ex) {
 bbscommon.showmsginresponse(response, ex.getmessage());
 return;
}