基本上我的想法是我需要某种类型的Java会话/ cookie模仿。我必须为大学项目开发某种博客程序。它不一定是Web,我的意思是我不需要servlet和其他Java EE的东西。整个界面必须在控制台中。
所以这就是问题,我已经开发了一个Menu类,我在其中为每个要添加的菜单选项添加一个MenuItem对象,在我浏览菜单后我想登录我的帐户,我需要类似会话的东西,否则我无法隐藏"登录"和"注册"选项,并且无法决定是否显示"退出"选项。由于Menu类只被实例化一次,因此无法在移动中更新它(或者至少我认为是这样,仍在学习Java)。
如果有人可以给我一个建议或想法,那对我有很大帮助。
以下是菜单类:
package my.app;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import my.toolbox.InputHandler;
public class Menu extends MenuItem {
protected SessionImitator SESSION = SessionImitator.getInstance();
protected String title;
private static final String OUT_OF_RANGE = "Please select an option within the range";
private static final MenuItem SEPARATOR = new MenuItem("---------------------");
private static final MenuItem BACK = new MenuItem("Go Back");
private static final MenuItem EXIT = new MenuItem("Exit", new Runnable() {
public void run() {
System.exit(0);
}
});
List<MenuItem> items;
public Menu(String title, MenuItem ... items) {
this(title, false, true, items);
}
public Menu(String title, boolean addBack, boolean addExit, MenuItem ... items) {
super(title);
setExecutable(this);
init(addBack, addExit, items);
}
private void init(boolean addBack, boolean addExit, MenuItem ... items) {
this.items = new ArrayList<MenuItem>(Arrays.asList());
for (MenuItem item : items) {
if (item.isSessionDependent()) {
if (!item.getSessionAction() && SESSION.isSet()) {
continue;
}
}
this.items.add(item);
}
if (SESSION.isSet()) {
System.out.println("THIS PART DOES NOT WORK SINCE THE OBJECT IT's NOT UPDATED AFTER THE SESSION IS SET!");
}
if (addBack) this.items.add(BACK);
if (addExit) this.items.add(EXIT);
}
private void display() {
int option = 0;
System.out.println(SEPARATOR.getTitle());
System.out.println(getTitle() + ":");
System.out.println(SEPARATOR.getTitle());
for (MenuItem item : items) {
System.out.println((option++) + ": " + item.getTitle());
}
System.out.println(SEPARATOR.getTitle());
System.out.println("select an option: ");
System.out.flush();
}
private MenuItem prompt() {
display();
int option = InputHandler.readInt();
if (option >= 0 && option < items.size()) {
return items.get(option);
}
System.out.println(OUT_OF_RANGE);
return null;
}
public void run() {
try {
for (MenuItem item = prompt(); item.isExecutable(); item = prompt()) {
item.run();
}
} catch (Throwable t) {
t.printStackTrace(System.out);
}
}
}
MenuItem类:
package my.app;
public class MenuItem implements Runnable {
private String title;
private Runnable executable;
private boolean sessionDependent;
private boolean sessionAction;
protected MenuItem(String title) {
this(title, null, false, false);
}
protected MenuItem(String title, boolean sessionDependent, boolean sessionAction) {
this(title, null, sessionDependent, sessionAction);
}
protected MenuItem(String title, Runnable executable) {
this(title, executable, false, false);
}
public MenuItem(String title, Runnable executable, boolean sessionDependent, boolean sessionAction) {
this.title = title;
this.executable = executable;
this.sessionDependent = sessionDependent;
this.sessionAction = sessionAction;
}
public void run() {
try {
executable.run();
} catch (Throwable t) {
t.printStackTrace(System.err);
}
}
public boolean isExecutable() {
return executable != null;
}
protected void setExecutable(Runnable executable) {
this.executable = executable;
}
public String getTitle() {
return title;
}
public boolean isSessionDependent() {
return sessionDependent;
}
public boolean getSessionAction() {
return sessionAction;
}
}
和SessionImitator类:
package my.app;
public class SessionImitator {
private static SessionImitator instance = null;
protected int userId;
protected boolean locked = false;
protected SessionImitator() {
}
public static SessionImitator getInstance() {
if (instance == null) {
instance = new SessionImitator();
}
return instance;
}
public int getUserId() {
return userId;
}
public void setUserId(String id) {
if (!locked) {
this.userId = Integer.parseInt(id);
}
}
public boolean isSet() {
return locked;
}
}
答案 0 :(得分:0)
你所描述的似乎是想要保存一个状态&#34;该计划。但特别是菜单的状态。
一个简单的实现是创建菜单serializable
,然后将序列化的菜单对象保存到文件中,并在登录时重新加载它(如果文件存在的话)。
以上示例适用于计算机上的用户。如果您希望跨网络等持久化。您可能希望将序列化对象写入db表中的blob,然后从那里加载它。
答案 1 :(得分:0)
如果您的程序是独立的,那么您需要的是容器来保存应用程序的当前状态。有不同的方法可以获得:
State state = State.getInstance()
等代码访问状态 - 这就是您SessionImitator
目前的状态。一个简单的对象注入在所有其他需要它的类中。构建它的一种方法是创建一个类持有状态的实例并将其传递给其他类(例如在构造函数中):
State state = new State();
...
Menu menu = new Menu(state);