设计模式----装饰者模式(decorator pattern)
SchoolReport.java代码如下:
package com.designPattern.decorator;public abstract class SchoolReport {public abstract void report();public abstract void sign(String name);}
package com.designPattern.decorator;public class FouthGradeSchoolReport extends SchoolReport{@Overridepublic void report() { System.out.println("========以下是成绩单原装内容!============"); System.out.println("+++++++++++++++++++++++++++++++++++"); System.out.println("+++++++++++++++++++++++++++++++++++"); System.out.println("+++++++++++++++++++++++++++++++++++"); System.out.println("+++++++++++++++++++++++++++++++++++"); System.out.println("+++++++++++++++++++++++++++++++++++"); System.out.println("========以上是成绩单原装内容!============");}@Overridepublic void sign(String name) {System.out.println("家长签名为:" +name);}}
package com.designPattern.decorator;public abstract class Decorator extends SchoolReport{private SchoolReport schoolReport;public Decorator(SchoolReport schoolReport){this.schoolReport =schoolReport;}public void report(){this.schoolReport.report();}public void sign(String name){this.schoolReport.sign(name);}}
package com.designPattern.decorator;public class HighScoreDecorator extends Decorator{public HighScoreDecorator(SchoolReport schoolReport) {super(schoolReport);}public void reportHighScore(){System.out.println("这次语文成绩最高是75,数学是78,自然是80");}public void report(){this.reportHighScore();super.report();}}
package com.designPattern.decorator;public class SortDecorator extends Decorator{public SortDecorator(SchoolReport schoolReport) {super(schoolReport);}private void reportSort(){System.out.println("我排名第38名");}@Overridepublic void report() {super.report();this.reportSort();}}
package com.designPattern.decorator;public class Father {public static void main(String[] args) {SchoolReport schoolReport;schoolReport = new FouthGradeSchoolReport();schoolReport = new HighScoreDecorator(schoolReport);schoolReport = new SortDecorator(schoolReport);schoolReport.report();schoolReport.sign("one'father");}}
这次语文成绩最高是75,数学是78,自然是80========以下是成绩单原装内容!============+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++========以上是成绩单原装内容!============我排名第38名家长签名为:one'father