标签:
桥接模式是针对某个类如果存在多个维度的变化时,将不同变化的维度单独抽象出来,独自变化。如下例:
之前的设计:
采用桥接模式:
代码如下:
public abstract class Color { public void draw(){}; }
public class BlueColor extends Color { public void draw(){ System.out.println("draw blue"); }; }
public abstract class Shape { Color color; public void buildShape(){}; }
public class RectangleShape extends Shape { public void buildShape(){ this.color.draw(); System.out.println("buildShape Rectangle"); }; }
public class App { public static void main(String[] args) { Shape shape = new RectangleShape(); shape.color = new BlueColor(); shape.buildShape(); } }
标签:
原文地址:http://www.cnblogs.com/Fredric-2013/p/4571818.html