标签:round 图片 翻译 开闭原则 imp https static abstract code
? Component: 抽象构件
? ConcreteComponent: 具体构件
? Decorator: 抽象装饰类
? ConcreteDecorator: 具体装饰类
1 /** 2 * Component 3 * 抽象构件 4 */ 5 public interface Person { 6 7 void eat(); 8 } 9 10 /** 11 * ConcreteComponent 12 * 具体构件 13 */ 14 public class Man implements Person { 15 16 @Override 17 public void eat() { 18 System.out.println("男人在吃"); 19 } 20 } 21 22 /** 23 * Decorator 24 * 抽象装饰类 25 */ 26 public abstract class Decorator implements Person { 27 28 protected Person person; 29 30 public void setPerson(Person person) { 31 this.person = person; 32 } 33 34 @Override 35 public void eat() { 36 person.eat(); 37 } 38 } 39 40 /** 41 * ConcreteDectrator 42 * 具体装饰类 43 */ 44 public class ManDecoratorA extends Decorator { 45 46 @Override 47 public void eat() { 48 super.eat(); 49 reEat(); 50 System.out.println("ManDecoratorA类"); 51 } 52 53 public void reEat() { 54 System.out.println("再吃一顿饭"); 55 } 56 } 57 58 59 /** 60 * ConcreteDectrator 61 * 具体装饰类 62 */ 63 public class ManDecoratorB extends Decorator { 64 65 @Override 66 public void eat() { 67 super.eat(); 68 System.out.println("==============="); 69 System.out.println("ManDecoratorB类"); 70 } 71 } 72 73 74 /** 75 * client 76 */ 77 public class Client { 78 79 public static void main(String[] args) { 80 Man man = new Man(); 81 ManDecoratorA md1 = new ManDecoratorA(); 82 ManDecoratorB md2 = new ManDecoratorB(); 83 84 md1.setPerson(man); 85 md2.setPerson(md1); 86 md2.eat(); 87 } 88 }
标签:round 图片 翻译 开闭原则 imp https static abstract code
原文地址:https://www.cnblogs.com/756623607-zhang/p/9222370.html