装饰者模式后续理解消化中
/** * */ package decorator2; /** * @author wangjian2 * */ public interface InterfaceComponent { public void say(); }
package decorator2; public class Component implements InterfaceComponent { @Override public void say() { System.out.println("Component.say():原组件的方法!"); } }
package decorator2; public abstract class AbstractDecorator implements InterfaceComponent { private InterfaceComponent component; public AbstractDecorator(InterfaceComponent component) { this.component = component; } /** * 组件方法执行前预处理方法 * */ protected void preSay() { }; /** * 组件方法执行后处理方法 * */ protected void afterSay() { }; public void say() { preSay(); component.say(); afterSay(); }; }
package decorator2; public class DecoratorTwo extends AbstractDecorator { public DecoratorTwo(InterfaceComponent component) { super(component); // TODO 自动生成构造函数存根 } /** * 根据需要重载模板类preSay()方法 */ protected void preSay() { System.out.println("DecoratorTwo.preSay():装饰者二的preSay()方法!"); } /** * 根据需要重载模板类afterSay()方法 */ protected void afterSay() { System.out.println("DecoratorTwo.afterSay():装饰者二的afterSay()方法!"); } }
package decorator2; public class DecoratorOne extends AbstractDecorator { public DecoratorOne(InterfaceComponent component) { super(component); // TODO 自动生成构造函数存根 } /** * 根据需要重载模板类preSay()方法 */ protected void preSay() { System.out.println("DecoratorOne.preSay():装饰者一的preSay()方法!"); } /** * 根据需要重载模板类afterSay()方法 */ protected void afterSay() { System.out.println("DecoratorOne.afterSay():装饰者一的afterSay()方法!"); } /** * 测试方法 * * @param args */ public static void main(String[] args) { // TODO 自动生成方法存根 InterfaceComponent interfaceComponent = new DecoratorTwo( new DecoratorOne(new Component())); interfaceComponent.say(); /* * 控制台输出: DecoratorTwo.preSay():装饰者二的preSay()方法! * DecoratorOne.preSay():装饰者一的preSay()方法! Component.say():原组件的方法! * DecoratorOne.afterSay():装饰者一的afterSay()方法! * DecoratorTwo.afterSay():装饰者二的afterSay()方法! */ } }
执行顺序:
interfaceComponent.say()=》DecoratorTwo继承AbstractDecorator的say()方法,并执行该方法=》执行DecoratorTwo的preSay方法=》
DecoratorTwo的构造方法的参数component是DecoratorOne,所以执行DecoratorOne的say方法=》
执行DecoratorOne的preSay方法=》执行Component类的say()方法=》执行DecoratorOne的afterSay方法=》
执行DecoratorTwo的afterSay方法
输出结果:
DecoratorTwo.preSay():装饰者二的preSay()方法!
DecoratorOne.preSay():装饰者一的preSay()方法!
Component.say():原组件的方法!
DecoratorOne.afterSay():装饰者一的afterSay()方法!
DecoratorTwo.afterSay():装饰者二的afterSay()方法!
二、另一种装饰者模式代码
/** * */ package decorator3; /** * @author wangjian2 * */ public interface Component { public double price(); }
/** * */ package decorator3; /** * @author wangjian2 * */ public class AllNeedMilk implements Component { private double price = 10.5; @Override public double price() { // TODO Auto-generated method stub return this.price; } }
/** * */ package decorator3; /** * @author wangjian2 * */ public abstract class Fruit implements Component { private Component component; public Fruit(Component component) { this.component = component; } protected double newPrice() { return 0; } public double price(){ return newPrice()+component.price(); } }
/** * */ package decorator3; /** * @author wangjian2 * */ public class Apple extends Fruit { public Apple(Component component) { super(component); // TODO Auto-generated constructor stub } protected double newPrice() { return 20; } }
/** * */ package decorator3; /** * @author wangjian2 * */ public class Orange extends Fruit { public Orange(Component component) { super(component); // TODO Auto-generated constructor stub } protected double newPrice() { return 30; } }
/** * */ package decorator3; /** * @author wangjian2 * */ public class Test { /** * @param args */ public static void main(String[] args) { Component apple = new Apple(new Orange(new AllNeedMilk())); System.out.println(apple.price()); Component orange = new Orange(new AllNeedMilk()); System.out.println(orange.price()); } }
输出结果:
60.5
40.5