之前说了aop有四种实现方式,其中最不推荐的就是动态代理实现,因为它浸入代码强,不利于维护,但是也要知道,因为其他实现方式里面的原理也用到了动态代理,只是封装更好 用起来更简单
比如一个app要收藏或者关注必须是用户已经登陆情况,除非是app可以是游客的情况下登陆也能收藏或者关注,这是另话了,
我定义了一个用户行为接口 有收藏和关注方法
public interface IUser {
void save();
void collect(int id);
void attention(int id);
}
上面是代理的实现
public class MainActivity extends AppCompatActivity implements IUser {
private IUser iUser;
private Button btnCollect,btnAttention;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iUser = (IUser) Proxy.newProxyInstance(IUser.class.getClassLoader(),
new Class[]{IUser.class}, new UserHandler(this));
btnCollect = findViewById(R.id.activity_main_btn_collect);
btnAttention = findViewById(R.id.activity_main_btn_attention);
btnCollect.s