디자인 모드 의 장식 자 모드: 스타 벅 스 커피

package decorate;

/*headfirst               ,               ,           ,    ,      ,    
 *          (                     ,                       )
 *               ,        ,          ,         
 */
abstract class Beverage	//             ,       
{
	String description = "Unknown Beverage";	//    ,       
	public String getDescription()	//         
	{
		return description;
	}
	public abstract double cost();	//    ,      
}

abstract class CondimentDecorator extends Beverage	//              ,         
{
	public abstract String getDescription();	//                ,       ,        
}

class Espresso extends Beverage	//         ,   
{
	public Espresso()
	{
		description = "espresso";	//        
	}
	public double cost()
	{
		return 1.99;	//       
	}
}

class HouseBlend extends Beverage	//         ,    
{
	public HouseBlend()	
	{
		description = "houseblend";	//         
	}
	public double cost()
	{
		return .89;	//         
	}
}

class Mocha extends CondimentDecorator	//         ,  
{
	Beverage beverage;	//    Beverage            ,                       
	public Mocha(Beverage bever)
	{
		this.beverage = bever;	//              
	}
	public String getDescription()
	{
		return beverage.getDescription() + ",mocha"; //           mocha     
	}
	public double cost()
	{
		return beverage.cost() + .20;	//               
	}
}

class Whip extends CondimentDecorator	//         ,  ,       mocha    
{
	Beverage beverage;	
	public Whip(Beverage bever)
	{
		this.beverage = bever;
	}
	public String getDescription()
	{
		return beverage.getDescription() + ",whip";
	}
	public double cost()
	{
		return beverage.cost() + .41;
	}
}

public class decorate {

	public static void main(String[] args) 
	{
		Beverage espresso1 = new Espresso();	//     espresso  
		System.out.println(espresso1.getDescription() + ":" + espresso1.cost()); //    espresso          
		espresso1 = new Mocha(espresso1);	// Mocha    espresso,                  ,             ,                ,           
		System.out.println(espresso1.getDescription() + ":" + espresso1.cost());	//  espresso mocha         
		espresso1 = new Whip(espresso1);	//   whip   mocha    espresso
		System.out.println(espresso1.getDescription() + ":" + espresso1.cost());	//                
	}

}

좋은 웹페이지 즐겨찾기