📄 context.java
字号:
package com.zdh.sms.system;
import java.util.HashSet;
import java.util.Set;
import com.zdh.sms.ILogoffListener;
import com.zdh.sms.model.IUser;
public class Context {
// 单例模式
private static Context context = new Context();
private Context() {
}
public static Context getInstance() {
return context;
}
private IUser currentUser = null;
public IUser getCurrentUser() {
return currentUser;
}
public void setCurrentUser(IUser currentUser) {
this.currentUser = currentUser;
}
// 判断当前系统是否有用户登录
public boolean isLogon() {
return currentUser != null;
}
// 监听器的注册表
private Set<ILogoffListener> logonListeners = new HashSet<ILogoffListener>();
// 注册监听器
public void addLogonListener(ILogoffListener listener) {
logonListeners.add(listener);
}
// 撤销监听器
public void removeLogonListener(ILogoffListener listener) {
logonListeners.remove(listener);
}
// 触发所有监听器中的登录事件处理方法
public void fireLogonEvent() {
for (ILogoffListener listener : new HashSet<ILogoffListener>(
logonListeners))
listener.logon();
}
// 触发所有监听器中的退出事件处理方法
public void fireLogoffEvent() {
for (ILogoffListener listener : new HashSet<ILogoffListener>(
logonListeners))
listener.logoff();
currentUser = null;// 当前登录用户设为空
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -