设计模式:套路
装饰设计模式
主要功能是:在原有的被装饰者的基础上增加其他功能
在构造方法里传入了Man的对象
public class Demo{
public static void main(String[] args) {
Man man=new Man("小明",10);
man.eat();
man.sleep();
Sman sman=new Sman(man);
sman.eat();
sman.sleep();
}
}
interface Ani{
void eat();
void sleep();
}
class Man implements Ani{
private String name;
private int age;
Man(){
}
Man(String x,int y){
name=x;
age=y;
}
public void eat() {
System.out.println("吃");
}
public void sleep() {
System.out.println("睡觉");
}
}
class Sman implements Ani{
private Man man=null;
Sman(){
}
Sman(Man man){
this.man=man;
}
@Override
public void eat() {
// TODO Auto-generated method stub
System.out.println("超人");
man.eat();
}
@Override
public void sleep() {
// TODO Auto-generated method stub
System.out.println("超人");
man.sleep();
}
}