天天看點

【設計模式】橋接模式

使用頻率:★★★☆☆

一、什麼是橋接模式

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

二、補充說明

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

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

三、角色

抽象類

具體實作類

行為接口

具體行為實作類

用戶端

四、例子,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");
    }

}      
【設計模式】橋接模式
【設計模式】橋接模式
package com.pichen.dp.structuralpattern.bridge;



public class EatWithFork implements Eatable{

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

    
}      

抽象人類:

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;
    }


}      
【設計模式】橋接模式
【設計模式】橋接模式
package com.pichen.dp.structuralpattern.bridge;

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

}      
【設計模式】橋接模式
【設計模式】橋接模式
package com.pichen.dp.structuralpattern.bridge;

public class Indian extends Person{

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

}      

用戶端:

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      

@Author      風一樣的碼農

@HomePageUrl http://www.cnblogs.com/chenpi/

@Copyright      轉載請注明出處,謝謝~