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


一、策略設計模式

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

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

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM