橋接模式(Bridge Pattern):將抽象部分與其實現(xiàn)部分分離,使它們可以獨立地變化。它是一種對象結(jié)構(gòu)型設計模式。在軟件系統(tǒng)中,如果某一部分功能存在2個變化的緯度,而這兩個緯度可以相互分離,互不影響。因此便可以使用橋接模式,將兩個變化的緯度進行抽象設計,兩個抽象部分相關(guān)聯(lián),具體實現(xiàn)部分互相無任何關(guān)系。

橋接模式.jpg
1)Client(客戶端):外部使用程序
2)Abstraction(抽象類):定義抽象類的接口,一般將最基礎(chǔ)業(yè)務方法的一個緯度定義為抽象類,并在類中定義抽象方法;
3)Implementor(實現(xiàn)類接口):定義實現(xiàn)類的接口,與Abstraction不同的另外一個緯度的基類或者是接口;
4)RefinderAbstracion(擴充抽象類):抽象類的實現(xiàn)者,根據(jù)不同變化緯度對應不同類;
5)ConcreteImplementor(具體實現(xiàn)類):實現(xiàn)抽象類接口,完成不同實現(xiàn);
示例:不同操作系統(tǒng),顯示不同格式的圖片, 維度一:操作平臺(Windows Linux Mobile),維度二:圖片格式(JPG, PDF, PNG)
操作系統(tǒng)類 相當于Abstraction
/**
*
* @author apple
*
* 操作系統(tǒng)
*
*/
public abstract class OperatingSystem {
// 圖片格式類
private IPictureFormat pictureFormat;
public OperatingSystem(IPictureFormat pictureFormat) {
super();
this.pictureFormat = pictureFormat;
}
/**
* 顯示操作系統(tǒng)
*/
protected void operatingSystemtype() {
this.pictureFormat.imageFormat();
}
}
windows、linux、mobile 相當于RefinderAbstracion(擴充抽象類)
/**
*
* @author apple
*
* Windows
*
*/
public class Windows extends OperatingSystem {
public Windows(IPictureFormat pictureFormat) {
super(pictureFormat);
// TODO Auto-generated constructor stub
}
@Override
protected void operatingSystemtype() {
// TODO Auto-generated method stub
super.operatingSystemtype();
System.out.println(" 圖片顯示在 Windows");
}
}
/**
*
* @author apple
*
* Linux
*
*/
public class Linux extends OperatingSystem {
public Linux(IPictureFormat pictureFormat) {
super(pictureFormat);
// TODO Auto-generated constructor stub
}
@Override
protected void operatingSystemtype() {
// TODO Auto-generated method stub
super.operatingSystemtype();
System.out.println(" 圖片顯示在 Linux");
}
}
/**
*
* @author apple
*
* Mobile
*
*/
public class Mobile extends OperatingSystem {
public Mobile(IPictureFormat pictureFormat) {
super(pictureFormat);
// TODO Auto-generated constructor stub
}
@Override
protected void operatingSystemtype() {
// TODO Auto-generated method stub
super.operatingSystemtype();
System.out.println(" 圖片顯示在 Mobile");
}
}
IPictureFormat 相當于 Implementor(實現(xiàn)類接口)
/**
*
* @author apple
*
* 圖片格式
*
*/
public interface IPictureFormat {
/**
* 顯示圖片格式
*/
void imageFormat();
}
PNG、JPG、PNG 相當于ConcreteImplementor
/**
*
* @author apple
*
* PNG
*
*/
public class PNG implements IPictureFormat {
@Override
public void imageFormat() {
// TODO Auto-generated method stub
System.out.print("圖片格式:PNG");
}
}
/**
*
* @author apple
*
* PDF
*
*
*/
public class PDF implements IPictureFormat {
@Override
public void imageFormat() {
// TODO Auto-generated method stub
System.out.print("圖片格式:PDF");
}
}
/**
*
* @author apple
*
* JPG
*
*
*/
public class JPG implements IPictureFormat {
@Override
public void imageFormat() {
// TODO Auto-generated method stub
System.out.print("圖片格式:JPG");
}
}
public class Client {
public static void main(String[] args) {
// TODO Auto-generated method stub
// 不同操作系統(tǒng),顯示不同格式的圖片, 維度一:操作平臺(Windows Linux Mobile),維度二:圖片格式(JPG, PDF, PNG)
// Mobile : PNG
Mobile mobile = new Mobile(new PNG());
mobile.operatingSystemtype();
// Linux : PDF
Linux linux = new Linux(new PDF());
linux.operatingSystemtype();
// Windows : JPG
Windows windows = new Windows(new JPG());
windows.operatingSystemtype();
}
}