装饰者模式

类图:





/**
 * 饮料的抽象类
 */
public abstract class Beverage {
	
	protected String description = "Unknown Beverage";
	
	public String getDescription() {
		return description;
	}
	
	/**计算总价格的方法*/
	public abstract double cost();
	
}

/**
 * 饮料的子类 -》深培咖啡
 */
public class DarkRoast extends Beverage {

	public DarkRoast() {
		description = "DarkRoast";
	}

	@Override
	public double cost() {
		// TODO Auto-generated method stub
		return 0.99;
	}
}


/**
 *饮料的子类-》浓缩咖啡
 */
public class Espresso extends Beverage {
	
	public Espresso() {
		description = "Espresso";
	}
	/**
	 * @see com.tj.decorator.Beverage#cost()
	 */
	@Override
	public double cost() {
		// TODO Auto-generated method stub
		return 1.99;
	}
}


/**
 * 饮料的子类-》混合咖啡
 */
public class HouseBlend extends Beverage {
	private Beverage be;


	public HouseBlend( ) {
		this.be=be;
		description = "House Blend Coffee";
	}
	
	
	/**
	 * @see com.tj.decorator.Beverage#cost()
	 */
	@Override
	public double cost() {
		// TODO Auto-generated method stub
		return 0.89;
	}
}



/**装饰者类*/
public abstract class CondimentDecorator extends Beverage {
}



/**
 *摩卡-》装饰类
 */
public class Mocha extends CondimentDecorator {
	
	Beverage beverage;
	public Mocha(Beverage beverage) {
		this.beverage = beverage;
	}
	/**
	 * @see com.tj.decorator.CondimentDecorator#getDescription()
	 */
	@Override
	public String getDescription() {
		// TODO Auto-generated method stub
		return beverage.getDescription() + ", Mocha";
	}
	/**
	 * @see com.tj.decorator.Beverage#cost()
	 */
	@Override
	public double cost() {
		// TODO Auto-generated method stub
		return 0.20 + beverage.cost();
	}
}


/**
 *豆浆 -》装饰类
 */
public class Soy extends CondimentDecorator {

	Beverage beverage;

	public Soy(Beverage beverage) {
		this.beverage = beverage;
	}

	/**
	 * @see com.tj.decorator.CondimentDecorator#getDescription()
	 */
	@Override
	public String getDescription() {
		// TODO Auto-generated method stub
		return beverage.getDescription() + ", Soy";
	}

	/**
	 * @see com.tj.decorator.Beverage#cost()
	 */
	@Override
	public double cost() {
		return 0.15 + beverage.cost();
	}
}






/**
 *奶泡 -》装饰类
 */
public class Whip extends CondimentDecorator {
	
	Beverage beverage;
	public Whip(Beverage beverage) {
		this.beverage = beverage;
	}
	/**
	 * @see com.tj.decorator.CondimentDecorator#getDescription()
	 */
	@Override
	public String getDescription() {
		// TODO Auto-generated method stub
		return beverage.getDescription() + ", Whip";
	}
	/**
	 * @see com.tj.decorator.Beverage#cost()
	 */
	@Override
	public double cost() {
		// TODO Auto-generated method stub
		return 0.10 + beverage.cost();
	}
}




测试类:

public class Test {
	public static void main(String[] args) {
		System.out.println("-------单个物品-------");
		Beverage beverage = new Espresso();// 饮料 1.99
		System.out.println(beverage.getDescription() + " $" + beverage.cost());

		System.out.println("-----装饰3个物品的方式之一---------");
		Beverage beverage2 = new DarkRoast();// 饮料 0.99
		beverage2 = new Mocha(beverage2);// 装饰者子类 0.2
		beverage2 = new Mocha(beverage2);// 装饰者子类 0.2
		beverage2 = new Whip(beverage2);// 装饰者子类 0.1
		beverage2.cost();
		System.out.println(beverage2.getDescription() + " $" + beverage2.cost());
		
		
		System.out.println("-----装饰3个物品的方式之一---------");
		Mocha mocha = new Mocha(new Mocha(new Whip(new DarkRoast())));
		System.out.println(mocha.getDescription() + " $" + mocha.cost());
		
		
		System.out.println("-----装饰3个物品的方式之一---------");
		Beverage beverage3 = new HouseBlend();
		beverage3 = new Soy(beverage3);
		beverage3 = new Mocha(beverage3);
		beverage3 = new Whip(beverage3);
		System.out.println(beverage3.getDescription() + " $" + beverage3.cost());
	}
}


