装饰模式java源码兑现(大话设计模式学习备忘录)

装饰模式java源码实现(大话设计模式学习备忘录)
package com.xangqun.decorator;

/**
 * <h1>装饰模式</h1>
 * QS:写一个可以给人搭配不同服饰的系统
 * @author xangqun
 * 
 */
public class Person {
  
	private String name;
	public Person(){}
	public Person(String name){
		this.name=name;
	}
	public void WearTShirts(){
		System.out.println("大T桖");
	}
	public void WearBigTrouser(){
		System.out.println("跨裤");
	}
	public void WearSneakers(){
		System.out.println("破球鞋");
	}
	public void WearTie(){
		System.out.println("领带");
	}
	public void WearSuit(){
		System.out.println("西装");
	}
	public void WearLeatherShoes(){
		System.out.println("皮鞋");
	}
	public void Show(){
		System.out.println("装扮的"+name);
	}
	/*
	 * PS:
	 * 没有面向对象设计,违背了开发-封闭原则
	 */
	public static void main(String[] args){
		Person p=new Person("xangqun");
		System.out.println("第一种装扮:");
		p.WearBigTrouser();
		p.WearSneakers();
		p.WearTShirts();
		p.Show();
		System.out.println("第二种装扮:");
		p.WearLeatherShoes();
		p.WearSuit();
		p.WearTie();
		p.Show();
	}
}

/*
 * 服饰父类
 */
public abstract class Finery {

	public abstract void Show();
}

public class Tshirts extends Finery {

	@Override
	public void Show() {
		System.out.println("大T桖");

	}

}

public class BigTrouser extends Finery {

	@Override
	public void Show() {
		System.out.println("跨裤");

	}

}

package com.xangqun.decorator;

/**
 * <h1>装饰模式</h1>
 * QS:写一个可以给人搭配不同服饰的系统
 * @author xangqun
 * 改进的程序1
 */
public class PersonTwo {

	private String name;
	public PersonTwo(){}
	public PersonTwo(String name){
		this.name=name;
	}
	public void Show(){
		System.out.println("装扮着:"+name);
	}
	/**
	 * PS:
	 *  这些服饰是一个一个显示出来的,应该要在内部组装完毕然后显示出来
	 *  建造者模式要求建造的过程必须是稳定的,现在这里是不稳定的
	 * @param args
	 */
	public static void main(String[] args) {
		PersonTwo pt=new PersonTwo("xangqun");
		System.out.println("第一种装扮:");
		Finery ts=new Tshirts();
		ts.Show();
		pt.Show();
		System.out.println("第一种装扮:");
		Finery bt=new BigTrouser();
		bt.Show();
		pt.Show();

	}

}

public class People {

	public People(){}
	private String name;
	public People(String name){
		this.name=name;
	}
	public  void Show(){
		System.out.println("装扮着:"+name);
	}
}

public class FineryTwo extends People {

	protected People component;
	public void Decorate(People component){
		this.component=component;
	}
	@Override
	public void Show() {
	if(component!=null){
		component.Show();
	}
	}
	
}

public class TshirtsTwo extends FineryTwo {

	@Override
	public void Show() {
		System.out.println("大T桖");
		super.Show();
	}

	
}

package com.xangqun.decorator;

/**
 * <h1>装饰模式</h1>
 * QS:写一个可以给人搭配不同服饰的系统
 * @author xangqun
 * 改进的程序2
 */
public class PersonThree {

	/**<b>装饰模式(decorator)</b>
	 * 动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更灵活。
	 *应用:当系统需要新功能的时候,是向旧的类中添加新的代码。这些新加的代码通常装饰了原有类的核心职责或主要行为
	 *     但是这种做法的问题在于,他们在主类中加入了新的字段,新的方法和新的逻辑,从而增加了主类的复杂度,
	 *     而装饰模式却提供了一个非常好的解决方案:他把每个要装饰的功能放在单独的类中,并让这个类包装他所要装饰的对象,
	 *     因此,当需要执行特殊的行为时,客户代码就可以在运行时根据需要有选择的,按顺序的使用装饰功能包装对象
	 *优点:把类的装饰功能从类中剥离去除,这样可以简化原有的类,有效地把类的核心职责和装饰功能区分开了,而且可以去除相关类中重复的装饰逻辑
	 *
	 * @param args
	 */
	public static void main(String[] args) {
		People p=new People("xangqun");
		System.out.println("第一种装扮:");
		TshirtsTwo ts=new TshirtsTwo();
		BigTrouserTwo gt=new BigTrouserTwo();
		
		ts.Decorate(p);
		gt.Decorate(ts);
		gt.Show();

	}

}