What is the Action Design Pattern?

天大地大妈咪最大 提交于 2019-12-10 12:48:59

问题


What is the Action Design Pattern, I haven't heard of it before? I am suspecting it is the same as the Command Design pattern [wikipedia] but I can't find any resources on it.


回答1:


You're right, action pattern == command pattern. You hear it called the action pattern more often in GUI design, in the form "on some button pressed, perform this action". In the code the button would be wired up with an action object of some kind.




回答2:


I'm reading "The Action/Executor Pattern" at MSDN right now, and I have to disagree with the premise that the Command and Action/Executor patterns are the same.

From the description of the Command Pattern at SourceMaking.com:

  • Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.
  • Promote "invocation of a method on an object" to full object status
  • An object-oriented callback

From the MSDN article about the Action/Executor pattern:

The Action/Executor pattern identifies a strategy for mapping use cases to code, allowing better visibility and agility. Also, it addresses the issues of contaminating entities and skipping over proper use of transactions.

The difference appears to be that an "action" encapsulates one or more steps, that when performed successfully delegate control to another object responsible for knowing how to persist those changes to a database, web service or file storage. The action is decoupled from how it is executed/persisted.

A "command" feels like one half of the Action/Executor pattern - the "action" seems synonymous with a "command". The Action/Executor pattern takes things one step further and describes another concern whose responsibility is to take the changes generated by the "action" or "command" and save them some place.




回答3:


Action design pattern is same as Command design pattern. Action is a key entity, which encapsulates information with in itself regarding what's its behavior, what processing have to be done on its do() method, how it can be undone, and so on. When an application or any of its components is designed in accordance with Action design pattern, then Everything activity in the application can be represented in the form of action, every thing can be redone/undone several times. Eg. Macros in excel, Undo/redo in text-editors, etc.

Action class, which is a building block in this design pattern can be designed as below :-

public interface Action{
  public void do();
  public void undo();
  public void do(int iNoOfTimes);
}

public class FileCopyAction implements Action{
  private int iActionId;
  public void do(){}
  public void undo(){}
  public void do(int iNoOfItems){}
}

Hope it helps.



来源:https://stackoverflow.com/questions/380724/what-is-the-action-design-pattern

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!