學習模式要善於變通,若是隻有一個ConcreteComponent類而沒有抽象的Component類,那麼Decorator類能夠是ConcreteComponent的一個子類。
一樣道理,若是隻有一個ConcreteDecorator類,那麼就沒有必要創建一個單獨的Decorator類,只保留一個ConcreteDecorator類便可。
在這裏咱們就不必有Component類了,直接讓服飾類Decorator繼承人類ConcreteComponent就可。java
/** * @CreateTime: 2021/02/17 12:13 * @Description: 裝飾模式:人穿衣服 */ public class PersonDecorator { private String name; public static void main(String[] args) { PersonDecorator pd = new PersonDecorator("小菜"); // 一、普通方式 // System.out.println("\n第一種裝扮:"); // pd.wearTShirts(); // pd.wearTrouser(); // pd.wearSneakers(); // pd.show(); // // System.out.println("\n第二種裝扮:"); // pd.wearSuit(); // pd.wearTie(); // pd.wearLeatherShoes(); // pd.show(); // 二、繼承 // System.out.println("\n第一種裝扮:"); // Finery shirts = new TShirts(); // Finery trouser = new Trouser(); // Finery sneakers = new Sneakers(); // // shirts.show(); // trouser.show(); // sneakers.show(); // pd.show(); // // System.out.println("\n第二種裝扮:"); // Finery suit = new Suit(); // Finery tie = new Tie(); // Finery shoes = new LeatherShoes(); // // suit.show(); // tie.show(); // shoes.show(); // pd.show(); // 三、裝飾模式 Person person = new Person("小菜"); System.out.println("\n第一種裝扮:"); Finery shirts = new TShirts(); Finery trouser = new Trouser(); Finery sneakers = new Sneakers(); // 裝飾過程 shirts.decorate(person); trouser.decorate(shirts); sneakers.decorate(trouser); sneakers.show(); System.out.println("\n第二種裝扮:"); Finery suit = new Suit(); Finery tie = new Tie(); Finery shoes = new LeatherShoes(); suit.decorate(person); tie.decorate(suit); shoes.decorate(tie); shoes.show(); } public PersonDecorator(String name) { this.name = name; } public void wearTShirts() { System.out.print("T恤 "); } public void wearTrouser() { 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); } } /** ConcreteComponent */ class Person { public Person() { } private String name = ""; public Person(String name) { this.name = name; } public void show() { System.out.println("裝扮的" + name); } } /** Decorator */ class Finery extends Person { private Person component = null; /** 打扮 */ public void decorate(Person component) { this.component = component; } /** 服飾展現 */ @Override public void show() { if (component != null) { component.show(); } } } class TShirts extends Finery { @Override public void show() { System.out.print("T恤 "); super.show(); } } class Trouser extends Finery { @Override public void show() { System.out.print("垮褲 "); super.show(); } } class Sneakers extends Finery { @Override public void show() { System.out.print("破球鞋 "); super.show(); } } class Suit extends Finery { @Override public void show() { System.out.print("西裝 "); super.show(); } } class Tie extends Finery { @Override public void show() { System.out.print("領帶 "); super.show(); } } class LeatherShoes extends Finery { @Override public void show() { System.out.print("皮鞋 "); super.show(); } }