JAVA學習筆記--策略設計模式與適配器模式

來源:互聯網
上載者:User

標籤:十分   blog   imp   system   apt   接受   interface   bst   static   

一、策略設計模式

  建立一個能夠根據所傳遞對象的不同而具有不同行為的方法被稱為策略設計模式;這類方法包含所要執行的演算法中固定不變的部分,而“策略”包含變化的部分。策略就是傳遞進去的參數對象,它包含要執行的代碼。

  這種設計模式將演算法分別獨立封裝起來,然後將其當做參數傳遞給方法從而讓方法產生不同的行為,不同的演算法可以進行替換(就像給方法不同的實參)。我們可以知道,在策略設計模式中有三個部分:策略、策略引用、接收策略引用的方法(這是筆者自己根據理解給出的名詞,並沒有查詢有沒有這些術語)。下面通過一個例子簡單介紹一下策略設計模式:

package com.tongye.strategy;/* 基類,這裡可以是普通類,也可以是一個介面 */abstract class Shape{    public abstract void outputShape();}/* 策略 */class Circle extends Shape{    public void outputShape(){        System.out.println("this is a circle");    }}/* 策略 */class Square extends Shape{    public void outputShape(){        System.out.println("this is a square");    }}/* 策略 */ class Triangle extends Shape{            public void outputShape(){        System.out.println("this is a triangle");    }}public class Strategy {    /* Strategy.strategic()方法接收一個Shape類型的引用作為參數     * 這個引用可以是任何類型的Shape,如Circle、Square、Triangle     * 根據所傳遞參數的不同,strategic方法有不同的行為略     *  */    public static void strategic(Shape S){    // 接收策略引用的方法,S是策略引用        S.outputShape();                    // 方法中固有不變的部分    }        public static void main(String[] args){        strategic(new Circle());    // 建立Circle()對象作為strategic()方法的參數,這裡的circle()對象就是一個策略,這裡用到了向上轉型        strategic(new Square());    // 策略Square()對象        strategic(new Triangle());  // 策略Triangle()對象    }}/* * output: * this is a circle * this is a square * this is a triangle * */

  通過上面的一小段代碼,我們知道,strategic方法可以根據所傳遞參數的不同而產生不同的行為,這些行為被封裝在不同的策略中,這就是策略設計模式。通過將不同的行為封裝到不同的策略中,我們可以隨時更換策略從而實現不同的行為,從而使得程式更為靈活,更易於維護。

 

 二、適配器模式

   舉個簡單的例子,當我們給手機充電時,由於手機充電口是5V,而插座提供的是220V交流電,因此我們通常需要使用充電器將220V交流電轉換成可供手機充電用的5V直流電,這個充電器就是一個適配器。

  同樣,在編寫JAVA程式時,我們可能會遇到這樣一種情況:我們需要一個類A來實現介面B,但是類A並沒有實現介面B中的所有方法,而類A是不能被改變的,這時我們可以建立一個類C,它繼承類A並實現介面B,這個類C就是一個適配器。適配器中的代碼將接受你所擁有的介面,併產生你所需要的介面。適配器模式有兩張:類適配器模式和對象適配器模式。下面用兩個例子來簡單示範一下:

1、類適配器模式:

/*****************************/package com.tongye.adapters;public interface TargetInterface {    void method1();    void method2();}/*****************************/package com.tongye.adapters;/* 源類,我們需要這個類實現介面  TargetInterface,但這個類不能被改變  * 源類與介面之間是沒有關係的 * */class BeAdapted{    public void method1(){        System.out.println("method1");    }}/* 這是適配器,它接受了BeAdapted中已有的介面併產生我們需要的介面 * method1()方法繼承自BeAdapted類(即已有的介面),不用再作聲明 * 這裡適配器的作用相當於為源類與介面之間建立了一種關係,類似於 implements *  */class Adapter extends BeAdapted implements TargetInterface{    public void method2(){                // method2()方法無法通過繼承獲得,因而需要自己聲明,這就是適配器為我們產生的介面        System.out.println("method2");    }}public class ClassAdapter {    public static void main(String[] args){        Adapter adapt = new Adapter();        adapt.method1();        adapt.method2();    }}

Adapter與BeAdapted是繼承關係,即為類適配器模式。

 

2、對象適配器模式:

/*****************************/package com.tongye.adapters;public interface TargetInterface {    void method1();    void method2();}/*****************************/package com.tongye.adapters;/* 源類,我們需要這個類實現介面  TargetInterface,但這個類不能被改變  * 源類與介面之間是沒有關係的 * */class BeAdapted{    public void method1(){        System.out.println("method1");    }}/* 這是適配器,它接受了中已有的介面併產生我們需要的介面  */class Adapter implements TargetInterface{    private static BeAdapted adapted;            // 聲明一個BeAdapted對象引用    public Adapter(BeAdapted adapted){        // 構造方法,接收一個BeAdapted引用作為參數        this.adapted = adapted;    }        public void method1(){                // method1()在源類BeAdapted中有,這裡直接委派        this.adapted.method1();    }        public void method2(){                // method2()方法無法通過繼承獲得,需要自己聲明        System.out.println("method2");    }}

Adapter與BeAdapted是委託關係,即為對象適配器模式。

通過使用適配器模式,可以大大提升程式的靈活性以及代碼的可重複性。

 

註:筆者在第一次在書上看到策略設計模式和適配器模式時,並沒有看懂,感覺雲裡霧裡的,於是去網上找了一下別人的經驗貼,其中有一位博主(java_my_life)寫的部落格十分簡明易懂,講的也很全面,我就是看了他的部落格才感覺恍然大悟的,這裡貼一下那位博主的兩篇部落格的地址:

http://www.cnblogs.com/java-my-life/archive/2012/05/10/2491891.html

http://www.cnblogs.com/java-my-life/archive/2012/04/13/2442795.html

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.