package basic; class Invoice{ public void printInvoice(){ System.out.println("this is the content of the invoice"); } } class Deco extends Invoice{ protected Invoice ticket; public Deco(Invoice t){ ticket=t; } public void printInvoice(){ if(ticket!=null){ ticket.printInvoice();; } } } class HeadDeco extends Deco{ public HeadDeco(Invoice t){ super(t); } public void printInvoice(){ System.out.println("this is the head of the invoice"); super.printInvoice(); } } class FootDeco extends Deco{ public FootDeco(Invoice t){ super(t); } public void printInvoice(){ super.printInvoice(); System.out.println("this is the foot of the invoice"); } } public class InvoiceDemo { public static void main(String[] args) { Invoice t=new Invoice(); Invoice ticket; ticket=new HeadDeco(new FootDeco(t)) ; ticket.printInvoice(); System.out.println("----------------"); ticket=new FootDeco(new HeadDeco(new Deco(null))); ticket.printInvoice(); } }
this is the head of the invoice this is the content of the invoice this is the foot of the invoice ---------------- this is the head of the invoice this is the foot of the invoice