文章出處

使用頻率:★★★☆☆

一、什么是橋接模式

將對象的行為抽象為接口,作為抽象類的成員屬性在抽象層進行組合(個人理解,僅供參考);

二、補充說明

改變對象與其行為的強耦合關系,使之與行為解耦;

使對象的行為以及對象本身都能獨立變化;

三、角色

抽象類

具體實現類

行為接口

具體行為實現類

客戶端

四、例子,JAVA實現

例子說明:

對象:人(有中國人、印度人。。。。)

行為:吃(用筷子吃、用手吃、用叉子吃。。。)

 

假設一個場景:有一個中國人和歐洲人,中國人用筷子吃,歐洲人用叉子吃,如果我們要對這兩個人進行封裝,一般用以下方式:

抽象出一個抽象人類,新建兩個具體類繼承抽象人類,其中一個實現筷子吃行為,另一個實現用叉子吃行為;

缺點:具體的人對象與吃的行為變成了強耦合關系,修改和擴展都不方便;說不定哪天中國人去吃西餐,用叉子吃,這時候就需要改變具體中國人實現類代碼;

 

假設使用橋接模式,我們可以這樣設計:

對吃行為抽象成一個接口,并定義好各自的實現類(用筷子吃實現類,用手吃實現類,用叉子吃實現類);

還是抽象出一個抽象人類,不過要在這個抽象人類中組合進一個吃行為接口;

具體的中國人或歐洲人繼承抽象人類,但其不需要實現具體吃行為,只需要對其成員屬性(吃行為接口)賦一個具體實現對象即可;

代碼實現如下:

吃行為接口:

package com.pichen.dp.structuralpattern.bridge;

/**
 * 吃行為接口
 * @author    pi chen
 * @version   V1.0.0, 2016年2月19日
 * @see       
 * @since     V1.0.0
 */
public interface Eatable {

    public void eat();
    
}

吃行為實現類(用筷子吃實現類,用手吃實現類,用叉子吃實現類):

package com.pichen.dp.structuralpattern.bridge;

public class EatWithChopsticks implements Eatable{

    @Override
    public void eat() {
        System.out.println("Eat with Chopsticks");
    }

}
View Code
package com.pichen.dp.structuralpattern.bridge;


public class EatWithHand implements Eatable{

    @Override
    public void eat() {
        System.out.println("Eat with Hand");
    }

}
View Code
package com.pichen.dp.structuralpattern.bridge;



public class EatWithFork implements Eatable{

    @Override
    public void eat() {
        System.out.println("Eat with Fork");
    }

    
}
View Code

抽象人類:

package com.pichen.dp.structuralpattern.bridge;


public abstract class Person {

    //吃行為抽象為接口,使之與具體對象解耦
    protected Eatable eatable;

    /**
     * @return the eatable
     */
    public Eatable getEatable() {
        return eatable;
    }

    /**
     * @param eatable the eatable to set
     */
    public void setEatable(Eatable eatable) {
        this.eatable = eatable;
    }
    

}

具體的不同種類的人(中國人、歐美人、印度人):

package com.pichen.dp.structuralpattern.bridge;

public class Chinese extends Person{


    public Chinese(Eatable eatable) {
        this.eatable = eatable;
    }


}
View Code
package com.pichen.dp.structuralpattern.bridge;

public class European extends Person{
    public European(Eatable eatable) {
        this.eatable = eatable;
    }

}
View Code
package com.pichen.dp.structuralpattern.bridge;

public class Indian extends Person{

    public Indian(Eatable eatable) {
        this.eatable = eatable;
    }
    

}
View Code

客戶端:

package com.pichen.dp.structuralpattern.bridge;

public class Main {

    public static void main(String[] args) {
        //定義三種具體行為
        Eatable eatWithChopsticks = new EatWithChopsticks();
        Eatable eatWithFork = new EatWithFork();
        Eatable eatWithHand = new EatWithHand();
        
        //定義三中具體人
        Person chinese = new Chinese(eatWithChopsticks);
        Person indian = new Indian(eatWithHand);
        Person european = new European(eatWithFork);
        
        //中國人用筷子吃,印度人用手,歐洲人用叉子
        chinese.getEatable().eat();
        indian.getEatable().eat();
        european.getEatable().eat();
        
        //當然,中國人有時候也用叉子吃西餐,或用手吃
        chinese.setEatable(eatWithFork);
        chinese.getEatable().eat();
        chinese.setEatable(eatWithHand);
        chinese.getEatable().eat();
        
        
    }
}

結果打印:

Eat with Chopsticks
Eat with Hand
Eat with Fork
Eat with Fork
Eat with Hand

 


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()