Undo,Redoã®å®è£ ã£ã¦ä½ååããã£ã¦ãæ°ããã
undo,redoã®å®è£ ã£ã¦ä½ååããã£ã¦ãæ°ããããæ¯ååããã¿ã¼ã³ã ãundo,redoãç»å ´ãããããªç·¨éã½ããã¯å¤§ä½åããã¿ã¼ã³ã«è½ã¨ããããã¬ã¼ã ã¯ã¼ã¯ãä½ã£ããããã°ã«ããããå 容ãæ¸ããããé¢åããããéè¦ãããã°é¢åã§ãæ¸ããã ãã©ãªã
http://twitter.com/youpychan/status/994486992
ã¨ããçºè¨ããããä½äººãåå¿ãé ããã®ã§æ¸ãã¦ã¿ããã¨ã«ããã
éè¦ããããªãæ¸ãããundo,redoã ããããªãã¦ã°ã©ãã£ã«ã«ãªç·¨éã½ããå ¨è¬ã®è©±ããã¤ãã¾ã¨ãããã¨æã£ã¦ããã®ã§ãã¡ããã¨ã·ãªã¼ãºã§æ¸ãã¦ã¿ãããã¨ããã
http://twitter.com/youpychan/status/994636764
æ¸ããã¨æãã
ã¾ãUndo,Redoã«ã¤ãã¦ã
Unod,Redoã£ã¦ã¿ãªããã©ããã風ã«å®è£
ãã¦ããã§ããããï¼
ç§ã¯ã³ãã³ããã¿ã¼ã³ã使ã£ã¦ã³ãã³ãã®ä¸ã«ãUndoã¨Redoã®æä½ã«å¿
è¦ãªæ
å ±ãä¿æããã¨ããæ¹æ³ãã¨ã£ã¦ãã¾ããã
å®éã«Undo,Redoå¦çãããã®ã¯ã³ãã³ããåãåãå´ã§å¦çãè¡ãã¾ããï¼ä½ååã®ãã¡ã®åæã®ããã¯å²ã¨ãã®ãã¿ã¼ã³ï¼
ã³ã¼ãã使ã£ã¦èª¬æãã¾ãããã
ãããã¼ã¿ã¢ãã«(DataModelã¯ã©ã¹ã¨ãã¾ãï¼ããã£ã¦setText(string text)ã¡ã½ããã«ãã£ã¦Textããããã£ãæ´æ°ããã¨ããä¾ãåãä¸ãã¾ãã
ã¾ããã³ãã³ãã¯ICommandã¨ããã¤ã³ã¿ãã§ã¼ã¹ãæã¡ãã³ãã³ãã®å®è¡ã¯ICommandã¤ã³ã¿ãã§ã¼ã¹ãéãã¦çµ±ä¸çã«è¡ãã¾ãã
public interface ICommand { public CommandType getCommandType(); public DataModel getDataModel(); }
ã³ãã³ãã®ç¨®é¡ãèå¥ããããã«CommandTypeã¨ããåæåãç¨æãã¦ãã³ãã³ãããããè¿ãã¨ããã®ã¨ãæä½ã®å¯¾è±¡ã¨ãªãDataModelã¯ã©ã¹ã®ãªãã¸ã§ã¯ããè¿ãã¨ããã¤ã³ã¿ãã§ã¼ã¹ãæã¡ã¾ãã
Textãæ´æ°ããã¨ããUPDATEæä½ãã³ãã³ãã¨ãã¦å®è£
ãã¾ãã
public class UpdateCommand implements ICommand { private DataModel mModel; private String mPrevText; private String mNewText; public UpdateCommand(DataModel model, String prevText, String newText) { mModel = model; mPrevText = prevText; mNewText = newText; } public CommandType getCommandType() { return CommandType.UPDATE; } public DataModel getDataModel() { return mModel; } public String getPrevText() { return mPrevText; } public String getNewText() { return mNewText; } }
ã³ãã³ãã®å®è¡é¨ã¯ãããªæãã§ãã
public class CommandInvoker { public void undo(ICommand command) { switch (command.getCommandType()) { case CommandType.UPDATE: UpdateCommand updateCmd = (UpdateCommand)command; updateCmd.getDataModel().setText(updateCmd.getPrevText()); break; ... } } }
undo()ã¡ã½ããã ããæ²è¼ãã¾ãããredo()ãæåã®invoke()ãåæ§ãªå®è£ ã«ãªããã¨ã¯æ³åãã¤ãã¨ãããã¾ãã
twitterã§ã¯ããã¤ãåããã¿ã¼ã³ã¨çºè¨ãã¾ããããå®ã¯ä½ååãå®è£
ãã¦ãããã¡ã«å®è£
ãå¤ãã£ã¦ãã¾ãã
ããã§ã¯ãã¹ã¤ããæã§å¦çãåå²ãã¦ãã£ã¹ããã¦ã³ãã³ããªãã¸ã§ã¯ããå©ç¨ãã¦ãã¾ãããªãã¸ã§ã¯ãæåã«æ
£ã親ããã§ããã¿ãªãããªããããæ°ã¥ãã§ãããããããã¯ããªã¢ã¼ãã£ãºã ã使ã£ã¦è§£æ±ºãã¹ãåé¡ã§ãã
ããªã¢ã¼ãã£ãºã ã使ã£ãå ´åICommandã¯ä»¥ä¸ã®ããã«ãªãã¾ãã
public interface ICommand { public void invoke(); public void undo(); public void redo(); }
ããã¦UpdateCommandã®å®è£
ã¯ä»¥ä¸ã®éãã§ãã
public class UpdateCommand implements ICommand { private DataModel mModel; private String mPrevText; private String mNewText; public UpdateCommand(DataModel model, String newText) { mModel = model; mPrevText = prevText; mNewText = newText; } public void invoke() { mPrevText = mModel.getText(); mModel.setText(mNewText); } public void undo() { mModel.setText(mPrevText); } public void redo() { mModel.setText(mNewText); } }
ã³ãã³ããå¦çããå´ã¯ICommandã¤ã³ã¿ãã§ã¼ã¹ã®invoke(),undo(),redo()ãå¼ã³åºãã ãã§ãã
C#ï¼ã¨ãããã¯ãã¼ã¸ã£ï¼ã使ãã¨ããã«ç°¡åã«ãªãã¾ãã
ãã¯ãICommandã¤ã³ã¿ãã§ã¼ã¹ã¯å¿ è¦ããã¾ããã以ä¸ã®ããã«ãããªã²ã¼ãã使ç¨ãã¦Commandã¯ã©ã¹ãå®ç¾©ãã¾ãã
public class Command { Action Invoke; Action Undo; Action Redo; }
ã³ãã³ãã®çæã¯ä»¥ä¸ã®éãã§ãã
public class CommandInvoker { public ICommand createUpdateCommand(DataModel model, string newText) { string prevText = null; Command cmd = new Command(); cmd.Invoke = () => { prevText = model.Text; model.Text = newText; }; cmd.Updo = () => { model.Text = prevText; }; cmd.Redo = () => { model.Text = newText; }; return cmd; } }
Javaã§ãç¡åã¯ã©ã¹ã使ãã°åæ§ã®ãã¨ãå¯è½ã§ãããããç¡åã¯ã©ã¹ã®ãªãã¸ã§ã¯ãã«å¤ã渡ãããã«finalæå®ããªããã°ãªããªãã£ããããã®ã§ãå§ãåºæ¥ãªãæ¹æ³ã§ãã
ã¯ãã¼ã¸ã£ã®ä»çµã¿ã®ä¾¿å©ãã«æ¹ãã¦æå¿ããä¸ä¾ã§ããã
ãã¦ãããã§ã¯Undo,Redoãå®è£ ãããã¨ã«ã¯ãªãã¾ããã®ã§ã次åã¯ã³ãã³ããå®è¡ããé¨åã«ã¤ãã¦èª¬æãããã¨æãã¾ãã