📄 +
字号:
<html><!-- #BeginTemplate "/Templates/module.dwt" --><!-- DW6 -->
<head>
<!-- #BeginEditable "doctitle" -->
<title>设计模式之Bridge 桥接模式 Bridge模式
</title>
<!-- #EndEditable -->
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link href="../jdon.css" rel="stylesheet" type="text/css">
<link rel="shortcut icon" href="http://www.jdon.com/favicon.ico">
<meta http-equiv="refresh" content="1200">
</head>
<body bgcolor="#014E82" text="#000000">
<table width="750" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="129" rowspan="3" align="center" valign="bottom" bgcolor="#FFFFFF">
<table width="100%" border="0" cellpadding="0" cellspacing="0" >
<tr>
<td align="center" >
<img src="http://www.jdon.com/images/jdon.gif" width="120" height="60"></td>
</tr>
<tr>
<td height="9"></td>
</tr>
</table>
</td>
<td valign="bottom" >
<table cellpadding="0" cellspacing="0" height="100%" width="100%" >
<tr bgcolor="#CAC9BB">
<td height="70" align="right" valign="bottom" bgcolor="#CAC9BB">
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="619" height="60">
<param name="movie" value="../images/jd.swf">
<param name="quality" value="high"><param name="BGCOLOR" value="#CAC9BB">
<embed src="../images/jd.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="619" height="60"></embed>
</object></td>
</tr>
</table></td>
</tr>
<tr>
<td>
<table cellpadding="0" cellspacing="1" bgcolor="#CCCCCC" >
<tr>
<td height="26" bgcolor="#707070" width="222"> </td>
<td height="26" width="70" bgcolor="#707070" class="unnamed2"> <div align="center"><a href="http://www.jdon.com/index.html" class="a03">首 页</a></div></td>
<td height="26" width="70" bgcolor="#707070" class="unnamed2"> <div align="center"><a href="http://www.jdon.com/design.htm" class="a03">设计研究</a></div></td>
<td height="26" width="70" bgcolor="#707070" class="unnamed2"> <div align="center"><a href="http://www.jdon.com/product.htm" class="a03">构件产品</a></div></td>
<td height="26" width="70" bgcolor="#707070" class="unnamed2"> <div align="center"><a href="http://www.jdon.com/trainning.htm" class="a03">培训咨询</a></div></td>
<td height="26" width="70" bgcolor="#707070"> <div align="center" class="unnamed2"><a href="http://www.jdon.com/jdonframework/" class="a03">Jdon框架</a></div></td>
<td height="26" width="70" bgcolor="#707070"> <div align="center" class="unnamed2"><a href="http://www.jdon.com/jive/index.jsp" class="a03">论坛</a></div></td>
</tr></table>
</td>
</tr>
</table>
<table width="750" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td height="11" colspan="7" bgcolor="#FFFFFF" class="bianxian">
<table width="100%" border="0" cellspacing="0" cellpadding="0" height="1" bordercolor="#000000">
<tr>
<td>
<table width="100%" border="0" cellspacing="0" cellpadding="0" height="2" bgcolor="#000000">
<tr>
<td></td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td bgcolor="#FFFFFF" valign="top">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="130" valign="top" bgcolor="#FFFFD7"><table width="100%" border="0" cellspacing="0" cellpadding="3" height="200" align="center">
<tr>
<td align="left" valign="top"><!-- #BeginEditable "Menu" --> <!-- #EndEditable --> </td>
</tr>
<tr>
<td align="right">
<p><a href="http://www.jdon.com/trainning/jiaocheng.htm" target="_blank"><font color="#FF9999"><strong>在线教程</strong></font><br>
<font color="#555555"> 原创教程区</font></a></p>
<p><a href="http://www.jdon.com/jdonframework/app.htm" target="_blank"><font color="#FF9999"><strong>Jdon框架</strong></font><br>
<font color="#555555"> 在线运行演示</font></a></p>
<br>
<div id=vgad ></div>
<!-- #BeginEditable "Menu2" --> <!-- #EndEditable -->
</td>
</tr>
</table>
</td>
<td width="1" height="100%" >
<table height="100%" cellpadding="0" cellspacing="0" background="../images/blackpoint.gif" >
<tr><td> <img name="" src="" width="1" height="1" alt="" ></td></tr></table>
</td >
<td valign="top" >
<table width="100%" border="0" cellspacing="13" cellpadding="2">
<tr>
<td class="article"><!-- #BeginEditable "main" -->
<h3 align="center">设计模式之Bridge</h3>
<p align="center"><a href="http://www.jdon.com/aboutme.htm">板桥里人</a> http://www.jdon.com 2002/05/01</p>
<p align="center"><a href="../mybook/index.htm" target="_blank"><strong>模式实战书籍《Java实用系统开发指南》</strong></a></p>
<p> <strong>Bridge模式定义 </strong>:<br>
将抽象和行为划分开来,各自独立,但能动态的结合。</p>
<p>任何事物对象都有抽象和行为之分,例如人,人是一种抽象,人分男人和女人等;人有行为,行为也有各种具体表现,所以,“人”与“人的行为”两个概念也反映了抽象和行为之分。</p>
<p>在面向对象设计的基本概念中,对象这个概念实际是由属性和行为两个部分组成的,属性我们可以认为是一种静止的,是一种抽象,一般情况下,行为是包含在一个对象中,但是,在有的情况下,我们需要将这些行为也进行归类,形成一个总的行为接口,这就是桥模式的用处。</p>
<p> <strong>为什么使用?</strong><br>
不希望抽象部分和行为有一种固定的绑定关系,而是应该可以动态联系的。</p>
<p>如果一个抽象类或接口有多个具体实现(子类、concrete subclass),这些子类之间关系可能有以下两种情况:<br>
1. 这多个子类之间概念是并列的,如前面举例,打桩,有两个concrete class:方形桩和圆形桩;这两个形状上的桩是并列的,没有概念上的重复。<br>
<br>
2.这多个子类之中有内容概念上重叠.那么需要我们把抽象共同部分和行为共同部分各自独立开来,原来是准备放在一个接口里,现在需要设计两个接口:抽象接口和行为接口,分别放置抽象和行为.<br>
<br>
例如,一杯咖啡为例,子类实现类为四个:中杯加奶、大杯加奶、
中杯不加奶、大杯不加奶。</p>
<p>但是,我们注意到:上面四个子类中有概念重叠,可从另外一个角度进行考虑,这四个类实际是两个角色的组合:抽象 和行为,其中抽象为:中杯和大杯;行为为:加奶
不加奶(如加橙汁 加苹果汁). </p>
<p>实现四个子类在抽象和行为之间发生了固定的绑定关系,如果以后动态增加加葡萄汁的行为,就必须再增加两个类:中杯加葡萄汁和大杯加葡萄汁。显然混乱,扩展性极差。</p>
<p>那我们从分离抽象和行为的角度,使用Bridge模式来实现。</p>
<p> <strong>如何实现?</strong><br>以上面提到的咖啡 为例. 我们原来打算只设计一个接口(抽象类),使用Bridge模式后,我们需要将抽象和行为分开,加奶和不加奶属于行为,我们将它们抽象成一个专门的行为接口.</p>
<p>先看看抽象部分的接口代码:<br></p>
<table width="80%" border="0" cellspacing="3" cellpadding="3">
<tr>
<td bgcolor="#cccccc"> public abstract class Coffee<br>{<br> CoffeeImp coffeeImp;<br><br> public void setCoffeeImp() {<br> this.CoffeeImp =
CoffeeImpSingleton.getTheCoffeImp();<br> }<br><br>
public CoffeeImp getCoffeeImp() {return this.CoffeeImp;}<br>
<br> public abstract void pourCoffee();<br>}<br>
</td>
</tr>
</table>
<p>其中CoffeeImp 是加不加奶的行为接口,看其代码如下:</p>
<table width="80%" border="0" cellspacing="3" cellpadding="3">
<tr>
<td bgcolor="#cccccc"> public abstract class CoffeeImp<br>{<br> public abstract void pourCoffeeImp();<br>}<br>
</td>
</tr>
</table>
<p>现在我们有了两个抽象类,下面我们分别对其进行继承,实现concrete class:</p>
<table width="80%" border="0" cellspacing="3" cellpadding="3">
<tr>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -