能夠給人搭配嘻哈服或白領裝的程序。ide
/** * 人類 * 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恤。。只須要把所需的功能按正確的順序串聯起來進行控制便可。ui
動態地給一個對象添加一些額外的職責,就增長來講,裝飾模式比生成子類更爲靈活this
/** * 究極進化人類(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