结构型模式
约 722 字大约 2 分钟
设计模式结构型模式
2026-04-14
概述
结构型模式关注对象和类的组织,形成更大的结构。
模式分类
| 模式 | 描述 |
|---|---|
| 适配器模式 | 转换接口使其兼容 |
| 桥接模式 | 分离抽象与实现 |
| 组合模式 | 整体与部分层次 |
| 装饰器模式 | 动态添加职责 |
| 外观模式 | 提供统一接口 |
| 亨元模式 | 共享细粒度对象 |
| 代理模式 | 控制访问 |
适配器模式 (Adapter)
将一个类的接口转换成客户希望的另一个接口。
class Adaptee {
specificRequest() {
return 'Adaptee specific behavior';
}
}
class Adapter {
constructor(adaptee) {
this.adaptee = adaptee;
}
request() {
return this.adaptee.specificRequest();
}
}
// 使用
const adaptee = new Adaptee();
const adapter = new Adapter(adaptee);
adapter.request();应用场景
- 集成第三方库
- 兼容新旧接口
桥接模式 (Bridge)
将抽象部分与它的实现部分分离,使它们都可以独立地变化。
// 实现部分
class Implementor {
operationImpl() {}
}
class ConcreteImplementorA extends Implementor {
operationImpl() {
return 'A';
}
}
class ConcreteImplementorB extends Implementor {
operationImpl() {
return 'B';
}
}
// 抽象部分
class Abstraction {
constructor(implementor) {
this.implementor = implementor;
}
operation() {
return this.implementor.operationImpl();
}
}应用场景
- 多个维度变化
- 避免类爆炸
组合模式 (Composite)
将对象组合成树形结构以表示"部分-整体"的层次结构。
class Component {
constructor(name) {
this.name = name;
}
add(component) {}
remove(component) {}
operation() {
throw new Error('必须实现');
}
}
class Leaf extends Component {
operation() {
return this.name;
}
}
class Composite extends Component {
constructor(name) {
super(name);
this.children = [];
}
add(component) {
this.children.push(component);
return this;
}
operation() {
return this.name + '(' + this.children.map(c => c.operation()).join('+') + ')';
}
}应用场景
- 文件系统
- 组织架构
装饰器模式 (Decorator)
动态地给对象添加一些额外的职责。
class Component {
operation() {
return 'Component';
}
}
class Decorator {
constructor(component) {
this.component = component;
}
operation() {
return this.component.operation();
}
}
class ConcreteDecoratorA extends Decorator {
operation() {
return `A(${super.operation()})`;
}
}
class ConcreteDecoratorB extends Decorator {
operation() {
return `B(${super.operation()})`;
}
}应用场景
- 日志功能
- 缓存
- 事务管理
外观模式 (Facade)
为子系统中的一组接口提供一个一致的界面。
class Subsystem1 {
operation1() {
return 'Subsystem1';
}
}
class Subsystem2 {
operation2() {
return 'Subsystem2';
}
}
class Facade {
constructor() {
this.subsystem1 = new Subsystem1();
this.subsystem2 = new Subsystem2();
}
operation() {
return this.subsystem1.operation1() + this.subsystem2.operation2();
}
}应用场景
- 简化复杂系统接口
- 层次化结构
亨元模式 (Flyweight)
运用共享技术有效地支持大量细粒度的对象。
class FlyweightFactory {
constructor() {
this.flyweights = {};
}
get(key) {
if (!this.flyweights[key]) {
this.flyweights[key] = new Flyweight(key);
}
return this.flyweights[key];
}
}
class Flyweight {
constructor(sharedState) {
this.sharedState = sharedState;
}
operation(uniqueState) {
return `${this.sharedState} - ${uniqueState}`;
}
}应用场景
- 大量相似对象
- 对象池
代理模式 (Proxy)
为其他对象提供一种代理以控制对这个对象的访问。
class RealSubject {
request() {
return 'RealSubject';
}
}
class Proxy {
constructor(realSubject) {
this.realSubject = realSubject;
}
request() {
if (this.checkAccess()) {
return this.realSubject.request();
}
return 'Proxy denied';
}
checkAccess() {
return true;
}
}应用场景
- 延迟加载
- 访问控制
- 日志记录
模式对比
| 模式 | 核心思想 | 关系 |
|---|---|---|
| 适配器 | 接口转换 | 已有 → 需要 |
| 桥接 | 分离变化 | 多个维度 |
| 组合 | 整体-部分 | 树形结构 |
| 装饰器 | 动态添加 | 包装 |
| 外观 | 简化接口 | 封装子系统 |
| 亨元 | 共享 | 细粒度共享 |
| 代理 | 控制访问 | 间接访问 |
