Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6087871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:55:11+00:00 2026-05-23T11:55:11+00:00

I am starting to implementing the command pattern in the hope to get a

  • 0

I am starting to implementing the command pattern in the hope to get a useful solution to my problem of providing an undo operation. Now I am facing a certain issue:

Implementing undo when operations are involved are rather easy: when I’ve added 5 to a number then I subtract 5. When I’ve added an object to a list, then I remove it, and so on. But what if I have a total state and not something like a list?

An example: I model information about a thread in a class:

public class ThreadInfo implements Comparable<ThreadInfo> {

    final int id;
    String name;
    int priority;
    String state;
    int waitCount;
    // ...
}

Certain information does not change, for instance the id. Undoing the waitCount is easy as described above, just subtract. But what about priority or state? It is not clear how to undo these information.

The only idea I came up with: when initializing the command object, preserve the old state in it’s object: by passing the relevant data into the constructor:

public MyCommand(int priority, String state) {
   previousPriority = priority;
   previousState = state;
}

Or would it be better to let ThreadInfo have a list of states and priorities with being the first elements the current?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T11:55:11+00:00Added an answer on May 23, 2026 at 11:55 am

    Just hold old state in command. for example

     class ChangeFoo {
        ChangeFoo (Bar bar, foo newFoo){}
        void execute(){
            oldFoo = bar.getFoo();
            bar.setFoo(newFoo);
        }
        void undo(){
            bar.setFoo(oldFoo);
        }
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm implementing the TTThumbsViewController from the Three20 project and things are finally starting to
its when i starting trying implementing this that i got quite stuck. some business
I'm just starting to mess with bindings. I've started implementing a preference dialog, binding
So by now I'm getting the point that we should all be implementing our
I am implementing some satellite image filters, starting with one known as the Enhanced
I'm in the process of implementing XML digital signatures. I'm starting with little steps,
I'm interested in implementing a Forth system, just so I can get some experience
I've noticed now after implementing TinyMCE on my site that when I'm editing it
I can't get a spawned thread to stop: I'm implementing the vibration-part of the
We're starting to experiment with implementing our backend services using CDI. The scenario is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.