所以我有一个程序,你可以登录并将朋友添加到和从
所以我想把apple添加为一个朋友。之后当我选择撤消选项时,我可以撤消那个动作,这样apple就不会是我的朋友了。如果输入是我输入的任何名称或单词来存储到
我进行了一些研究,发现使用命令模式可能是最好的选择,因为这必须在我已经拥有的Facebook Class下完成。我假设我将不得不使用两个不同的堆栈,但我在这个主题上有点迷失了。
我决定添加一些我所拥有的部分,以便我可以更好地了解我需要做什么以及我的程序的功能。
在驱动程序中
在Facebook班级中
friends
数组列表中删除。此外,我可以喜欢某个东西,并将该物品存储到likes
数组列表中。我被要求为我所做的任何操作制作撤消和重做选项。所以我想把apple添加为一个朋友。之后当我选择撤消选项时,我可以撤消那个动作,这样apple就不会是我的朋友了。如果输入是我输入的任何名称或单词来存储到
friends
数组列表中,我该如何使用Command Pattern
来处理?我进行了一些研究,发现使用命令模式可能是最好的选择,因为这必须在我已经拥有的Facebook Class下完成。我假设我将不得不使用两个不同的堆栈,但我在这个主题上有点迷失了。
我决定添加一些我所拥有的部分,以便我可以更好地了解我需要做什么以及我的程序的功能。
在驱动程序中
Facebook facebook1 = new Facebook();
if (userInput == 6)
{
System.out.println("Login");
String operand1 = getOperand("What is the Username? ");
String operand2 = getOperand("What is the Password? ");
System.out.println("Enter a friend to be added. ");
String operand3 = getOperand("What is the Username? ");
facebook1.friend(operand3);
}
if (userInput == 7)
{
System.out.println("Login");
String operand1 = getOperand("What is the Username? ");
String operand2 = getOperand("What is the Password? ");
System.out.println("Enter a friend to be removed. ");
String operand3 = getOperand("What is the Username? ");
facebook1.defriend(operand3);
}
if (userInput == 12)
{
System.out.println("Login");
String operand1 = getOperand("What is the Password? ");
facebook1.undo();
}
if (userInput == 13)
{
System.out.println("Login");
String operand1 = getOperand("What is the Password? ");
facebook1.redo();
}
在Facebook班级中
ArrayList<FacebookUser> recommendedFriends = new ArrayList<FacebookUser>();
void friend(String newFriend)
{
boolean positiveChecker = false;
for (int i = 0; i < recommendedFriends.size(); i++)
{
if (recommendedFriends.get(i).toString().equalsIgnoreCase(newFriend))
{
System.out.println("Error: This friend already exists.");
positiveChecker = true;
}
}
if (positiveChecker == false)
{
FacebookUser friend = new FacebookUser(newFriend, newFriend );
recommendedFriends.add(friend);
System.out.println(friend + " is now your friend.");
}
positiveChecker = false;
}
void defriend(String formerFriend)
{
boolean positiveChecker = false;
for (int i = 0; i < recommendedFriends.size(); i++)
{
if (recommendedFriends.get(i).toString().equalsIgnoreCase(formerFriend))
{
recommendedFriends.remove(i);
System.out.println(formerFriend + " has been removed from your friends list.");
positiveChecker = true;
}
if (recommendedFriends.size() == (i + 1) && recommendedFriends.get(i).toString() != formerFriend
&& positiveChecker == false)
{
System.out.println("Error: There is no friend with this username.");
}
}
positiveChecker = false;
}
public interface Command
{
public void undo();
public void redo();
}