java - I'm currently working on a text editor and need some help making a "undo change" function -


as stated above, i'm working on text-editor, fun side project.

i want include revert/redo changes function now, can't quite figure out how so.

my current thoughts it:

  1. i need list of bufferedreaders.
  2. on document load, create bufferedreader anyways paste docs text textarea. state first item in list
  3. when user edits document, everytime key press detected, start timer in thread. if timer runs out without additional keys coming in ( lets on 300ms timeout), generate new bufferedreader , paste list.
  4. when user demands revert changes, pick recent list entry, , overwrite textarea it. (this think problems, considering files could huge, , take time). if wants revert again, step 1 index in list. if wants redo, step 1 forward.

however, i'm not quite aware if best way go, considering huge amount of data can come easily. however, can't quite figure out idea right how realize this.

are there tips can give me better way of doing this?

i suggest read design patterns command pattern. here's info:

the command pattern known behavioural pattern, it's used manage algorithms, relationships , responsibilities between objects. definition of command provided in original gang of 4 book on design patterns states:

encapsulate request object, thereby letting parameterize clients different requests, queue or log requests, , support undoable operations

so mean in class diagram?

command pattern

command declares interface commands, providing simple execute() method asks receiver of command carry out operation. receiver has knowledge of carry out request. invoker holds command , can command execute request calling execute method. client creates concretecommands , sets receiver command. concretecommand defines binding between action , receiver. when invoker calls execute concretecommand run 1 or more actions on receiver.

the following sequence diagram shows relationship in clearer way: sequence diagram

when use pattern? command pattern useful when:

  • a history of requests needed
  • you need callback functionality
  • requests need handled @ variant times or in variant orders
  • the invoker should decoupled object handling invocation.

you'll see command being used lot when need have multiple undo operations, stack of executed commands maintained. implement undo, need last command in stack , execute it's undo() method.

you'll find command useful wizards, progress bars, gui buttons , menu actions, , other transactional behaviour.

so how work in java? let's use remote control example. our remote center of home automation , can control everything. we'll use light example, can switch on or off, add many more commands.

first we'll create our command interface:

//command public interface command {     public void execute(); } 

now let's create 2 concrete commands. 1 turn on lights, turns off lights:

//concrete command public class lightoncommand implementscommand { //reference light light light;  public lightoncommand(light light) {     this.light = light; }  public void execute() {     light.switchon(); }  }    //concrete command public class lightoffcommand implementscommand { //reference light light light;  public lightoffcommand(light light) {     this.light = light; }  public void execute() {     light.switchoff(); }  } 

light our receiver class, let's set now:

    //receiver    public class light    {    private boolean on;     public void switchon()    {       on = true;    }     public void switchoff()    {       on = false;    }     } 

our invoker in case remote control.

//invoker public class remotecontrol { private command command;  public void setcommand(command command) {     this.command = command; }   public void pressbutton() {     command.execute(); }  } 

finally we'll set client use invoker

//client public class client { public static void main(string[] args) {     remotecontrol control = new remotecontrol();      light light = new light();      command lightson = new lightsoncommand(light);     command lightsoff = new lightsoffcommand(light);      //switch on     control.setcommand(lightson);     control.pressbutton();      //switch off     control.setcommand(lightsoff);     control.pressbutton();  }  } 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -