设计模式之桥接模式(结构型)

举报
yd_273762914 发表于 2020/12/03 00:51:33 2020/12/03
【摘要】 文章目录 模式定义模式角色模式分析模式例子模式应用 模式定义 桥接模式(Bridge Pattern)是将抽象部分和实现部分分离,使它们可以独立地改变,是一种对象结构型模式。 模式角色 桥接模式包含如下角色: Abstraction(抽象类)RefinedAbstraction(扩充抽象类)Implementor(实现类接口)Concre...

模式定义

桥接模式(Bridge Pattern)是将抽象部分和实现部分分离,使它们可以独立地改变,是一种对象结构型模式。

模式角色

桥接模式包含如下角色:

  • Abstraction(抽象类)
  • RefinedAbstraction(扩充抽象类)
  • Implementor(实现类接口)
  • ConcreteImplementor(具体实现类)

模式分析

桥接模式关键在于如何将抽象化与实现化解耦,使得两者可以独立改变。

抽象化:抽象就是忽略一些信息,将不同的实体当作同样的实体对待。在面向对象中将对象的共同性质抽取出来形成类的过程称之为抽象化的过程

实现化:针对抽象话给出的具体实现,就是实现化,抽象化与实现化是互逆的过程

解耦:解耦就是将抽象化和实现化直接的耦合解脱开,或者说将两者之间的强关联变成弱关联,将两个角色由继承改成关联关系(组合或者聚合)

典型代码:

public interface Implementor
{
	public void operationImpl();
} 


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
public abstract class Abstraction
{
	protected Implementor impl; public void setImpl(Implementor impl)
	{
		this.impl=impl;
	} public abstract void operation();
} 


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
public class RefinedAbstraction extends Abstraction
{
	public void operation()
	{
		//代码
		impl.operationImpl();
		//代码
	}
} 


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

模式例子

画出不同颜色的圆,DrawAPI 接口的实体类 RedCircle、GreenCircle。Shape 是一个抽象类,例子来自:http://www.runoob.com/design-pattern/bridge-pattern.html

创建桥接接口:

public interface DrawAPI { public void drawCircle(int radius, int x, int y);
}

  
 
  • 1
  • 2
  • 3

接口具体实现类:

public class RedCircle implements DrawAPI { @Override public void drawCircle(int radius, int x, int y) { System.out.println("Drawing Circle[ color: red, radius: " + radius +", x: " +x+", "+ y +"]"); }
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
public class GreenCircle implements DrawAPI { @Override public void drawCircle(int radius, int x, int y) { System.out.println("Drawing Circle[ color: green, radius: " + radius +", x: " +x+", "+ y +"]"); }
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

抽象类关联方式实现接口:

public abstract class Shape { protected DrawAPI drawAPI; protected Shape(DrawAPI drawAPI){ this.drawAPI = drawAPI; } public abstract void draw();  
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

具体类实现抽象类:

public class Circle extends Shape { private int x, y, radius; public Circle(int x, int y, int radius, DrawAPI drawAPI) { super(drawAPI); this.x = x; this.y = y; this.radius = radius; } public void draw() { drawAPI.drawCircle(radius,x,y); }
}


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
public class BridgePatternDemo { public static void main(String[] args) { Shape redCircle = new Circle(100,100, 10, new RedCircle()); Shape greenCircle = new Circle(100,100, 10, new GreenCircle()); redCircle.draw(); greenCircle.draw(); }
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

打印到控制台:

Drawing Circle[ color: red, radius: 10, x: 100, 100]
Drawing Circle[  color: green, radius: 10, x: 100, 100]

  
 
  • 1
  • 2

模式应用

  • 一些软件的跨平台设计有时候也是应用了桥接模式
  • JDBC的驱动程序,实现了将不同类型的数据库与Java程序的绑定
  • Java虚拟机实现了平台的无关性,Java虚拟机设计就是通过桥接模式

文章来源: smilenicky.blog.csdn.net,作者:smileNicky,版权归原作者所有,如需转载,请联系作者。

原文链接:smilenicky.blog.csdn.net/article/details/88412075

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。