import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
public class ExploreBridgePattern {
interface DrawAPI {
String drawCircle(int radius, int x, int y);
String colorName();
}
static class RedCircle implements DrawAPI {
@Override
public String drawCircle(int radius, int x, int y) {
return "Drawing Circle[ color: red, radius: " + radius + ", x: " + x + ", " + y + "]";
}
@Override
public String colorName() {
return "red";
}
}
static class GreenCircle implements DrawAPI {
@Override
public String drawCircle(int radius, int x, int y) {
return "Drawing Circle[ color: green, radius: " + radius + ", x: " + x + ", " + y + "]";
}
@Override
public String colorName() {
return "green";
}
}
static class BlueCircle implements DrawAPI {
@Override
public String drawCircle(int radius, int x, int y) {
return "Drawing Circle[ color: blue, radius: " + radius + ", x: " + x + ", " + y + "]";
}
@Override
public String colorName() {
return "blue";
}
}
static class SvgDrawAPI implements DrawAPI {
private final String color;
SvgDrawAPI(String color) {
this.color = color;
}
@Override
public String drawCircle(int radius, int x, int y) {
return "<circle cx=\"" + x + "\" cy=\"" + y + "\" r=\"" + radius + "\" fill=\"" + color + "\"/>";
}
@Override
public String colorName() {
return "svg-" + color;
}
}
static abstract class Shape {
protected DrawAPI drawAPI;
protected Shape(DrawAPI drawAPI) {
this.drawAPI = drawAPI;
}
public void setDrawAPI(DrawAPI drawAPI) {
this.drawAPI = drawAPI;
}
public abstract String draw();
}
static class Circle extends Shape {
private final int x;
private final int y;
private final int radius;
public Circle(int x, int y, int radius, DrawAPI drawAPI) {
super(drawAPI);
this.x = x;
this.y = y;
this.radius = radius;
}
@Override
public String draw() {
return drawAPI.drawCircle(radius, x, y);
}
}
static class RingShape extends Shape {
private final int x;
private final int y;
private final int outerRadius;
public RingShape(int x, int y, int outerRadius, DrawAPI drawAPI) {
super(drawAPI);
this.x = x;
this.y = y;
this.outerRadius = outerRadius;
}
@Override
public String draw() {
return "Ring(" + drawAPI.colorName() + ") -> " + drawAPI.drawCircle(outerRadius, x, y);
}
}
public static void main(String[] args) {
List<Map<String, Object>> cases = new ArrayList<Map<String, Object>>();
// Case 1: Classic Shape + DrawAPI bridge demo
Shape redCircle = new Circle(100, 100, 10, new RedCircle());
Shape greenCircle = new Circle(100, 100, 10, new GreenCircle());
addCase(cases, "shape_drawapi_color_bridge",
"验证抽象类 Shape 通过聚合 DrawAPI 实现类分别绘制红色圆与绿色圆。",
Arrays.asList(
"Drawing Circle[ color: red, radius: 10, x: 100, 100]",
"Drawing Circle[ color: green, radius: 10, x: 100, 100]"
),
Arrays.asList(redCircle.draw(), greenCircle.draw()));
// Case 2: Orthogonal M x N combinations without M*N subclasses
List<DrawAPI> apis = Arrays.<DrawAPI>asList(new RedCircle(), new GreenCircle(), new BlueCircle());
List<String> combos = new ArrayList<String>();
for (DrawAPI api : apis) {
combos.add("Circle:" + api.colorName());
combos.add("Ring:" + api.colorName());
}
Map<String, Object> orthoExp = new LinkedHashMap<String, Object>();
orthoExp.put("combination_count", 6);
orthoExp.put("concrete_classes_needed", 5); // 2 refined abstractions + 3 concrete implementors
orthoExp.put("combos", Arrays.asList("Circle:red", "Ring:red", "Circle:green", "Ring:green", "Circle:blue", "Ring:blue"));
Map<String, Object> orthoAct = new LinkedHashMap<String, Object>();
orthoAct.put("combination_count", combos.size());
orthoAct.put("concrete_classes_needed", 2 + apis.size());
orthoAct.put("combos", combos);
addCase(cases, "orthogonal_dimensions_avoid_class_explosion",
"验证形状(2 维)与颜色渲染实现(3 维)正交组合,仅需 2+3=5 个具体类即可覆盖 2×3=6 种运行态组合。",
orthoExp,
orthoAct);
// Case 3: Runtime swapping of implementor
Shape dynamicCircle = new Circle(20, 30, 5, new RedCircle());
String beforeSwap = dynamicCircle.draw();
dynamicCircle.setDrawAPI(new BlueCircle());
String afterSwap = dynamicCircle.draw();
addCase(cases, "runtime_implementor_swap",
"验证运行期动态替换 DrawAPI 桥接实现后,同一 Shape 实例立即切换渲染颜色。",
Arrays.asList(
"Drawing Circle[ color: red, radius: 5, x: 20, 30]",
"Drawing Circle[ color: blue, radius: 5, x: 20, 30]"
),
Arrays.asList(beforeSwap, afterSwap));
// Case 4: Extending a new implementor (SvgDrawAPI) works immediately with both Circle & RingShape
DrawAPI svgApi = new SvgDrawAPI("navy");
Shape svgCircle = new Circle(50, 60, 12, svgApi);
Shape svgRing = new RingShape(50, 60, 18, svgApi);
addCase(cases, "independent_dimension_extension",
"验证新增 SvgDrawAPI 实现类无需修改既有 Circle 与 RingShape 即可直接协作。",
Arrays.asList(
"<circle cx=\"50\" cy=\"60\" r=\"12\" fill=\"navy\"/>",
"Ring(svg-navy) -> <circle cx=\"50\" cy=\"60\" r=\"18\" fill=\"navy\"/>"
),
Arrays.asList(svgCircle.draw(), svgRing.draw()));
Map<String, Object> root = new LinkedHashMap<String, Object>();
root.put("runtime", "OpenJDK " + System.getProperty("java.version"));
root.put("cases", cases);
System.out.println(toJson(root));
}
private static void addCase(List<Map<String, Object>> cases, String name, String summary, Object expected, Object actual) {
Map<String, Object> c = new LinkedHashMap<String, Object>();
c.put("name", name);
c.put("summary", summary);
c.put("expected", expected);
c.put("actual", actual);
c.put("passed", Objects.equals(expected, actual));
cases.add(c);
}
private static String toJson(Object obj) {
if (obj == null) return "null";
if (obj instanceof Boolean || obj instanceof Number) return obj.toString();
if (obj instanceof String) return escapeString((String) obj);
if (obj instanceof List<?>) {
StringBuilder sb = new StringBuilder("[");
List<?> list = (List<?>) obj;
for (int i = 0; i < list.size(); i++) {
if (i > 0) sb.append(",");
sb.append(toJson(list.get(i)));
}
return sb.append("]").toString();
}
if (obj instanceof Map<?, ?>) {
StringBuilder sb = new StringBuilder("{");
boolean first = true;
for (Map.Entry<?, ?> e : ((Map<?, ?>) obj).entrySet()) {
if (!first) sb.append(",");
first = false;
sb.append(escapeString(String.valueOf(e.getKey()))).append(":").append(toJson(e.getValue()));
}
return sb.append("}").toString();
}
return escapeString(obj.toString());
}
private static String escapeString(String s) {
StringBuilder sb = new StringBuilder("\"");
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
switch (ch) {
case '"': sb.append("\\\""); break;
case '\\': sb.append("\\\\"); break;
case '\b': sb.append("\\b"); break;
case '\f': sb.append("\\f"); break;
case '\n': sb.append("\\n"); break;
case '\r': sb.append("\\r"); break;
case '\t': sb.append("\\t"); break;
default:
if (ch < 0x20) {
sb.append(String.format("\\u%04x", (int) ch));
} else {
sb.append(ch);
}
}
}
return sb.append("\"").toString();
}
}
本例需在 Windows x64、Eclipse Adoptium OpenJDK 1.8.0_492 (javac + java) 本地环境。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、Eclipse Adoptium OpenJDK 1.8.0_492 (javac + java) 本地环境。
入口文件:projects/design-pattern-bridge-pattern-lab/ExploreBridgePattern.java
本地运行步骤快速上手
- 点击上方“保存源码”保存为
ExploreBridgePattern.java,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、Eclipse Adoptium OpenJDK 1.8.0_492 (javac + java) 本地环境。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联36章) · 含当前章节或实例 · mbskycom202612a2686.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2687.zip