前言:
这个案例是学习设计模式的期末作业,老师很大气的只让我们写一个小案例就行,所以就写了个小小案例,注意:案例是只有逻辑代码,没有具体执行部署。
参考:设计模式(五)观察者模式-CSDN博客 || 动态代理实现动态插入日志-CSDN博客
说明:采用的被观察者(后面称为通知者)命名为Examine类:页面发User数据到controller,controlloer调用service类,在注册函数register类中经过审核后就调用examine的执行函数notify进行通知广播所有被管理的user(也就是观察者)进行状态的更改(user继承的接口有updata这个类来进行state属性的更改)
采用的动态代理进行调用service函数的之前和之后打印一句话(也就是日志),
1controller类:
public class Controller {
public void logout(User user){
//实例化代理函数
UserProxy userProxy = new UserProxy();
//根据代理函数产生一个service代理类,这个代理类已经被logger类切入了织点
UserService userServiceProxy = (UserService) userProxy.getProxy(new UserService());
//按理来说user1、user2、user3应该是页面传来的,但是案例就自己创建了
User user1 = new User(1,10010,123);
User user2 = new User(2,10011,123);
User user3 = new User(3,10110,123);
userServiceProxy.logout(user1);
userServiceProxy.logout(user2);
userServiceProxy.logout(user3);
}
}
2.service类
public interface Service {
void login(User user);
void register(User user);
void logout(User user);
}
public class UserService implements Service{
public void login(User user){
if(user == null){
}
if (user.getState() == 1){
System.out.println("登录成功");
}
}
public void register(User user){
ExamineImple examineImple = new ExamineImple();
/*
* 判断User是否有效的业务代码.......
* */
examineImple.notify(1);
}
public void logout(User user){
ExamineImple examineImple = new ExamineImple();
/*
* 绝定User是否要删除的业务代码.......
* */
examineImple.notify(0);
}
}
3通知者examin
public interface Examine { public void attach(Observer observer); public void detach(Observer observer); public void notify(int notLogout); } public class ExamineImple implements Examine { //储存待审核的用户 private List<Observer> logoutUserList = new ArrayList<Observer>(); @Override public void attach(Observer observer) { logoutUserList.add(observer); } @Override public void detach(Observer observer) { logoutUserList.remove(observer); } @Override public void notify(int notLogout) { for (Observer observer : logoutUserList){ observer.update(1); } } }
4.代理类
public class UserProxy { public static Object getProxy(Object o){ return Proxy.newProxyInstance(o.getClass().getClassLoader(), o.getClass().getInterfaces(), new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Logger logger = new Logger(); logger.before(); Object result = method.invoke(o,args); logger.aftor(); return result; } } ); } }
logger日志类
public class Logger { public void before(){ System.out.println("------调用service------"); } public void aftor(){ System.out.println("-----调用结束-----"); } }
5含有更新函数的实体类
public interface Observer {
public void update(int notLogout);
}
public class User implements Observer{
private int id;
private int name;
private int pwd;
//0是注销,1是正常激活状态
private int state;
@Override
public String toString() {
return "User{" +
"id=" + id +
", name=" + name +
", pwd=" + pwd +
", state=" + state +
'}';
}
public User(int id, int name, int pwd) {
this.id = id;
this.name = name;
this.pwd = pwd;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getName() {
return name;
}
public void setName(int name) {
this.name = name;
}
public int getPwd() {
return pwd;
}
public void setPwd(int pwd) {
this.pwd = pwd;
}
public int getState() {
return state;
}
public User() {
}
@Override
public void update(int notLogout) {
if (notLogout == 1){
this.state = 1;
} else {
this.state = 0;
}
}
}