class Invoice{
    public void printInvoice(){
        System.out.println("this is content");
    }
}

class Decorator extends Invoice{
    protected Invoice ticket;
    public Decorator (Invoice t){
        ticket=t;
    }
    public void printInvoice(){
        if(ticket!=null){
            ticket.printInvoice();
        }
    }
}

class HeaderDecorator extends Decorator{
        
    public HeaderDecorator(Invoice t){
        super(t);
    }
    public void printInvoice(){
        System.out.println("this is the header");
        super.printInvoice();
    }
    
}

class FooterDecorator extends Decorator{
    
    public FooterDecorator(Invoice t){
        super(t);
    }
    public void printInvoice(){
        
        super.printInvoice();
        System.out.println("this is the footer");
    }
    
}



public class zhuangshi {
    
    public static void main(String[] args){
        Invoice t =new Invoice();
        Invoice ticket;
        ticket=new FooterDecorator(new HeaderDecorator(t));
        ticket.printInvoice();
        System.out.println("=====================");
        ticket=new FooterDecorator(new HeaderDecorator(new Decorator(null)));
        ticket.printInvoice();
    }

}

结果

图片描述


牙小木木
1.5k 声望80 粉丝

iamtb.cn


引用和评论

0 条评论