---
title: "java桥接模式"
author: "怪盗西特乐"
date: "2012-04-17T14:56:38Z"
tags: []
categories: ["编程语言"]
slug: "java"
draft: false
lastmod: "2012-04-17T14:56:38Z"
id: 1488467
url: "https://skhero2012.iteye.com/blog/1488467"
---

Uml图如下：

![](http://dl.iteye.com/upload/attachment/0066/7199/ab1e51c8-136a-329a-8687-c4361e37f336.png)

```
//实现者（Implementor）
public interface BuildingDesign {
	public double computerArea();
}
```

 

```
public class HomeDesign implements BuildingDesign {
	double width,length;
	int floorNumber;
	public HomeDesign(double width,double length,int floorNumber) {
		this.width=width;
		this.length=length;
		this.floorNumber=floorNumber;
	}
	@Override
	public double computerArea() {
		return width*length*floorNumber;
	}

}
```

 

```
//抽象(Abstraction)
public abstract class ArchitectureCost {
	BuildingDesign design;
	double unitPrice;
	public abstract double giveCost();
}
```

 

```
//细化抽象
public class BuildingCose extends ArchitectureCost {

	public BuildingCose(BuildingDesign design,double unitPrice) {
		this.design=design;
		this.unitPrice=unitPrice;
	}

	@Override
	public double giveCost() {
		double area = design.computerArea();
		return area*unitPrice;
	}

}
```

   模式使用：

 

```
public class Appliaction {

	public static void main(String[] args) {
		double width = 63, height = 30;
		int floorNumber = 8;
		double unitPrice = 6867.38;
		BuildingDesign design = new HomeDesign(width, height, floorNumber);
		ArchitectureCost cost = new BuildingCose(design, unitPrice);
		double price = cost.giveCost();
		System.out.println(price);
	}

}
```