设计模式讲解4:Bridge模式源码
声明:迁移自本人CSDN博客https://blog.csdn.net/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
完。
设计模式讲解4:Bridge模式源码的更多相关文章
- 并发编程学习笔记(9)----AQS的共享模式源码分析及CountDownLatch使用及原理
1. AQS共享模式 前面已经说过了AQS的原理及独享模式的源码分析,今天就来学习共享模式下的AQS的几个接口的源码. 首先还是从顶级接口acquireShared()方法入手: public fin ...
- 设计模式课程 设计模式精讲 7-3 建造者模式源码解析(jdk+guava+spring+mybaties)
1 源码解析 1.1 jdk解析 1.2 guava解析 1.3 spring解析 1.4 mybaties解析 1 源码解析 1.1 jdk解析 String public StringBuilde ...
- 1个月连载30个设计模式真实案例(附源码),挑战年薪60W不是梦
本文所有内容均节选自<设计模式就该这样学> 本文自2012年10月29日起持续连载,请大家持续关注.... 序言 Design Patterns: Elements of Reusable ...
- 设计模式-简单工厂Coding+jdk源码解析
感谢慕课geely老师的设计模式课程,本套设计模式的所有内容均以课程为参考. 前面的软件设计七大原则,目前只有理论这块,因为最近参与项目重构,暂时没有时间把Coding的代码按照设计思路一点点写出来. ...
- 【一起学源码-微服务】Nexflix Eureka 源码十二:EurekaServer集群模式源码分析
前言 前情回顾 上一讲看了Eureka 注册中心的自我保护机制,以及里面提到的bug问题. 哈哈 转眼间都2020年了,这个系列的文章从12.17 一直写到现在,也是不容易哈,每天持续不断学习,输出博 ...
- ACE - Reactor模式源码剖析及具体实现(大量源码慎入)
原文出自http://www.cnblogs.com/binchen-china,禁止转载. 在之前的文章中提到过Reactor模式和Preactor模式,现在利用ACE的Reactor来实现一个基于 ...
- Android Doze模式源码分析
科技的仿生学无处不在,给予我们启发.为了延长电池是使用寿命,google从蛇的冬眠中得到体会,那就是在某种情况下也让手机进入类冬眠的情况,从而引入了今天的主题,Doze模式,Doze中文是打盹儿,打盹 ...
- Flink源码阅读(一)——Flink on Yarn的Per-job模式源码简析
一.前言 个人感觉学习Flink其实最不应该错过的博文是Flink社区的博文系列,里面的文章是不会让人失望的.强烈安利:https://ververica.cn/developers-resource ...
- 硬核讲解 Jetpack 之 LifeCycle 源码篇
前一篇 硬核讲解 Jetpack 之 LifeCycle 使用篇 主要介绍了 LifeCycle 存在的意义,基本和进阶的使用方法.今天话不多说,直接开始撸源码. 本文基于我手里的 android_9 ...
随机推荐
- Python面试常问的10个问题
很多人想找Python工作,面试往往在基础知识点采坑了 Python是个非常受欢迎的编程语言,随着近些年机器学习.云计算等技术的发展,Python的职位需求越来越高.下面我收集了10个Python面试 ...
- Unity 打开其他exe文件
using UnityEngine; using System.Collections; using System.Diagnostics;///// public class FeiYuZhu : ...
- 吴裕雄--天生自然java开发常用类库学习笔记:线程常用的操作方法
class MyThread implements Runnable{ // 实现Runnable接口 public void run(){ // 覆写run()方法 for(int i=0;i< ...
- ACM-生化武器
Description在一个封闭的房间里,gogo给大家表演了他的屁遁术,人果然一下没影了,但是他留下的“生化武器”,却以每秒1米的速度向上下左右扩散出去.为了知道自己会不会被“毒”到,你果断写了个算 ...
- 51nod 1206:Picture 求覆盖周长
1206 Picture 题目来源: IOI 1998 基准时间限制:2 秒 空间限制:131072 KB 分值: 160 难度:6级算法题 收藏 关注 给出平面上的N个矩形(矩形的边平行于X轴和 ...
- HTML的文档结构与语法(一)
一.走进Web开发 Web运行的原理: 二.HTML 1.1什么是html HTML是用来描述网页的一种语言 HTML指的是超文本标记语言(Hyper Text Markup Language) 超文 ...
- MongoDB 删除,添加副本集,并修改副本集IP等信息
MongoDB 删除,添加副本集,并修改副本集IP等信息 添加副本,在登录到主节点下输入 rs.add("ip:port"); 删除副本 rs.remove("ip:po ...
- (1)关于PSP寄存器和MSP寄存器的简单描述
由于 Cortex-M3 和 M4 内核具有双堆栈指针, MSP 主堆栈指针和 PSP 进程堆栈指针,或者叫 PSP任务堆栈指针也是可以的.在 FreeRTOS 操作系统中,主堆栈指针 MSP 是给系 ...
- java核心-多线程-零碎知识收集
1.不能使用Integer作为并发锁 原因:synchronized(Integer)时,当值发生改变时,基本上每次锁住的都是不同的对象实例,想要保证线程安全,推荐使用AtomicInteger之类会 ...
- HDU_4939 stupid tower defense 2014多校7 多变量型DP
意思是有个塔防游戏,有三种塔,红塔在怪物经过的时候每秒会产生攻击力大小的伤害,绿塔对怪物经过以及经过之后每秒产生攻击力大小的伤害,还有种蓝塔,对怪物进行减速,即怪物从此之后经过一个单位都会减慢c秒 最 ...