结构型模式
2026/1/15大约 3 分钟
结构型模式
结构型模式关注类和对象的组合,形成更大的结构。
代理模式
为其他对象提供一种代理以控制对这个对象的访问。
静态代理
// 接口
public interface UserService {
void save(User user);
}
// 目标类
public class UserServiceImpl implements UserService {
@Override
public void save(User user) {
System.out.println("保存用户: " + user);
}
}
// 代理类
public class UserServiceProxy implements UserService {
private UserService target;
public UserServiceProxy(UserService target) {
this.target = target;
}
@Override
public void save(User user) {
System.out.println("开始事务");
target.save(user);
System.out.println("提交事务");
}
}JDK 动态代理
public class JdkProxyFactory {
public static Object createProxy(Object target) {
return Proxy.newProxyInstance(
target.getClass().getClassLoader(),
target.getClass().getInterfaces(),
(proxy, method, args) -> {
System.out.println("前置处理");
Object result = method.invoke(target, args);
System.out.println("后置处理");
return result;
}
);
}
}
// 使用
UserService proxy = (UserService) JdkProxyFactory.createProxy(new UserServiceImpl());
proxy.save(user);CGLIB 动态代理
public class CglibProxyFactory {
public static Object createProxy(Object target) {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(target.getClass());
enhancer.setCallback((MethodInterceptor) (obj, method, args, proxy) -> {
System.out.println("前置处理");
Object result = proxy.invokeSuper(obj, args);
System.out.println("后置处理");
return result;
});
return enhancer.create();
}
}适配器模式
将一个类的接口转换成客户期望的另一个接口。
类适配器
// 目标接口
public interface Target {
void request();
}
// 被适配类
public class Adaptee {
public void specificRequest() {
System.out.println("特殊请求");
}
}
// 适配器(继承)
public class ClassAdapter extends Adaptee implements Target {
@Override
public void request() {
specificRequest();
}
}对象适配器
// 适配器(组合)
public class ObjectAdapter implements Target {
private Adaptee adaptee;
public ObjectAdapter(Adaptee adaptee) {
this.adaptee = adaptee;
}
@Override
public void request() {
adaptee.specificRequest();
}
}实际应用
// InputStreamReader 是适配器
// 将 InputStream 适配为 Reader
Reader reader = new InputStreamReader(inputStream, "UTF-8");装饰器模式
动态地给对象添加额外的职责。
// 组件接口
public interface Coffee {
double cost();
String description();
}
// 具体组件
public class SimpleCoffee implements Coffee {
@Override
public double cost() {
return 10.0;
}
@Override
public String description() {
return "简单咖啡";
}
}
// 装饰器基类
public abstract class CoffeeDecorator implements Coffee {
protected Coffee coffee;
public CoffeeDecorator(Coffee coffee) {
this.coffee = coffee;
}
}
// 具体装饰器
public class MilkDecorator extends CoffeeDecorator {
public MilkDecorator(Coffee coffee) {
super(coffee);
}
@Override
public double cost() {
return coffee.cost() + 2.0;
}
@Override
public String description() {
return coffee.description() + " + 牛奶";
}
}
public class SugarDecorator extends CoffeeDecorator {
public SugarDecorator(Coffee coffee) {
super(coffee);
}
@Override
public double cost() {
return coffee.cost() + 1.0;
}
@Override
public String description() {
return coffee.description() + " + 糖";
}
}
// 使用
Coffee coffee = new SimpleCoffee();
coffee = new MilkDecorator(coffee);
coffee = new SugarDecorator(coffee);
System.out.println(coffee.description()); // 简单咖啡 + 牛奶 + 糖
System.out.println(coffee.cost()); // 13.0实际应用
// Java IO 中的装饰器
InputStream is = new FileInputStream("file.txt");
is = new BufferedInputStream(is);
is = new DataInputStream(is);外观模式
为子系统提供一个统一的接口。
// 子系统
public class CPU {
public void start() { System.out.println("CPU 启动"); }
public void shutdown() { System.out.println("CPU 关闭"); }
}
public class Memory {
public void load() { System.out.println("内存加载"); }
public void clear() { System.out.println("内存清理"); }
}
public class Disk {
public void read() { System.out.println("磁盘读取"); }
public void write() { System.out.println("磁盘写入"); }
}
// 外观类
public class ComputerFacade {
private CPU cpu;
private Memory memory;
private Disk disk;
public ComputerFacade() {
this.cpu = new CPU();
this.memory = new Memory();
this.disk = new Disk();
}
public void start() {
cpu.start();
memory.load();
disk.read();
System.out.println("电脑启动完成");
}
public void shutdown() {
disk.write();
memory.clear();
cpu.shutdown();
System.out.println("电脑关闭完成");
}
}
// 使用
ComputerFacade computer = new ComputerFacade();
computer.start();
computer.shutdown();组合模式
将对象组合成树形结构,使单个对象和组合对象具有一致性。
// 组件接口
public interface Component {
void operation();
}
// 叶子节点
public class Leaf implements Component {
private String name;
public Leaf(String name) {
this.name = name;
}
@Override
public void operation() {
System.out.println("叶子: " + name);
}
}
// 组合节点
public class Composite implements Component {
private String name;
private List<Component> children = new ArrayList<>();
public Composite(String name) {
this.name = name;
}
public void add(Component component) {
children.add(component);
}
@Override
public void operation() {
System.out.println("组合: " + name);
for (Component child : children) {
child.operation();
}
}
}
// 使用
Composite root = new Composite("根");
root.add(new Leaf("叶子1"));
Composite branch = new Composite("分支");
branch.add(new Leaf("叶子2"));
branch.add(new Leaf("叶子3"));
root.add(branch);
root.operation();桥接模式
将抽象部分与实现部分分离,使它们可以独立变化。
// 实现接口
public interface Color {
void applyColor();
}
public class Red implements Color {
@Override
public void applyColor() {
System.out.println("红色");
}
}
public class Blue implements Color {
@Override
public void applyColor() {
System.out.println("蓝色");
}
}
// 抽象类
public abstract class Shape {
protected Color color;
public Shape(Color color) {
this.color = color;
}
public abstract void draw();
}
public class Circle extends Shape {
public Circle(Color color) {
super(color);
}
@Override
public void draw() {
System.out.print("圆形 - ");
color.applyColor();
}
}
// 使用
Shape redCircle = new Circle(new Red());
redCircle.draw(); // 圆形 - 红色