1、Report.java
package com.ssm.cts.pattern.decorator; public abstract class Report { public abstract void display(); }
2、MonthReport.java
package com.ssm.cts.pattern.decorator; public class MonthReport extends Report { @Override public void display() { System.out.println("财务月报:营业额:1亿。 利润:1000万"); } }
3、Decorator.java
package com.ssm.cts.pattern.decorator; public abstract class Decorator extends Report { private Report rep; public Decorator(Report _report){ this.rep = _report; } @Override public void display() { this.rep.display(); } }
4、RankDecorator.java
package com.ssm.cts.pattern.decorator; public class RankDecorator extends Decorator { public RankDecorator(Report rep){ super(rep); } private void reportRank(){ System.out.println("事业部排名:营业额第二名、利润第一名"); } @Override public void display(){ this.reportRank(); super.display(); } }
5、Demo.java
package com.ssm.cts.pattern.decorator; public class Demo { // 测试 public static void main(String[] args) { Report rep; // 月报 rep = new MonthReport(); // 增加排名 rep = new RankDecorator(rep); rep.display(); } }