标签:修饰模式
public abstract class SchoolReport { public abstract void report(); public abstract void sign(String name); }
public class FourthGradeSchoolReport extends SchoolReport { @Override public void report() { System.out.println("尊重的xxx"); System.out.println("..............."); System.out.println("语文:60 数学:68 自然:87"); } public void sign(String name) { System.out.println("家长签字:"+name); } }
public abstract class Decorator extends SchoolReport { private SchoolReport sr; public Decorator(SchoolReport sr){ this.sr=sr; } @Override public void report() { this.sr.report(); } @Override public void sign(String name) { this.sr.sign(name); } }
public class HighScoreDecorator extends Decorator { public HighScoreDecorator(SchoolReport sr) { super(sr); } private void reportHighScore(){ System.out.println("这次考试语文最高70,数学最高70,自然最高100"); } @Override public void report() { this.reportHighScore(); super.report(); } }
public class SortDecorator extends Decorator { public SortDecorator(SchoolReport sr) { super(sr); } private void reportSort(){ System.out.println("我排名38"); } @Override public void report() { super.report(); this.reportSort(); } }
public static void main(String[] args) { SchoolReport sr; sr=new FourthGradeSchoolReport(); sr=new HighScoreDecorator(sr); sr=new SortDecorator(sr); sr.report(); sr.sign("Dad"); //这样老爸自然就开心的签字了 }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:修饰模式
原文地址:http://blog.csdn.net/cjvs9k/article/details/46971785