Vì vậy, tôi có một chương trình mà bạn có thể đăng nhập và thêm/xóa bạn bè đến và đi từ friends
ArrayList. Ngoài ra tôi có thể thích một thứ nhất định và thứ đó sẽ được lưu trữ trong danh sách mảng likes
. Tôi được yêu cầu thực hiện các tùy chọn hoàn tác và làm lại cho bất kỳ hành động nào tôi thực hiện.Sử dụng mẫu Command cho Undo và Redo trong ArrayLists
Vì vậy, tôi muốn thêm apple làm bạn. Sau đó khi tôi chọn tùy chọn hoàn tác, tôi có thể hoàn tác hành động để táo sẽ không là bạn của tôi. Làm thế nào tôi có thể tiếp cận này với một Command Pattern
khi đầu vào là bất cứ tên hoặc chữ I đầu vào để lưu trữ vào friends
ArrayList?
tôi đã làm một số nghiên cứu và thấy rằng việc sử dụng một mô hình lệnh có thể là lựa chọn tốt nhất của tôi kể từ khi điều này đã được thực hiện dưới sự Lớp Facebook tôi đã có. Tôi giả sử tôi sẽ phải sử dụng hai ngăn xếp khác nhau, nhưng tôi bị mất một chút trong chủ đề.
Tôi quyết định thêm các phần của những gì tôi có để tôi có thể được trợ giúp thêm một chút về những gì tôi cần làm và những gì chương trình của tôi làm.
Trong chương trình lái xe
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();
}
Trong Facebook Lớp
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();
}
có thể trùng lặp của [hoàn tác và thực hiện lại trong Java] (http://stackoverflow.com/questions/7374545/the-undo-and-redo-implementation-in-java) –
iirc, bạn chỉ cần một ngăn xếp . –