//定义人(物件)
public class Person {
private String name;
public Person(){
}
public Person(String name){
this.name = name;
}
public void Show() {
System.out.println("装饰" + this.name);
}
}
//装饰抽象类
//需继承对象,从外类拓展对象类功能
public class Finery extends Person {
protected Person component;
//打扮
public void Decorate(Person component){
this.component = component;
}
@Override
public void Show(){
if(component!=null){
component.Show();
}
}
}
//给对象添加职责功能
public class Suit extends Finery {
@Override
public void Show() {
// TODO Auto-generated method stub
System.out.println("西装");
super.Show();
//System.out.println("西装");
}
}
。。。。。。
//主类,先定义对象,然后定义各种装饰类,装饰类中依次加入对象,最后展示类。
public static void main(String[] args) {
// TODO Auto-generated method stub
Person person =new Person("Tom");
System.out.println("第一种装饰");
Sneakers sneakers = new Sneakers();
TBigTrouser tBigTrouser =new TBigTrouser();
TShirts tShirts = new TShirts();
sneakers.Decorate(person);
tBigTrouser.Decorate(sneakers);
tShirts.Decorate(tBigTrouser);
tShirts.Show();
System.out.println("第二种装饰");
LeatherShoes leatherShoes = new LeatherShoes();
Tie tie = new Tie();
Suit suit = new Suit();
leatherShoes.Decorate(person);
tie.Decorate(leatherShoes);
suit.Decorate(tie);
suit.Show();
}