裝飾者模式的核心思想在於
圖
IPacketCreator即裝飾接口
IPacketCreator的實現很簡單
public interface IPacketCreator {
public String handleContent()
}
PacketBodyCreator用於返回數據包的核心數據
public class PacketBodyCreator implements IPacketCreator{
@Override
public String handleContent() {
return
}
}
PacketDecorator維護核心組件component對象
public abstract class PacketDecorator implements IPacketCreator{
IPacketCreator component;
public PacketDecorator(IPacketCreator c){
ccomponent=c;
}
}
PacketHTMLHeaderCreator是具體的裝飾器
public class PacketHTMLHeaderCreator extends PacketDecorator{
public PacketHTMLHeaderCreator(IPacketCreator c) {
super(c)
}
@Override
public String handleContent() { //將給定數據封裝成HTML
StringBuffer sb=new StringBuffer()
sb
sb
sb
sb
sb
return sb
}
}
返回目錄
編輯推薦
Java程序設計培訓視頻教程
J
J
Visual C++音頻/視頻技術開發與實戰
Oracle索引技術
ORACLE
From:http://tw.wingwit.com/Article/program/Java/gj/201311/27823.html