//抽象构件角色
abstract public class Component{
abstract public void prtTicket();
}
//具体构建角色
public class SaleTicket extend Component{
public void prtTicket(){
System.out.println("Sale Ticket" );
}
}
//抽象装饰者角色
abstract public class TicketDecorator extend Component {
private Component myTrailer;
public TicketDecorator(Component comp){
myTrailer = comp;
}
public void callTrailer(){
if (myTrailer != null )
myTrailer.prtTicket();
}
}
}
//具体装饰角色header
public class Header extend TicketDecorator{
public Header(Component comp){
super(comp);
}
public void prtTicket(){
System.out.println("Header" );
super.callTrailer();
}
}
//具体装饰角色footer
public class Footer extend TicketDecorator{
public Footer (Component comp){
super(comp);
}
public void prtTicket(){
super.callTrailer();
System.out.println("Footer" );
}
}
//测试
public class Test{
public static void main(String args[]){
Component c = new Header( new Footer( new SaleTicket()));
c.prtTicket();
}
}
java设计模式 -Decorator
最新推荐文章于 2024-08-22 22:06:48 发布