天天看點

設計模式講解4:Bridge模式源碼

通告:個人部落格遷往 https://www.cnblogs.com/xsl-thumb-rfcs

橋接模式可以和排列組合關聯起來了解,一個對象有多種不通種類的屬性,如attributeA1,attributeA2,attributeB1,attributeB2,attributeB3。可以組裝出23=6種對象。作為服務的提供方,應該怎樣提供這種服務給用戶端呢?是代碼中寫死23中組合還是交給用戶端一定的靈活性,使用戶端可以靈活組裝出自己想要的對象呢?無論是代碼的可擴充性還是從代碼的簡潔性來講,我們都會選擇後者。(注:這是作者第一次在設計模式講解中引入了服務端、用戶端的說法,事實上,設計模式的完成一半都要涉及服務端、用戶端的說法,如果隻存在服務方而用戶端的配置,大部分的設計模式是沒有意義的。)

以顯示字型為例,不同的字型在同樣的作業系統上會顯示不同的樣式。同樣的字型在不通的作業系統上也可能顯示不同的樣式。那這就是很典型的可以使用Bridge模式的場景。

字型抽象類

package com.designpattern.bridge;

/**
 * 字型抽象類
 */
public abstract class Font {
    protected FontImplOSPlatform fontImplOSPlatform;

    public abstract void DrawText(String text);

    protected FontImplOSPlatform getFontOSPlatform(String type) {
        if(type.equals("Windows")) {
            return new FontImplOnWindows();
        } else if(type.equals("Linux")) {
            return new FontImplOnLinux();
        } else {
            return new FontImplOnOtherOS();
        }
    }
}
           

宋體實作類

package com.designpattern.bridge;

/**
 * 宋體
 */
public class SongTypeFaceFont extends Font {
    public SongTypeFaceFont(String osType) {
        fontImplOSPlatform = getFontOSPlatform(osType);
    }

    public void DrawText(String text) {
        System.out.println("will render \"" + text + "\"");
        System.out.println("The text is Song style text!");
        fontImplOSPlatform.DrawTextOnOS();
    }
}
           

楷書實作類

package com.designpattern.bridge;

/**
 * 楷書
 */
public class RegularStyleFont extends Font {
    public RegularStyleFont(String osType) {
        fontImplOSPlatform = getFontOSPlatform(osType);
    }

    public void DrawText(String text) {
        System.out.println("will render \"" + text + "\"");
        System.out.println("The text is regular style text!");
        fontImplOSPlatform.DrawTextOnOS();
    }
}
           

作業系統字型實作接口類

package com.designpattern.bridge;

public interface FontImplOSPlatform {
    void DrawTextOnOS();
}
           

Windows實作字型渲染

package com.designpattern.bridge;

/**
 * 字型在Windows上的實作
 */
public class FontImplOnWindows implements FontImplOSPlatform {
    public void DrawTextOnOS() {
        System.out.println("The text will be rendered on Windows");
    }
}
           

Linux實作字型渲染

package com.designpattern.bridge;

/**
 * 字型在Linux上的實作
 */
public class FontImplOnLinux implements FontImplOSPlatform {
    public void DrawTextOnOS() {
        System.out.println("The text will be rendered on Linux");
    }
}
           

其他作業系統實作字型渲染

package com.designpattern.bridge;

/**
 * 字型在其他作業系統上的實作
 */
public class FontImplOnOtherOS implements FontImplOSPlatform {
    public void DrawTextOnOS() {
        System.out.println("The text will be rendered on Other OS");
    }
}
           

測試類(用戶端類)

package com.designpattern.bridge;

public class TestBridge {
    public static void main(String[] args) {
        Font myText = new SongTypeFaceFont("Windows");
        myText.DrawText("中國");
        System.out.println();

        myText =  new SongTypeFaceFont("Linux");
        myText.DrawText("中國");
        System.out.println();

        myText =  new RegularStyleFont("Windows");
        myText.DrawText("中國");
        System.out.println();

        myText =  new RegularStyleFont("Linux");
        myText.DrawText("中國");
        System.out.println();
    }
}
           

運作結果如下

will render "中國"
The text is Song style text!
The text will be rendered on Windows

will render "中國"
The text is Song style text!
The text will be rendered on Linux

will render "中國"
The text is regular style text!
The text will be rendered on Windows

will render "中國"
The text is regular style text!
The text will be rendered on Linux
           

完。