第6章 穿什么有这么重要?——装饰模式(Java)
学习模式要善于变通,如果只有一个ConcreteComponent类而没有抽象的Component类,那么Decorator类可以是ConcreteComponent的一个子类。
同样道理,如果只有一个ConcreteDecorator类,那么就没有必要建立一个单独的Decorator类,只保留一个ConcreteDecorator类即可。
在这里我们就没必要有Component类了,直接让服饰类Decorator继承人类ConcreteComponent就可。
/**
* @CreateTime: 2021/02/17 12:13
* @Description: 装饰模式:人穿衣服
*/
public class PersonDecorator {
private String name;
public static void main(String[] args) {
PersonDecorator pd = new PersonDecorator("小菜");
// 1、普通方式
// System.out.println("\n第一种装扮:");
// pd.wearTShirts();
// pd.wearTrouser();
// pd.wearSneakers();
// pd.show();
//
// System.out.println("\n第二种装扮:");
// pd.wearSuit();
// pd.wearTie();
// pd.wearLeatherShoes();
// pd.show();
// 2、继承
// 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();
// 3、装饰模式
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();
}
}