JAVA设计模式之【桥接模式】

桥接模式
	蜡笔中颜色和型号之间存在耦合
	毛笔中,颜色和型号解耦了
	如果软件系统中某个类存在两个独立变化的维度,桥接模式可以将两个维度分离出来
	角色
		抽象类
		扩充抽象类
		实现类接口
			提供基本操作
			抽象类定义的接口会做更多更负责的操作
			使用关联关系,替代继承关系
		具体实现类
	面向对象思想
		单一职责原则
		开闭原则
		合成复用原则
		里氏替换原则
		依赖倒转原则
	毛笔案例
		毛笔型号维度
			大毛笔
			中毛笔
			小毛笔
		型号是毛笔的抽象部分,颜色是毛笔的实现部分
		毛笔颜色维度
			黑色
			红色
			灰色
			蓝色
			...
		毛笔和颜色之间是聚合关系
	图片案例
		Image充当抽象类
		子类JPGImage,PNGImage、BMPImage和GIFImage充当扩充抽象类
		ImageImp充当实现类接口
		子类WindowsImp、LinuxImp、UnixImp充当具体实现类
		像素辅助类,Matrix,不同操作系统提供不同的方式显示像素矩阵
	桥接模式可以和适配器模式结合使用

1.图片实现类接口

package Bridge;

/**
 * Created by e550 on 2016/10/3.
 */
public interface ImageImp {
    public void doPaint(Matrix m);
}

2.图片抽象类

package Bridge;

/**
 * Created by e550 on 2016/10/3.
 */
abstract class Image {
    protected ImageImp imp;
    public void setImageImp(ImageImp imp) {
        this.imp = imp;
    }

    public abstract void parseFile(String fileName);
}

3.图片扩充抽象类

JPG

package Bridge;

/**
 * Created by e550 on 2016/10/3.
 */
public class JPGImage extends Image{
    public void parseFile(String fileName) {
        Matrix m = new Matrix();
        imp.doPaint(m);
        System.out.println(fileName + ",格式为JPG");
    }
}

PNG

package Bridge;

/**
 * Created by e550 on 2016/10/3.
 */
public class PNGImage extends Image{
    public void parseFile(String fileName) {
        Matrix m = new Matrix();
        imp.doPaint(m);
        System.out.println(fileName + ",格式为PNG。");
    }
}

GIF

package Bridge;

/**
 * Created by e550 on 2016/10/3.
 */
public class GIFImage extends Image{
    public void parseFile(String fileName) {
        Matrix m = new Matrix();
        imp.doPaint(m);
        System.out.println(fileName + ",格式为GIF。");
    }
}

BMP

package Bridge;

/**
 * Created by e550 on 2016/10/3.
 */
public class BMPImage extends Image{
    public void parseFile(String fileName) {
        Matrix m = new Matrix();
        imp.doPaint(m);
        System.out.println(fileName + ",格式为BMP。");
    }
}

4.具体实现类
Windows

package Bridge;

/**
 * Created by e550 on 2016/10/3.
 */
public class WindowsImp implements ImageImp{
    public void doPaint(Matrix m) {
        //调用Windows系统的绘制函数绘制像素矩阵
        System.out.println("在Windows操作系统中显示图像:");
    }
}

Linux

package Bridge;

/**
 * Created by e550 on 2016/10/3.
 */
public class LinuxImp implements ImageImp{
    public void doPaint(Matrix m) {
        //调用Windows系统的绘制函数绘制像素矩阵
        System.out.println("在Linux操作系统中显示图像:");
    }

}

Unix

package Bridge;

/**
 * Created by e550 on 2016/10/3.
 */
public class UnixImp implements ImageImp{
    public void doPaint(Matrix m) {
        System.out.println("在Unix操作系统中显示图像:");
    }
}

5.客户端

package Bridge;

/**
 * Created by e550 on 2016/10/3.
 */
public class Client {
    public static void main(String args[]) {
        Image image;
        ImageImp imp;
        image  = new GIFImage();
        imp    = new LinuxImp();
        image.setImageImp(imp);
        image.parseFile(" 日出美景");
    }
}

执行结果
在Linux操作系统中显示图像:
日出美景,格式为GIF。

再看一个笔的例子
1.Color接口

package Bridge.PenColor;

/**
 * Created by e550 on 2016/10/3.
 */
public interface Color {
    void bepaint(String penType,String name);
}

2.Pen抽象类

package Bridge.PenColor;

/**
 * Created by e550 on 2016/10/3.
 */
public abstract class Pen {
    protected Color color;
    public void setColor(Color color) {
        this.color = color;
    }
    public abstract void draw(String name);
}

3.Color接口实现

package Bridge.PenColor;

/**
 * Created by e550 on 2016/10/3.
 */
public class Black implements Color{
    public void bepaint(String penType,String name) {
        System.out.println(penType + "黑色的" + name + ".");
    }
}

package Bridge.PenColor;

/**
 * Created by e550 on 2016/10/3.
 */
public class Blue implements Color{
    public void bepaint(String penType,String name) {
        System.out.println(penType + "蓝色的" + name + ".");
    }
}

4.Pen抽象类实现

package Bridge.PenColor;

/**
 * Created by e550 on 2016/10/3.
 */
public class BigPen extends Pen{
    public void draw(String name) {
        String penType="大号毛笔绘制";
        this.color.bepaint(penType,name);
    }
}

package Bridge.PenColor;

/**
 * Created by e550 on 2016/10/3.
 */
public class MiddlePen extends Pen{
    public void draw(String name) {
        String penType = "中型笔绘制";
        this.color.bepaint(penType,name);
    }
}

package Bridge.PenColor;

/**
 * Created by e550 on 2016/10/3.
 */
public class SmallPen extends Pen{
    public void draw(String name) {
        String penType = "小型笔绘制";
        this.color.bepaint(penType,name);
    }
}

5.客户端

package Bridge.PenColor;

/**
 * Created by e550 on 2016/10/3.
 */
public class Client {
    public static void main(String args[]) {
        Color color;
        Pen pen;
        color = new Blue();
        pen = new BigPen();
        pen.setColor(color);
        pen.draw("鲜花");
    }
}

执行结果
大号毛笔绘制蓝色的鲜花

posted @ 2016-10-03 19:15  TBHacker  阅读(2189)  评论(0编辑  收藏  举报