概念:
将业务的抽象和实现分离,使模块之间的耦合发生在抽象层,与实现无关。
要点:
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();
}
}