一. 裝飾者模式
裝飾者模式,又叫包裝器,動態地給動象添加一些額外的職責,若要擴展功能,裝飾者指供了比繼承更有彈性的替代方案.
三. 本節內容以一個點餐配菜的案例來說明裝飾者模式的用法,完整代碼如下:
1. 主食類超類:
package structuralPattern.decorator.decorator2;
/**
* @ClassName: StapleFood
* @Description:主食類[抽象組件]
* @author Liyongbin
* @date 2011-2-7 下午09:43:39
* @version V1.0
*/
public abstract class StapleFood {
private String description ="Unknown StapleFood";
/**
* 主食的描述[主食名稱]
* @return
*/
public String getDescription(){
return description;
}
/**
* 主食的價格
* @return
*/
public abstract double cost();
}
2. 菜類的超類
package structuralPattern.decorator.decorator2;
/**
* @ClassName: Dish
* @Description: 菜類
* @author Liyongbin
* @date 2011-2-7 下午09:53:11
* @version V1.0
*/
public abstract class Dish extends StapleFood {
//繼承了主食類的描述[菜的名字]
public abstract String getDescription() ;
}
3. 主食類的派生類_米飯類
package structuralPattern.decorator.decorator2;
/**
* @ClassName: Rice
* @Description: 米飯_主食_具體組件
* @author Liyongbin
* @date 2011-2-7 下午09:56:26
* @version V1.0
*/
public class Rice extends StapleFood {
@Override
public String getDescription() {
return "泰國香米";
}
@Override
public double cost() {
return 5.0;
}
}
4. 主食類的派生類_面食類
package structuralPattern.decorator.decorator2;
/**
* @ClassName: Rice
* @Description: 面飯_主食_具體組件
* @author Liyongbin
* @date 2011-2-7 下午09:56:26
* @version V1.0
*/
public class Noodle extends StapleFood {
@Override
public String getDescription() {
return "面飯";
}
@Override
public double cost() {
return 4.0;
}
}
5. 菜類的派生類: 雞肉
package structuralPattern.decorator.decorator2;
public class Chicken extends Dish {
//
private StapleFood stapleFood;
//
public Chicken(StapleFood stapleFood) {
this.stapleFood = stapleFood;
}
主食的名稱+雞肉的名稱
@Override
public String getDescription() {
return stapleFood.getDescription() + ",辣子雞";
}
//主食的價格+雞肉的價格
@Override
public double cost() {
return stapleFood.cost()+12.0d ;
}
}
6. 菜類的派生類: 魚肉
package structuralPattern.decorator.decorator2;
public class Fish extends Dish {
//
StapleFood stapleFood;
//
public Fish(StapleFood stapleFood) {
this.stapleFood = stapleFood;
}
主食的名稱+魚肉的名稱
@Override
public String getDescription() {
return stapleFood.getDescription() + ",烏江魚";
}
//主食的價格+魚肉的價格
@Override
public double cost() {
return stapleFood.cost()+10.0d ;
}
}
7. 菜類的派生類: 鴨肉
package structuralPattern.decorator.decorator2;
public class PekingDuck extends Dish {
//
StapleFood stapleFood;
//
public PekingDuck(StapleFood stapleFood) {
this.stapleFood = stapleFood;
}
主食的名稱+鴨肉的名稱
@Override
public String getDescription() {
return stapleFood.getDescription() + ",北京烤鴨";
}
//主食的價格+鴨肉的價格
@Override
public double cost() {
return stapleFood.cost()+25.0d ;
}
}
8. 菜類的派生類: 羊肉
package structuralPattern.decorator.decorator2;
public class Mutton extends Dish {
//
StapleFood stapleFood;
//
public Mutton(StapleFood stapleFood) {
this.stapleFood = stapleFood;
}
//主食的名稱+羊肉的名稱
@Override
public String getDescription() {
return stapleFood.getDescription() + ",小肥羊";
}
//主食的價格+羊肉的價格
@Override
public double cost() {
return stapleFood.cost()+30.0d ;
}
}
9. 點餐測試類
package structuralPattern.decorator.decorator2;
/**
* @ClassName: DecoratorTest
* @Description:點餐測試
* @author Liyongbin
* @date 2011-2-7 下午09:12:46
* @version V1.0
*/
public class DecoratorTest {
public static void main(String[] args) {
//無菜面飯[¥4]
StapleFood noodle0 = new Noodle();
System.out.println(noodle0.getDescription()+" ¥"+noodle0.cost());
//帶菜(雞肉[¥12],魚肉[¥10])面飯[¥4]
StapleFood noodle1 = new Noodle();
noodle1=new Chicken(noodle1);
noodle1=new Fish(noodle1);
System.out.println(noodle1.getDescription()+" ¥"+noodle1.cost());
//無菜的米飯[¥5]
StapleFood rice0 = new Rice();
System.out.println(rice0.getDescription()+" ¥"+rice0.cost());
//帶菜(雞肉[¥12],魚肉[¥10],羊肉[¥30],鴨肉[¥25])的米飯[¥5]
StapleFood rice1 = new Rice();
rice1=new Chicken(rice1);
rice1=new Fish(rice1);
rice1=new Mutton(rice1);
rice1=new PekingDuck(rice1);
System.out.println(rice1.getDescription()+" ¥"+rice1.cost());
}
}
10. 運行結果如下: