面向切面编程(AOP)与 Spring AOP 详解
1. 代码问题:散射与缠结
在软件开发中,代码散射和代码缠结是常见的问题,它们会导致系统出现重复代码、功能不清晰以及模块间强耦合等问题。
1.1 代码散射
代码散射指的是功能分散在多个模块中实现的情况,主要有两种类型:
- 重复代码块 :相同的代码出现在不同的模块中。例如, Info
接口被 ScatteringA
和 ScatteringB
两个不同的类以相同的方式实现,这是一种无用的代码重复。
public interface Info {
public String getName();
public Date getCreationDate();
}
public class ScatteringA implements Info{
public ScatteringA(String name, String author){
creation = new Date();
this.name = name;
this.autor = author;
}
public ScatteringA(Date creation, String name, String author){
this.creation = creation;
this.name = name;
th