码迷,mamicode.com
首页 > 其他好文 > 详细

设计模式 - 装饰者模式(Decorator Pattern) 具体解释

时间:2017-05-21 11:57:54      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:输出   关闭   override   设计   decorator   nbsp   抽象   end   代码   

装饰者模式(Decorator Pattern) 具体解释


本文地址: http://blog.csdn.net/caroline_wendy/article/details/26707033


装饰者模式(Decorator Pattern):动态地将责任附加到对象上. 若要扩展功能, 装饰者提供了比继承更有弹性的替代方案.


用法:


1. 首先创建组件(Component)父类, 全部类,详细组件(Concrete Component)装饰者(Decorator)都属于这一类型, 能够进行扩展;

能够是抽象类(abstract class), 也能够是接口(interface);

代码:

/**
 * @time 2014年5月23日
 */
package decorator;

/**
 * @author C.L.Wang
 *
 */
public abstract class Beverage {
	String description = "Unkown Beverage";
	
	public String getDescription() {
		return description;
	}
	
	public abstract double cost();
}

2. 装饰者(Decorator)类父类, 继承组件(component)父类, 可是不要实现接口函数, 由它的继承类(详细的装饰者)去实现,

能够是抽象类(abstract class), 也能够是接口(interface);

代码:

/**
 * @time 2014年5月23日
 */
package decorator;

/**
 * @author C.L.Wang
 *
 */
public abstract class CondimentDecorator extends Beverage {
	public abstract String getDescription();

}

3. 详细组件(Concrete Component), 即装饰者(Decorator)须要装饰的基础, 继承组件(Component)父类;

代码:

/**
 * @time 2014年5月23日
 */
package decorator;

/**
 * @author C.L.Wang
 *
 */
public class DarkRoast extends Beverage {

	public DarkRoast() {
		description = "Dark Roast";
	}
	
	/* (non-Javadoc)
	 * @see decorator.Beverage#cost()
	 */
	@Override
	public double cost() {
		// TODO Auto-generated method stub
		return 0.99;
	}

}


/**
 * @time 2014年5月23日
 */
package decorator;

/**
 * @author C.L.Wang
 *
 */
public class Decat extends Beverage {

	public Decat() {
		description = "Decat";
	}
	
	/* (non-Javadoc)
	 * @see decorator.Beverage#cost()
	 */
	@Override
	public double cost() {
		// TODO Auto-generated method stub
		return 1.05;
	}

}


/**
 * @time 2014年5月23日
 */
package decorator;

/**
 * @author C.L.Wang
 *
 */
public class Espresso extends Beverage {

	public Espresso() {
		description = "Espresso";
	}
	
	/* (non-Javadoc)
	 * @see decorator.Beverage#cost()
	 */
	@Override
	public double cost() {
		// TODO Auto-generated method stub
		return 1.99;
	}

}


/**
 * @time 2014年5月23日
 */
package decorator;

/**
 * @author C.L.Wang
 *
 */
public class HouseBlend extends Beverage {

	public HouseBlend() {
		description = "House Blend Coffee";
	}
	
	/* (non-Javadoc)
	 * @see decorator.Beverage#cost()
	 */
	@Override
	public double cost() {
		// TODO Auto-generated method stub
		return .89;
	}

}

4. 装饰者(Decorator)类继承装饰者父类, 实现组件父类的接口装饰者父类的接口.

代码:

/**
 * @time 2014年5月23日
 */
package decorator;

/**
 * @author C.L.Wang
 *
 */
public class Milk extends CondimentDecorator {

	Beverage beverage;
	
	public Milk(Beverage beverage) {
		this.beverage = beverage;
	}
	
	/* (non-Javadoc)
	 * @see decorator.CondimentDecorator#getDescription()
	 */
	@Override
	public String getDescription() {
		// TODO Auto-generated method stub
		return beverage.getDescription() + ", Milk";
	}

	/* (non-Javadoc)
	 * @see decorator.Beverage#cost()
	 */
	@Override
	public double cost() {
		// TODO Auto-generated method stub
		return 0.10 + beverage.cost();
	}

}


/**
 * @time 2014年5月23日
 */
package decorator;

/**
 * @author C.L.Wang
 *
 */
public class Mocha extends CondimentDecorator {

	Beverage beverage;
	
	public Mocha(Beverage beverage) {
		this.beverage = beverage;
	}
	
	/* (non-Javadoc)
	 * @see decorator.CondimentDecorator#getDescription()
	 */
	@Override
	public String getDescription() {
		// TODO Auto-generated method stub
		return beverage.getDescription() + ", Mocha";
	}

	@Override
	public double cost() {
		// TODO Auto-generated method stub
		return 0.20+beverage.cost();
	}

}


/**
 * @time 2014年5月23日
 */
package decorator;

/**
 * @author C.L.Wang
 *
 */
public class Soy extends CondimentDecorator {

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

	/* (non-Javadoc)
	 * @see decorator.Beverage#cost()
	 */
	@Override
	public double cost() {
		// TODO Auto-generated method stub
		return 0.15 + beverage.cost();
	}

}


/**
 * @time 2014年5月23日
 */
package decorator;

/**
 * @author C.L.Wang
 *
 */
public class Whip extends CondimentDecorator {

	Beverage beverage;
	
	public Whip(Beverage beverage) {
		this.beverage = beverage;
	}
	
	/* (non-Javadoc)
	 * @see decorator.CondimentDecorator#getDescription()
	 */
	@Override
	public String getDescription() {
		// TODO Auto-generated method stub
		return beverage.getDescription() + ", Whip";
	}

	/* (non-Javadoc)
	 * @see decorator.Beverage#cost()
	 */
	@Override
	public double cost() {
		// TODO Auto-generated method stub
		return 0.10 + beverage.cost();
	}

}

5. 測试, 创建详细组件(Concrete Component), 再一层一层加入装饰者(Decorator)类, 能够实现动态的组合;

代码:

/**
 * @time 2014年5月23日
 */
package decorator;

/**
 * @author C.L.Wang
 *
 */
public class StarbuzzCoffee {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Beverage beverage = new Espresso();
		System.out.println(beverage.getDescription() + 
				" $" + beverage.cost());
		
		Beverage beverage2 = new DarkRoast();
		beverage2 = new Mocha(beverage2);
		beverage2 = new Mocha(beverage2);
		beverage2 = new Whip(beverage2);
		System.out.println(beverage2.getDescription() + 
				" $" + beverage2.cost());
		
		Beverage beverage3 = new HouseBlend();
		beverage3 = new Soy(beverage3);
		beverage3 = new Mocha(beverage3);
		beverage3 = new Whip(beverage3);
		System.out.println(beverage3.getDescription() + 
				" $" + beverage3.cost());
	}
	

}

6. 输出:

Espresso $1.99
Dark Roast, Mocha, Mocha, Whip $1.49
House Blend Coffee, Soy, Mocha, Whip $1.34

面向对象的原则:

对扩展开发, 对改动关闭.


技术分享




设计模式 - 装饰者模式(Decorator Pattern) 具体解释

标签:输出   关闭   override   设计   decorator   nbsp   抽象   end   代码   

原文地址:http://www.cnblogs.com/cxchanpin/p/6884114.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!