1. 程式人生 > >設計模式之裝飾者模式

設計模式之裝飾者模式

一點 ron 測試類 實現類 stub generated class void nbsp

一.裝飾者模式特點:
1.裝飾者和被裝飾者對象有相同的超類
2.可以用一個或多個裝飾者包裝一個對象
3.由於裝飾者和被裝飾者具有相同超類,所以任何需要被包裝對象的場合,可以用裝飾過的對象代替
4.裝飾者可以再所委托被裝飾者的行為之前或之後,加上自己的行為,以達到特定的目的(重點)
5.對象可以在任何時候被裝飾,所以可以在運行時動態的不限量的用你喜歡的裝飾者來裝飾對象
二.裝飾者模式定義:

動態地將責任附加到對象上,若要擴展功能,裝飾者提供了比繼承更具有彈性的替代方案

三.代碼部分

1.咖啡超類

 1 package com.pattern.decorate;
 2 /*
 3  * 咖啡超類
 4  */
5 public abstract class Coffee { 6 7 String des;//對咖啡的描述 8 9 public String getDes() { 10 return des; 11 } 12 13 public void setDes(String des) { 14 this.des = des; 15 } 16 17 public abstract double cost();//不同的咖啡花費不同 18 19 }

2.咖啡實體類一意大利咖啡,被修飾者

 1 package com.pattern.decorate;
 2 /**
 3  * 意大利咖啡,被裝飾者
 4  * @author Administrator
 5  *
 6  */
 7 public class ItalianCoffee extends Coffee {
 8 
 9     public ItalianCoffee() {
10         des = "意大利咖啡";
11     }
12     
13     @Override
14     public double cost() {
15         return 10.99;
16 
17
} 18 19 }

3.咖啡實體類2被裝飾者

 1 package com.pattern.decorate;
 2 /**
 3  * 美式咖啡,被裝飾者
 4  * @author Administrator
 5  *
 6  */
 7 public class CafeAmericano extends Coffee {
 8 
 9     public CafeAmericano() {
10         des = "美式咖啡";
11     }
12     
13     @Override
14     public double cost() {
15         // TODO Auto-generated method stub
16         return 8.10;
17     }
18 
19 }

4.裝飾者超類,調料

 1 package com.pattern.decorate;
 2 /**
 3  * 裝飾者的超類,咖啡調料,也是咖啡一部分,所以繼承Coffee
 4  * @author Administrator
 5  *
 6  */
 7 public abstract class CoffeeCondiment extends Coffee {
 8 
 9     
10     
11     @Override
12     public abstract String getDes();//要知道調料加到什麽咖啡中
13     @Override
14     public abstract double cost();//不同的調料價格不一樣
15 }

5.裝飾者實現類1巧克力

 1 package com.pattern.decorate;
 2 /**
 3  * 裝飾者實現類,用來修飾Coffee對象
 4  * @author Administrator
 5  *
 6  */
 7 public class Chocolate extends CoffeeCondiment {
 8 
 9     Coffee coffee;//肯定會有這個引用表示調料要加到咖啡中
10 
11     public Chocolate(Coffee coffee) {
12         this.coffee = coffee;
13         this.des = "巧克力";
14     }
15 
16     @Override
17     public String getDes() {
18         // TODO Auto-generated method stub
19         return this.des+coffee.getDes();
20     }
21 
22     @Override
23     public double cost() {
24         // TODO Auto-generated method stub
25         return coffee.cost()+5.1;//加一種調料價錢加一點
26     }
27 
28 }

6.裝飾者實現類2冰塊

 1 package com.pattern.decorate;
 2 /**
 3  * 裝飾者實現類,用來修飾Coffee對象,
 4  * @author Administrator
 5  *
 6  */
 7 public class Ice extends CoffeeCondiment {
 8 
 9     Coffee coffee;//肯定會有這個引用表示調料要加到咖啡中
10 
11     public Ice(Coffee coffee) {
12         this.coffee = coffee;
13         this.des = "冰塊";
14     }
15 
16     @Override
17     public String getDes() {
18         // TODO Auto-generated method stub
19         return this.des+coffee.getDes();
20     }
21 
22     @Override
23     public double cost() {
24         // TODO Auto-generated method stub
25         return coffee.cost()+1.5;//加一種調料價錢加一點
26     }
27 
28 }

7.裝飾者實現類3摩卡

 1 package com.pattern.decorate;
 2 /**
 3  * 裝飾者實現類,用來修飾Coffee對象,
 4  * @author Administrator
 5  *
 6  */
 7 public class Mocha extends CoffeeCondiment {
 8 
 9     Coffee coffee;//肯定會有這個引用表示調料要加到咖啡中
10 
11     public Mocha(Coffee coffee) {
12         this.coffee = coffee;
13         this.des = "摩卡";
14     }
15 
16     @Override
17     public String getDes() {
18         // TODO Auto-generated method stub
19         return this.des+coffee.getDes();
20     }
21 
22     @Override
23     public double cost() {
24         // TODO Auto-generated method stub
25         return coffee.cost()+8.2;//加一種調料價錢加一點
26     }
27 
28 }

8.測試類

 1 package com.pattern.decorate;
 2 /**
 3  * 裝飾者模式測試代碼
 4  * @author Administrator
 5  *
 6  */
 7 public class Client {
 8 
 9     public static void main(String[] args) {
10         //創建原始對象,意大利咖啡
11         Coffee coffee1 = new ItalianCoffee();
12         System.out.println("給我來一杯"+coffee1.getDes()+",價格:"+coffee1.cost());
13         System.out.println("---------------------------------------------");
14         Coffee coffee2 = new CafeAmericano();
15         System.out.println("給我來一杯"+coffee2.getDes()+",價格:"+coffee2.cost());
16         System.out.println("---------------------------------------------");
17         //創建裝飾者
18         Coffee co = new  Chocolate(coffee1);//加入巧克力
19         co = new Ice(co);//在加入冰塊
20         co = new Mocha(co);//再加入摩卡
21         System.out.println("給我來一杯"+co.getDes()+",價格:"+co.cost());
22         System.out.println("------------------------------------------------");
23         Coffee cof = new Ice(coffee2);
24         cof = new Mocha(cof);
25         System.out.println("給我來一杯"+cof.getDes()+",價格:"+cof.cost());
26         
27         
28         
29     }
30 
31 }

9.運行結果

技術分享

設計模式之裝飾者模式