发票基本类
java
public class Invoice {
public void printInvoice() {
System.out.println("打印发票正文");
}
}
发票正文类
java
public class Decorator extends Invoice {
protected Invoice ticket;
public Decorator(Invoice ticket) {
this.ticket = ticket;
}
@Override
public void printInvoice() {
if (ticket != null) {
ticket.printInvoice();
}
}
}
发票头部类
java
public class HeadDecorator extends Decorator {
public HeadDecorator(Invoice ticket) {
super(ticket);
}
@Override
public void printInvoice() {
System.out.println("打印发票头");
super.printInvoice();
}
}
发票尾部类
java
public class FootDecorator extends Decorator {
public FootDecorator(Invoice ticket) {
super(ticket);
}
@Override
public void printInvoice() {
super.printInvoice();
System.out.println("打印发票尾");
}
}
演示类
java
public class Demo {
public static void main(String[] args) {
Invoice t= new Invoice();
Decorator ticket;
ticket=new FootDecorator(new HeadDecorator(t));
ticket.printInvoice();
}
}
演示结果