设计模式讲解4:Bridge模式源码

本文通过实例解析了桥接模式在字体渲染领域的应用,展示了如何将字体类型与操作系统平台解耦,以实现更灵活和可扩展的字体渲染服务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

声明:迁移自本人优快云博客https://blog.youkuaiyun.com/u013365635

桥接模式可以和排列组合关联起来理解,一个对象有多种不通种类的属性,如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

完。

转载于:https://www.cnblogs.com/xsl-thumb-rfcs/p/9941591.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值