MicroStrategy Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: In-Person
May be like this ,
interface Action {
execute() ;
Action getInverseAction() ;
}
class DrawLineAction implements Action {
DrawLineAction (Point p1, Point p2) {
}
public execute() {
// Logic to draw a line
}
public getInverseAction() {
return new EraseLineAction(p1,p2) ;
}
}
class EraseLineAction implements Action {
EraseLineAction (Point p1, Point p2) {
}
public execute() {
//Logic to erase a line
}
public Action getInverseAction() {
return new DrawLineAction(p1,p2);
}
-------------------------
Once you have this setup , you can maintain a linked list . And have the index as current Action . For every line you draw you will add the Action to the list
List<Action> m_actions = new ...
Action m_currentAction ;
drawObject(...) {
for each point {
Point p1 , Point p2 ;
Action act = new DrawLineActin(p1,p2) ;
m_actions.append(act);
act.execute() ;
m_currentAction = act ;
}
}
undo() {
Action act = m_currentAction.getInverseAction();
action.execute();
m_currentAction = (Previous Action in m_actions() )
}
redo() {
action = (next Action of m_currentAction in m_actions)
action.execute() ;
m_currentAction = action ;
}
use stacks
- bbarodia January 12, 2013