输出日志:
-------单个物品-------
Espresso $1.99
-----装饰3个物品的方式之一---------
DarkRoast, Mocha, Mocha, Whip $1.49
-----装饰3个物品的方式之一---------
DarkRoast, Whip, Mocha, Mocha $1.49
-----装饰3个物品的方式之一---------
House Blend Coffee, Soy, Mocha, Whip $1.34




再上一个例子:

/**抽象的人  被装饰的抽象类*/
public abstract class Human {

	public abstract double calcMoney();

	public void run() {
		System.out.println("在走路...");
	}

}



/**装饰者抽象类*/
public abstract class Decorator extends Human{

	public abstract double calcMoney();

}



/**
 * 具体的人,被装饰者。
 * @author hp
 *
 */
public class ConcreateHuman extends Human{
	
	private String name;

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

	public String getName() {
		return name;
	}


	@Override
	public double calcMoney() {
		//被装饰者自身携带了0大洋
		return 0;
	}

	@Override
	public void run() {
		System.out.println(name+"在商场散步...");
	}
	
}



/**
 * 具体的装饰者。   赚取30RMB
 */
public class ConcreateDecoratorA extends Decorator{
	
	private Human human;

	public ConcreateDecoratorA(Human human) {
		this.human=human;
	}
	
	@Override
	public void run() {
		super.run();
		System.out.println("ConcreateDecoratorA.run()");
	}
	
	
	@Override
	public double calcMoney() {
		return human.calcMoney()+30;
	}
	
	public void saleCoffer(){
		System.out.println("卖出一份咖啡");
	}

}



/***
 * 具体的装饰者。 赚取20RMB
 */
public class ConcreateDecoratorB extends Decorator {
	
	private Human human;

	public ConcreateDecoratorB(Human human) {
		this.human=human;
	}

	public void saleMilk() {
		System.out.println("卖出了 牛奶");
	}

	@Override
	public double calcMoney() {
		return human.calcMoney()+20;
	}

}





public class Test {

	public static void main(String[] args) {
		
		
/**方式1:*/		
		System.out.println("/**方式1:*/	");
		ConcreateHuman human = new ConcreateHuman("张无忌");
		human.run();
		System.out.println(human.getName()+"身上有:"+human.calcMoney()+"RMB");

		ConcreateDecoratorB concreateB = new ConcreateDecoratorB(human);
		concreateB.saleMilk();
		System.out.println(human.getName()+"身上有:"+concreateB.calcMoney()+"RMB");

		ConcreateDecoratorA concreateA = new ConcreateDecoratorA(concreateB);
		concreateA.saleCoffer();
		System.out.println(human.getName()+"身上有:"+concreateA.calcMoney()+"RMB");
		
		
		System.out.println("/**********************/");
		System.out.println("/**********************/");
/**方式2:*/		
		System.out.println("/**方式2:一般用于计算总价*/	");
		Human huamnZhang = new ConcreateDecoratorA(new ConcreateDecoratorB(new ConcreateHuman("张无忌")));
		
		System.out.println("身上有:"+huamnZhang.calcMoney()+"RMB");
		
		
		

	}

}


/**方式1:*/	
张无忌在商场散步...
张无忌身上有:0.0RMB
卖出了 牛奶
张无忌身上有:20.0RMB
卖出一份咖啡
张无忌身上有:50.0RMB
/**********************/
/**********************/
/**方式2:一般用于计算总价*/	
身上有:50.0RMB










你可能感兴趣的:(装饰者模式)