Java裝飾者模式

來源:互聯網
上載者:User

標籤:

Java裝飾者模式簡介

 一、假設有一個Worker介面,它有一個doSomething方法,Plumber和Carpenter都實現了Worker介面,代碼及關係如下:

 

1.Worker.javapackage decorator;public interface Worker {    public void doSomething();}

 

2.Plumber.javapublic class Plumber implements Worker {    @Override    public void doSomething() {        System.out.println("修理水管");    }}

 

3.Carpenterpublic class Carpenter implements Worker{    public void doSomething(){        System.out.println("修門窗");    }}
4.測試代碼 Test.javapublic class Test {    public static void main(String[] args) {        Worker worker = null;        Carpenter carpenter = new Carpenter();        Plumber plumber = new Plumber();        worker = carpenter;        worker.doSomething();        worker = plumber;        worker.doSomething();    }}

測試結果如下:

 

二、現有A、B兩家公司,都有木工(Carpenter)和水管工(Plumber),但兩公司的對兩個工種的要求都不同,A公司要求員工在doSomething前自己介紹“我是A公司的”;B公司要求員工在doSomething前自己介紹“我是B公司的”;一個直接但不科學的設計方法是為每個公司都設計各自的木工(Carpenter)和水管工(Plumber),若公司增加到100個,則類要增加到200個,若再增加其他工種,則類會更多,關係如下:

 

三、此情況適合採用裝飾者模式,為每個公司設計一個裝飾類xWorker,xWorker也實現Worker介面,且有一個參數類型為Worker的建構函式和一個類型為Worker的成員變數,這樣xWorker就可以取得相應類型worker的引用,在xWorker的doSomething()方法裡處理各自公司的特殊要求:

 

 

 1 增加類 AWorker.java 2  3 public class AWorker implements Worker { 4  5     private Worker worker; 6      7     //通過些建構函式,AWorker就可以得到具體工種的引用, 8     //為下面的doSomething()方法中加入A公司的特殊要求做準備 9     public AWorker(Worker worker){10         this.worker = worker;11     }12     13     @Override14     public void doSomething() {15         System.out.println("您好!我是A公司的員工");16         worker.doSomething();17     }18 19 }

 

 1 //增加類 BWorker.java 2  3 public class BWorker implements Worker { 4  5     private Worker worker; 6      7     public BWorker(Worker worker){ 8         this.worker = worker; 9     }10     11     @Override12     public void doSomething() {13         System.out.println("您好!我是B公司的員工。");14         worker.doSomething();15     }16 17 }

 

 1 //測試代碼 2      3 public static void main(String[] args) { 4          5         //A公司 6         Carpenter carpenterA = new Carpenter(); 7         Plumber plumberA = new Plumber(); 8         AWorker aWorker1 = new AWorker(carpenterA); 9         AWorker aWorker2 = new AWorker(plumberA);10         11         aWorker1.doSomething();12         aWorker2.doSomething();13     14         System.out.println("==================================");15         16         //B公司17         Carpenter carpenterB = new Carpenter();18         Plumber plumberB = new Plumber();19         BWorker bWorker1 = new BWorker(carpenterB);20         BWorker bWorker2 = new BWorker(plumberB);21         22         bWorker1.doSomething();23         bWorker2.doSomething();24     }25 }

測試結果:

Java裝飾者模式

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.