設計模式——橋接模式

橋接模式(Bridge Pattern):將抽象部分與其實現(xiàn)部分分離,使它們可以獨立地變化。它是一種對象結(jié)構(gòu)型設計模式。在軟件系統(tǒng)中,如果某一部分功能存在2個變化的緯度,而這兩個緯度可以相互分離,互不影響。因此便可以使用橋接模式,將兩個變化的緯度進行抽象設計,兩個抽象部分相關(guān)聯(lián),具體實現(xiàn)部分互相無任何關(guān)系。

橋接模式.jpg

1)Client(客戶端):外部使用程序
2)Abstraction(抽象類):定義抽象類的接口,一般將最基礎(chǔ)業(yè)務方法的一個緯度定義為抽象類,并在類中定義抽象方法;
3)Implementor(實現(xiàn)類接口):定義實現(xiàn)類的接口,與Abstraction不同的另外一個緯度的基類或者是接口;
4)RefinderAbstracion(擴充抽象類):抽象類的實現(xiàn)者,根據(jù)不同變化緯度對應不同類;
5)ConcreteImplementor(具體實現(xiàn)類):實現(xiàn)抽象類接口,完成不同實現(xiàn);

示例:不同操作系統(tǒng),顯示不同格式的圖片, 維度一:操作平臺(Windows Linux Mobile),維度二:圖片格式(JPG, PDF, PNG)

操作系統(tǒng)類 相當于Abstraction

/**
 * 
 * @author apple
 * 
 * 操作系統(tǒng)
 *
 */
public abstract class OperatingSystem {
    // 圖片格式類
    private IPictureFormat pictureFormat;
    
    public OperatingSystem(IPictureFormat pictureFormat) {
        super();
        this.pictureFormat = pictureFormat;
    }
    
    /**
     * 顯示操作系統(tǒng)
     */
    protected void operatingSystemtype() {
        this.pictureFormat.imageFormat();
    }
    
}

windows、linux、mobile 相當于RefinderAbstracion(擴充抽象類)

/**
 * 
 * @author apple
 * 
 * Windows
 *
 */
public class Windows extends OperatingSystem {

    public Windows(IPictureFormat pictureFormat) {
        super(pictureFormat);
        // TODO Auto-generated constructor stub
    }
    
    @Override
    protected void operatingSystemtype() {
        // TODO Auto-generated method stub
        super.operatingSystemtype();
        System.out.println(" 圖片顯示在 Windows");
    }

}

/**
 * 
 * @author apple
 * 
 * Linux
 *
 */
public class Linux extends OperatingSystem {

    public Linux(IPictureFormat pictureFormat) {
        super(pictureFormat);
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void operatingSystemtype() {
        // TODO Auto-generated method stub
        super.operatingSystemtype();
        System.out.println(" 圖片顯示在 Linux");
    }
}

/**
 * 
 * @author apple
 * 
 * Mobile
 *
 */
public class Mobile extends OperatingSystem {

    public Mobile(IPictureFormat pictureFormat) {
        super(pictureFormat);
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void operatingSystemtype() {
        // TODO Auto-generated method stub
        super.operatingSystemtype();
        System.out.println(" 圖片顯示在 Mobile");
    }
}

IPictureFormat 相當于 Implementor(實現(xiàn)類接口)

/**
 * 
 * @author apple
 * 
 * 圖片格式
 *
 */
public interface IPictureFormat {
    /**
     * 顯示圖片格式
     */
    void imageFormat();
}

PNG、JPG、PNG 相當于ConcreteImplementor

/**
 * 
 * @author apple
 * 
 * PNG
 *
 */
public class PNG implements IPictureFormat {

    @Override
    public void imageFormat() {
        // TODO Auto-generated method stub
        System.out.print("圖片格式:PNG");
    }

}

/**
 * 
 * @author apple
 * 
 * PDF
 * 
 *
 */
public class PDF implements IPictureFormat {

    @Override
    public void imageFormat() {
        // TODO Auto-generated method stub
        System.out.print("圖片格式:PDF");
    }

}

/**
 * 
 * @author apple
 * 
 * JPG
 * 
 *
 */
public class JPG implements IPictureFormat {

    @Override
    public void imageFormat() {
        // TODO Auto-generated method stub
        System.out.print("圖片格式:JPG");
    }

}

public class Client {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //  不同操作系統(tǒng),顯示不同格式的圖片, 維度一:操作平臺(Windows Linux Mobile),維度二:圖片格式(JPG, PDF, PNG)
        
        //  Mobile : PNG
        Mobile mobile = new Mobile(new PNG());
        mobile.operatingSystemtype();
        //  Linux : PDF
        Linux linux = new Linux(new PDF());
        linux.operatingSystemtype();
        //  Windows : JPG
        Windows windows = new Windows(new JPG());
        windows.operatingSystemtype();
        
    }

}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

  • 1.初識橋接模式 將抽象部分與它的實現(xiàn)部分分離,使它們都可以獨立地變化。 Abstraction:抽象部分的接口。...
    王偵閱讀 1,011評論 0 7
  • 場景 某公司準備開發(fā)一個跨平臺圖像瀏覽系統(tǒng),要求可以顯示JPG、PNG等多種格式圖片,并且能夠在Windows、L...
    皆為序幕_閱讀 425評論 0 0
  • 定義 橋接模式的用意是將抽象化(Abstraction)與實現(xiàn)化(Implementation)脫耦,使得二者可以...
    易兒善閱讀 774評論 1 3
  • Ariel的世界閱讀 155評論 0 0
  • 歲月,悄悄地從我指尖滑過,如水銀一般,靜靜地流淌,不被輕易改變。當我試著想要捉住它,它卻偷偷溜走了。終于發(fā)現(xiàn)...

友情鏈接更多精彩內(nèi)容