Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Oop 命令模式澄清_Oop - Fatal编程技术网

Oop 命令模式澄清

Oop 命令模式澄清,oop,Oop,我看不到任何命令模式类,例如invoker、receiver在下面链接的接受答案中出现。我已经用公认的答案来解决我的30多条if/else语句 我有一个存储库,我正试图通过DTO保存到数据库中。我希望存储库为DTO调用正确的save方法,以便在运行时检查实例类型 下面是存储库中的实现 private Map<Class<?>, Command> commandMap; public void setCommandMap(Map<Class<?>, Com

我看不到任何命令模式类,例如invoker、receiver在下面链接的接受答案中出现。我已经用公认的答案来解决我的30多条if/else语句

我有一个存储库,我正试图通过DTO保存到数据库中。我希望存储库为DTO调用正确的save方法,以便在运行时检查实例类型

下面是存储库中的实现

private Map<Class<?>, Command> commandMap;
public void setCommandMap(Map<Class<?>, Command> commandMap) {
    this.commandMap = commandMap;
}
最后是节约的方法

public void getValue(){
    commandMap.get(these.get(0).getClass()).save();
}
使用Repo的服务类注册commandMap


接受的答案是否表示命令模式的某种(近似)实现

实现exec接口的枚举似乎可以消除if/else问题或将其转换为交换机

看起来您不需要命令模式

说:


您想执行上述哪项操作?

实现exec接口的枚举似乎可以消除if/else问题,或者将其转换为交换机

看起来您不需要命令模式

说:


你想做以上哪一项?

我会选择第一个选项,这不是命令模式。有些答案会让人产生误解,但我也会使用该模式中的一些想法,但不将其称为模式。也许我会选择第一个选项,这不是命令模式。有些答案会让人产生误解,但是,我也会使用模式中的一些想法,但不把它们称为模式
public void getValue(){
    commandMap.get(these.get(0).getClass()).save();
}
Use the Command pattern when you want to

    parameterize objects by an action to perform, as MenuItem objects did above. You can express such parameterization in a procedural language with a callback function, that is, a function that's registered somewhere to be called at a later point. Commands are an object-oriented replacement for callbacks.

    specify, queue, and execute requests at different times. A Command object can have a lifetime independent of the original request. If the receiver of a request can be represented in an address space-independent way, then you can transfer a command object for the request to a different process and fulfill the request there.

    support undo. The Command's Execute operation can store state for reversing its effects in the command itself. The Command interface must have an added Unexecute operation that reverses the effects of a previous call to Execute. Executed commands are stored in a history list. Unlimited-level undo and redo is achieved by traversing this list backwards and forwards calling Unexecute and Execute, respectively.

    support logging changes so that they can be reapplied in case of a system crash. By augmenting the Command interface with load and store operations, you can keep a persistent log of changes. Recovering from a crash involves reloading logged commands from disk and reexecuting them with the Execute operation.

    structure a system around high-level operations built on primitives operations. Such a structure is common in information systems that support transactions. A transaction encapsulates a set of changes to data. The Command pattern offers a way to model transactions. Commands have a common interface, letting you invoke all transactions the same way. The pattern also makes it easy to extend the system with new transactions.