桥接模式

概念:

将业务的抽象和实现分离,使模块之间的耦合发生在抽象层,与实现无关。

要点:

1、定义抽象(抽象类或接口)和实现

2、在抽象层实现低耦合

实例:

定义两组工具,一组用来画线,另一组用来画图形。每组工具都采用抽象+实现的方式来编写代码,其中画图形工具会引用画线工具。

1、定义一个坐标点类

/**
 * 坐标点类
 * 
 * @author ypqiao
 *
 */
public class Point {
	
	private int x;
	private int y;
	
	public Point(){
		
	}
	
	public String toString(){
		
		return "x: "+x+" y: "+y;
	}
	
	public Point( int x,int y){
		this.x = x;
		this.y = y;
	}

	public int getX() {
		return x;
	}

	public int getY() {
		return y;
	}
}
/**
 * 画图工具抽象类
 * 
 * @author ypqiao
 *
 */
public abstract class AbstractDrawGraph {
	
	protected DrawLine drawLineTool;		// 引用的画图工具
	
	public AbstractDrawGraph(){
		
	}
	
	public AbstractDrawGraph( DrawLine drawLineTool ){
		this.drawLineTool = drawLineTool;
	}

	
	/**
	 * 画图方法
	 * 
	 * @throws Exception
	 */
	public abstract void drawGraph() throws Exception;

}
/**
 * 画图抽象类实现
 * 画一条直线
 * @author ypqiao
 *
 */
public class DrawGraph_Line extends AbstractDrawGraph {
	
	public DrawGraph_Line(){
		super();
	}
	
	public DrawGraph_Line( DrawLine drawLineTool ){
		super(drawLineTool);
	}

	@Override
	public String toString() {
		return "DrawGraphTool: DrawGraph_Line"+" DrawLineTool: "+drawLineTool.getClass().getName();
	}

	@Override
	public void drawGraph() throws Exception {
		System.out.println(this.toString());
		
		Point start = new Point(0,0);
		Point end = new Point(1,0);
		
		drawLineTool.drawLine(start, end);
		
		System.out.println("draw a line..");
	}

}
/**
 * 画图抽象类实现
 * 画一个正方形
 * @author ypqiao
 *
 */
public class DrawGraph_Square extends AbstractDrawGraph {

	public DrawGraph_Square(){
		super();
	}
	
	public DrawGraph_Square( DrawLine drawLineTool ){
		super(drawLineTool);
	}
	
	
	@Override
	public String toString() {
		return "DrawGraphTool: DrawGraph_Square"+" DrawLineTool: "+drawLineTool.getClass().getName();
	}
	
	@Override
	public void drawGraph() throws Exception {
		System.out.println(this.toString());
		
		Point start = new Point(0,0);
		Point end_x = new Point(1,0);
		Point end_y = new Point(0,1);
		
		drawLineTool.drawLine(start, end_x);
		drawLineTool.drawLine(start, end_y);
		
		System.out.println("draw a Square..");

	}

}
/**
 * 画图工具抽象接口
 * 
 * @author ypqiao
 *
 */
public interface DrawLine {
	
	public void drawLine( Point start, Point end ) throws Exception;

}
/**
 * 
 * 画线工具Unix实现
 * 
 * @author ypqiao
 *
 */
public class DrawLine_Unix implements DrawLine {

	@Override
	public void drawLine(Point start, Point end) throws Exception {
		System.out.println("unix-line: \n\tstart: "+start+"\n\tend: "+end);
	}

}
/**
 * 
 * 画线工具Win实现
 * 
 * @author ypqiao
 *
 */
public class DrawLine_Win implements DrawLine {

	@Override
	public void drawLine(Point start, Point end) throws Exception {
		System.out.println("windows7-line : \n\tstart: "+start+"\n\tend: "+end);
	}

}
/**
 * 
 * 画线工具Linux实现
 * 
 * @author ypqiao
 *
 */
public class DrawLine_Linux implements DrawLine {

	@Override
	public void drawLine(Point start, Point end) throws Exception {
		System.out.println("linux-line: \n\tstart: "+start+"\n\tend: "+end);
	}

}
public class Test {
	
	public static void main( String[] args ) throws Exception{
		AbstractDrawGraph drawGraph_tool = null ;
		DrawLine drawLine_tool = null;
		
//		drawLine_tool = new DrawLine_Win();
//		drawGraph_tool = new DrawGraph_Line(drawLine_tool);
		
		drawLine_tool = new DrawLine_Linux();
		drawGraph_tool = new DrawGraph_Square(drawLine_tool);
		
		drawGraph_tool.drawGraph();
	}
}










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值