通过动态代理实现 AOP架构
实现业务: 每次操作数据之前 进行 对数据保存, 把保存的方法 单独抽取出来
创建 接口类
public interface DbHelper { void insert(); void detele(); void update(); void select(); void save(); }
dbHelper = (DbHelper) Proxy.newProxyInstance(DbHelper.class.getClassLoader(), new Class[]{DbHelper.class}, new DbHandler(this));
class DbHandler implements InvocationHandler { private MainActivity con; public DbHandler(MainActivity mainActivity) { this.con = mainActivity; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if(con!=null){ Log.e("--","添加数据之前"); Log.e("--","保存数据"); save(); Log.e("--","数据保存完毕"); Log.e("--","等待执行"); } return method.invoke(con,args); } }