标签:
桥接模式 Bridge Pattern 结构设计模式
例如:
public interface Shape { public void bepaint(String color); }
public abstract class Color { Shape shape; public void setShape(Shape shape) { this.shape = shape; } public abstract void draw(); }
public class Red extends Color { @Override public void draw() { shape.bepaint("红色"); } }
public class Green extends Color { @Override public void draw() { shape.bepaint("绿色"); } }
public class Circle implements Shape { @Override public void bepaint(String color) { System.out.println(color + "的圆形"); } }
public class Square implements Shape { @Override public void bepaint(String color) { System.out.println(color + "的正方形"); } }
public class Test { /** * @param args */ public static void main(String[] args) { Shape circle_shape = new Circle(); Color red_color = new Red(); red_color.setShape(circle_shape); red_color.draw(); System.out.println("-----------------"); Shape square_shape = new Square(); red_color.setShape(square_shape); red_color.draw(); } }
实验结果:
红色的圆形 ----------------- 红色的正方形
设计难度比较大,要能正确识别系统中独立变化的维度,具有局限性。
标签:
原文地址:http://www.cnblogs.com/androidsuperman/p/5862966.html