可以给人搭配嘻哈服或白领装的程序。
/** * 人类 * created by callmedevil on 2019/6/23. */ public class person { private string name; public person(string name) { this.name = name; } public void weartshirts(){ system.out.print("大t恤 "); } public void wearbigtrouser(){ system.out.print("垮裤 "); } public void wearsneakers(){ system.out.print("破球鞋 "); } public void wearsuit(){ system.out.print("西装 "); } public void weartie(){ system.out.print("领带 "); } public void wearleathershoes(){ system.out.print("皮鞋 "); } public void show(){ system.out.println("装扮的" + name); } }
/** * 装扮测试类 * created by callmedevil on 2019/6/23. */ public class test { public static void main(string[] args) { person devil = new person("devil"); system.out.println("第一种装扮:"); devil.weartshirts(); devil.wearbigtrouser(); devil.wearsneakers(); devil.show(); system.out.println("\n第二种装扮:"); devil.wearsuit(); devil.weartie(); devil.wearleathershoes(); devil.show(); } }
第一种装扮: 大t恤 垮裤 破球鞋 装扮的devil 第二种装扮: 西装 领带 皮鞋 装扮的devil
如果需要增加“超人”装扮,会导致需要修改“person”类,违背了开放-封闭原则
/** * 人类 * created by callmedevil on 2019/6/23. */ public class person { private string name; public person(string name){ this.name = name; } public void show(){ system.out.print("装扮的" + name); } }
/** * 服装抽象类 * created by callmedevil on 2019/6/23. */ public abstract class finery { public abstract void show(); }
/** * t恤 类 * created by callmedevil on 2019/6/23. */ public class tshirts extends finery { @override public void show() { system.out.print("大t恤 "); } }
/** * 垮裤 类 * created by callmedevil on 2019/6/23. */ public class bigtrouser extends finery { @override public void show() { system.out.print("垮裤 "); } } // 其余子类相似,此处省略
/** * 装饰升级版测试 * created by callmedevil on 2019/6/23. */ public class test { public static void main(string[] args) { person devil = new person("devil"); system.out.println("第一种装扮:"); finery tshirts = new tshirts(); finery bigtrouser = new bigtrouser(); finery sneakers = new sneakers(); tshirts.show(); bigtrouser.show(); sneakers.show(); devil.show(); system.out.println("\n第二种装扮:"); finery suit = new suit(); finery tie = new tie(); finery leathershoes = new leathershoes(); suit.show(); tie.show(); leathershoes.show(); devil.show(); } }
第一种装扮: 大t恤 垮裤 破球鞋 装扮的devil 第二种装扮: 西装 领带 皮鞋 装扮的devil
现在如果要加超人装扮,只要增加子类就可以了,但是这么做虽然把“服装”类和“人”类分离开了,仍然是存在问题的。把“大t恤”、“垮裤”、“破球鞋”和“装扮的devil”一个词一个词显示出来,就好比:你光着身子,当着大家的面,先穿t恤,再穿裤子,再穿鞋,仿佛在跳穿衣舞。。。因此需要一个房间(组合类)来换衣服,同时这个穿的顺序对每个人来说是不固定的,有的人喜欢先穿裤子,再穿鞋,最后穿t恤。。只需要把所需的功能按正确的顺序串联起来进行控制即可。
动态地给一个对象添加一些额外的职责,就增加来说,装饰模式比生成子类更为灵活
/** * 究极进化人类(concretecomponent) * created by callmedevil on 2019/6/23. */ public class person { private string name; public person(){} public person(string name){ this.name = name; } public void show(){ system.out.println("装扮的" + name); } }
/** * 究极进化 服饰类(decorator) * created by callmedevil on 2019/6/23. */ public abstract class finery extends person{ protected person component; /** * 装扮 * @param component */ public void decorate(person component) { this.component = component; } @override public void show() { if (component != null) { component.show(); } } }
/** * 究极进化 t恤(concretedecorator) * created by callmedevil on 2019/6/23. */ public class tshirts extends finery{ @override public void show() { system.out.print("大t恤 "); super.show(); } }
/** * 究极进化 垮裤(concretedecorator) * created by callmedevil on 2019/6/23. */ public class bigtrouser extends finery { @override public void show() { system.out.print("垮裤 "); super.show(); } } // 其余子类相似,此处省略
/** * 装饰模式测试 * created by callmedevil on 2019/6/23. */ public class test { public static void main(string[] args) { person devil = new person("devil"); system.out.println("第一种装扮:"); sneakers sneakers = new sneakers(); bigtrouser bigtrouser = new bigtrouser(); tshirts tshirts = new tshirts(); // 装饰 sneakers.decorate(devil); bigtrouser.decorate(sneakers); tshirts.decorate(bigtrouser); tshirts.show(); system.out.println("\n第二种装扮:"); leathershoes leathershoes = new leathershoes(); tie tie = new tie(); suit suit = new suit(); // 装饰 leathershoes.decorate(devil); tie.decorate(leathershoes); suit.decorate(tie); suit.show(); } }
第一种装扮: 大t恤 垮裤 破球鞋 装扮的devil 第二种装扮: 西装 领带 皮鞋 装扮的devil
如对本文有疑问, 点击进行留言回复!!
网友评论