标签:tor comm 结果 img .com auth contains color 图片
对原有对象进行修饰,如有一个篮子,现在对篮子进行修饰,放入苹果,香蕉,橙子
package constructional.pattern.decorator; /* * 创建一个对象的抽象也就是接口 */ public interface Basket { public void show(); }
package constructional.pattern.decorator; /** *身份:被装饰的对象 *一个对接口的实现,这个对象表示要我们将来要修饰的篮子里装内容,如果想修饰篮子的造型,还可以创建其他类实现Basket的接口,比如Shape * 不理解的话可以查看java语言的接口抽象机制 */ public class Original implements Basket{ public void show(){ System.out.println("The original Basket contains"); } }
package constructional.pattern.decorator; /** *身份:装饰器 *为原来的类添加新的功能 */ public class AppleDecorator implements Basket{ private Basket basket; public AppleDecorator( Basket basket){ super(); this.basket = basket; } public void show(){ basket.show(); System.out.println("An Apple"); } }
package constructional.pattern.decorator; /** *身份:装饰器 */ public class BananaDecorator implements Basket{ private Basket basket; public BananaDecorator(Basket basket){ super(); this.basket = basket; } public void show(){ basket.show(); System.out.println("A Banana"); } }
package constructional.pattern.decorator; /** *身份:装饰器 */ public class OrangeDecorator implements Basket{ private Basket basket; public OrangeDecorator(Basket basket){ super(); this.basket = basket; } public void show(){ basket.show(); System.out.println("An Oranage"); } }
测试代码如下:
package constructional.pattern.decorator; /* * 设计模式:装饰器模式 * java 简单的演示 */ /** * * @author octobershiner * 2011 7 25 * SE.HIT */ public class DecoratorPattern { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here Basket basket = new Original(); //一个装饰的过程 Basket myBasket =new AppleDecorator(new BananaDecorator(new OrangeDecorator(basket))); myBasket.show(); } }
运行结果:
标签:tor comm 结果 img .com auth contains color 图片
原文地址:https://www.cnblogs.com/liunianfeiyu/p/10116398.html