装饰设计模式,可以在原有技能的基础上,新增技能,降低继承所带来的耦合性,具体细节详见代码:
package test1;
/**
* 装饰设计模式
* @author pecool
*
*/
public class Test {
public static void main(String[] args) {
HeiMa heima = new HeiMa(new Student());
heima.code();
}
}
/*
* code接口
*/
interface Code{
public void code();
}
/*
* 学生从学校出来,所会技能
*/
class Student implements Code{
public void code(){
System.out.println("javase");
System.out.println("javaweb");
}
}
/*
* 黑马培训机构包装后的技能
*/
class HeiMa implements Code {
private Student student;
//构造方法中传入学生对象
public HeiMa(Student student){
this.student = student;
}
//新的技能
@Override
public void code() {
student.code();
System.out.println("oracle");
System.out.println("大数据");
System.out.println("云计算");
}
}
Best Regards
浙公网安备 33010602011771号