测试类:
1 public class Test { 2 3 public static void main(String[] args) { 4 Man man = new Man(); 5 ManDecoratorA md1 = new ManDecoratorA(); 6 ManDecoratorB md2 = new ManDecoratorB(); 7 8 md1.setPerson(man); 9 md2.setPerson(md1); 10 md2.eat(); 11 } 12 } 1 public interface Person { 2 3 void eat(); 4 } 1 public class Man implements Person { 2 3 public void eat() { 4 System.out.println("男人在吃"); 5 } 6 } 1 public abstract class Decorator implements Person { 2 3 protected Person person; 4 5 public void setPerson(Person person) { 6 this.person = person; 7 } 8 9 public void eat() { 10 person.eat(); 11 } 12 } 1 public class ManDecoratorA extends Decorator { 2 3 public void eat() { 4 super.eat(); 5 reEat(); 6 System.out.println("ManDecoratorA类"); 7 } 8 9 public void reEat() { 10 System.out.println("再吃一顿饭"); 11 } 12 } 1 public class ManDecoratorB extends Decorator { 2 3 public void eat() { 4 super.eat(); 5 System.out.println("==============="); 6 System.out.println("ManDecoratorB类"); 7 } 8 }
用了大牛的代码做笔记,学习观摩不停进步...用了这么多框架,源码还是不易看懂啊
转载于:https://www.cnblogs.com/huzi007/p/4045278.html