大话设计模式_装饰模式

小菜要去见漂亮女孩,不知道穿什么衣服好,就开始一件一件的开始搭配,实验出最好效果的搭配...

package com.wzs.design;

/**
 * 大话设计模式--page53 装饰模式
 * 
 * @author Administrator
 * 
 */
public class DecorativePattern {
	public static void main(String[] args) {
		Person xc = new Person("小菜");
		System.out.println("第一种装扮:");

		WearSneakers pqx = new WearSneakers();// 破球鞋
		BigTrouser kk = new BigTrouser();// 垮裤
		Tshirts dtx = new Tshirts(); // 大体恤

		pqx.decorate(xc);
		kk.decorate(pqx);
		dtx.decorate(kk);
		dtx.show();

		System.out.println("\n\n第二种装扮:");

		WearLeatherShoes px = new WearLeatherShoes();// 皮鞋
		WearSuit xz = new WearSuit();// 西服
		WearTie ld = new WearTie();// 领带

		px.decorate(xc);
		ld.decorate(px);
		xz.decorate(ld);
		xz.show();
	}
}

/*
 * 人
 */
class Person {
	private String name;

	public Person() {
	}

	public Person(String name) {
		this.name = name;
	}

	public void show() {
		System.out.print(name + "的装扮.");
	}
}

/*
 * 服饰
 */
class Finery extends Person {
	private Person person;

	/*
	 * 装扮
	 */
	public void decorate(Person person) {
		this.person = person;
	}

	@Override
	public void show() {
		if (null != person) {
			person.show();
		}
	}
}

/*
 * 大T恤
 */
class Tshirts extends Finery {

	@Override
	public void show() {
		System.out.print("大T恤  ");
		super.show();
	}
}

/*
 * 垮裤
 */
class BigTrouser extends Finery {

	@Override
	public void show() {
		System.out.print("垮裤  ");
		super.show();
	}
}

/*
 * 破球鞋
 */
class WearSneakers extends Finery {

	@Override
	public void show() {
		System.out.print("破球鞋  ");
		super.show();
	}
}

/*
 * 西装
 */
class WearSuit extends Finery {

	@Override
	public void show() {
		System.out.print("西装  ");
		super.show();
	}
}

/*
 * 领带
 */
class WearTie extends Finery {

	@Override
	public void show() {
		System.out.print("领带  ");
		super.show();
	}
}

/*
 * 皮鞋
 */
class WearLeatherShoes extends Finery {

	@Override
	public void show() {
		System.out.print("皮鞋  ");
		super.show();
	}
}

输出结果:

第一种装扮:
大T恤  垮裤  破球鞋  小菜的装扮.

第二种装扮:
西装  领带  皮鞋  小菜的装扮.


你可能感兴趣的:(设计模式,装饰模式,大话设计模式,小菜见美女)