/*
装饰设计模式:
当想要对已有的对象惊醒功能增强时,
可以定义类,将已有对象传入,基于已有的功能,并提供加强功能。
那么自定义的该类称为装饰类。
装饰类通常会通过构造方法接受被装饰的对象。
并基于被装饰的对象的功能,提供更强的功能。
*/
class Person{
public void eat(){
System.out.println("eat");
}
}
class SuperPerson{
private Person p;
SuperPerson(Person p){
this.p = p;
}
public void superEat(){
System.out.println("soup");
p.eat();
System.out.println("Dessert");
}
}
public class DecoratorTest {
public static void main(String[] args) {
Person p = new Person();
SuperPerson sp = new SuperPerson(p);
sp.superEat();
}
}
装饰设计模式总结
最新推荐文章于 2024-05-06 09:42:21 发布