第4篇(中)|结构型设计模式沉浸式理解:模块组织的艺术
“你写的代码有点像租来的毛坯房,结构乱、线头露,风一吹就乱了。”
“架构师要做的,是给它装修成宜居豪宅。”
——结构型设计模式的真谛
🧱 一、什么是结构型设计模式?
结构型模式关注的是:
- 类和对象之间如何组合与协作
- 实现系统“结构层面”的清晰与弹性
它解决的是:怎么组合已有的模块、类、接口,形成更大更灵活的系统。
🔖 二、七大结构型设计模式一览
| 模式 | 关键点 | 应用场景 |
|---|---|---|
| 适配器(Adapter) | 接口不兼容但想合作 | 老系统升级、外部接口对接 |
| 装饰器(Decorator) | 动态增强对象功能 | AOP、日志增强、安全包装 |
| 代理(Proxy) | 控制访问、延迟处理 | RPC代理、权限控制、懒加载 |
| 外观(Facade) | 简化接口 | 提供统一API、隐藏内部复杂性 |
| 桥接(Bridge) | 拆分抽象与实现 | 平台-设备解耦、多维组合 |
| 组合(Composite) | 树状结构 | 文件系统、组织结构、菜单结构 |
| 享元(Flyweight) | 共享内部状态,节省内存 | 大量重复对象(字符、图元) |
🌟 1. 适配器模式(Adapter)—— 让“牛头”对“马嘴”
🧭 使用场景:
- 你引入一个第三方库,它的接口和你的代码不兼容
- 老系统和新系统对接
Java 示例:
你想用一个 USB 接口的键盘,但电脑只支持 Type-C?
interface TypeC {
void connect();
}
// 老设备:USB 接口
class USBKeyboard {
public void plug() {
System.out.println("USB 键盘已连接");
}
}
// 适配器:USB -> TypeC
class USBtoTypeCAdapter implements TypeC {
private USBKeyboard keyboard;
public USBtoTypeCAdapter(USBKeyboard keyboard) {
this.keyboard = keyboard;
}
public void connect() {
keyboard.plug();
}
}
使用:
TypeC port = new USBtoTypeCAdapter(new USBKeyboard());
port.connect();
🌟 2. 装饰器模式(Decorator)—— 动态贴功能“标签”
🧭 使用场景:
- 动态添加职责(日志、缓存、安全检查等)
- 避免修改原类代码
Java 示例:
interface Coffee {
String getDesc();
double getCost();
}
// 基础咖啡
class SimpleCoffee implements Coffee {
public String getDesc() { return "黑咖啡"; }
public double getCost() { return 10; }
}
// 加牛奶
class MilkDecorator implements Coffee {
private Coffee coffee;
public MilkDecorator(Coffee coffee) {
this.coffee = coffee;
}
public String getDesc() {
return coffee.getDesc() + " + 牛奶";
}
public double getCost() {
return coffee.getCost() + 2;
}
}
使用:
Coffee c = new SimpleCoffee();
c = new MilkDecorator(c);
System.out.println(c.getDesc()); // 黑咖啡 + 牛奶
🌟 3. 代理模式(Proxy)—— 中介出马,一切搞定
🧭 使用场景:
- 控制访问权限
- 远程代理(如RPC)
- 增加开销控制(如懒加载)
Java 示例:
interface Service {
void request();
}
// 真实类
class RealService implements Service {
public void request() {
System.out.println("处理真实请求");
}
}
// 代理类
class ProxyService implements Service {
private RealService real;
public void request() {
if (real == null) {
real = new RealService();
}
System.out.println("权限校验中...");
real.request();
}
}
🌟 4. 外观模式(Facade)—— 系统的“一键操作面板”
🧭 使用场景:
- 简化复杂子系统调用
- 为模块或子系统提供统一入口
Java 示例:
class CPU {
void start() { System.out.println("CPU启动"); }
}
class Disk {
void load() { System.out.println("硬盘加载"); }
}
class OS {
void boot() { System.out.println("操作系统启动"); }
}
// 外观类
class ComputerFacade {
private CPU cpu = new CPU();
private Disk disk = new Disk();
private OS os = new OS();
public void start() {
cpu.start();
disk.load();
os.boot();
}
}
使用:
new ComputerFacade().start();
🌟 5. 桥接模式(Bridge)—— 抽象与实现分离,各走各路
🧭 使用场景:
- 功能多维扩展(平台+行为)
- 比如图形库支持多平台+多图形
示例结构:
interface DrawAPI {
void draw(String name);
}
class WindowsDraw implements DrawAPI {
public void draw(String name) {
System.out.println("在 Windows 上画 " + name);
}
}
abstract class Shape {
protected DrawAPI drawAPI;
public Shape(DrawAPI api) {
this.drawAPI = api;
}
public abstract void draw();
}
class Circle extends Shape {
public Circle(DrawAPI api) { super(api); }
public void draw() {
drawAPI.draw("圆形");
}
}
使用:
Shape shape = new Circle(new WindowsDraw());
shape.draw();
🌟 6. 组合模式(Composite)—— 树形结构神器
🧭 使用场景:
- 文件系统、组织结构树、菜单树
- 对“整体”和“部分”操作一致
示例:
interface Component {
void display();
}
class Leaf implements Component {
private String name;
public Leaf(String name) { this.name = name; }
public void display() {
System.out.println("节点:" + name);
}
}
class Composite implements Component {
private List<Component> children = new ArrayList<>();
private String name;
public Composite(String name) {
this.name = name;
}
public void add(Component c) {
children.add(c);
}
public void display() {
System.out.println("组:" + name);
for (Component c : children) {
c.display();
}
}
}
🌟 7. 享元模式(Flyweight)—— 节省内存的利器
🧭 使用场景:
- 大量对象存在重复状态
- 比如文本编辑器中的“字体对象”、“棋子对象”
示例:
class Shape {
private String color;
public Shape(String color) {
this.color = color;
}
public void draw() {
System.out.println("绘制颜色为:" + color + " 的圆");
}
}
class ShapeFactory {
private static Map<String, Shape> cache = new HashMap<>();
public static Shape getShape(String color) {
if (!cache.containsKey(color)) {
cache.put(color, new Shape(color));
}
return cache.get(color);
}
}
使用:
Shape red1 = ShapeFactory.getShape("red");
Shape red2 = ShapeFactory.getShape("red");
System.out.println(red1 == red2); // true,共享对象
📊 三、总结:结构型模式核心逻辑对照表
| 模式 | 场景 | 关键词 |
|---|---|---|
| 适配器 | 老接口适配新系统 | 兼容性 |
| 装饰器 | 动态功能增强 | 可叠加 |
| 代理 | 控制访问,延迟处理 | 替身/中介 |
| 外观 | 简化子系统 | 门面 |
| 桥接 | 多维度组合 | 拆抽象与实现 |
| 组合 | 树状结构 | 部分-整体一致性 |
| 享元 | 重复对象共享 | 内部状态缓存 |
“结构混乱,系统易垮;结构合理,扩展如画。”
——设计模式使用心得
✅ 下一篇预告:
📖 第4篇(下)|行为型设计模式沉浸式理解:把“业务流程”变成可控套路!

被折叠的 条评论
为什么被折叠?



