简体   繁体   English

我应该只是移动文件还是创建一个移动文件的类?

[英]Should I just move the file or create a class which moves it?

Okay, I got this small program which tags (as in ID3v2.4 etc.) some music files. 好的,我得到了这个标记(如在ID3v2.4等中)一些音乐文件的小程序。 Now I want the user to have the option to move and/or rename those tagged files if he/she wishes to. 现在我希望用户可以选择移动和/或重命名这些标记文件,如果他/她愿意的话。

Considering that I am trying to keep a fairly clean and loosely coupled design in this system (even though extensibility is not really important here, it's just fun), would you just call someFileInfoObject.Move(someWhere) where someWhere is the applied pattern or would it be wise to implement some classes - maybe MoveFileStrategy, RenameFileStrategy (I know that moving/renaming can be considered the same in some systems, but I want them to be enabled separately) - which figure out the destination and whether the strategy should be applied when an Apply(FileInfo file) method or so is called. 考虑到我试图在这个系统中保持一个相当干净和松散耦合的设计(尽管可扩展性在这里并不重要,但它很有趣),你会调用someFileInfoObject.Move(someWhere) ,其中someWhere应用的模式或者实现一些类是明智的 - 可能是MoveFileStrategy,RenameFileStrategy(我知道移动/重命名在某些系统中可以被认为是相同的,但我希望它们能够单独启用) - 它会找出目标以及是否应该应用策略当调用Apply(FileInfo file)方法时。

If you think that some strategy classes may be useful, do you have any suggestion on a good implementation strategy? 如果您认为某些策略类可能有用,您是否对良好的实施策略有任何建议?

As already said, over-engineering is not really an issue here, because it is a fun project mainly targeted at getting some programming and engineering practice. 如前所述,过度工程在这里并不是一个真正的问题,因为它是一个有趣的项目,主要是为了获得一些编程和工程实践。 :) :)

You can make the case that you have a virtually unlimited number of commands for your files. 您可以证明您的文件具有几乎无限数量的命令。 Think of this class hierarchy. 想想这个类层次结构。

Command
    CopyCommand
    RenameCommand
    MoveCommand
    DiffCommand
    CompressCommand

These aren't really strategies. 这些并不是真正的策略。 They're just ordinary classes with a simple "execute" method. 它们只是具有简单“执行”方法的普通类。 You provide the options and arguments through ordinary setters. 您可以通过普通的setter提供选项和参数。 Then you execute the method. 然后执行该方法。

This borrows from Ant's design pattern for Tasks that can be plugged in. 这借鉴了Ant可以插入的任务的设计模式。

Off the top of my head, building on @S.Lott, keep the commands themselves simple and atomic, and create a command queue. 在@ S.Lott的基础上,保持命令本身简单和原子化,并创建一个命令队列。 The UI add's commands to the queue, and the program executes the commands sequentially. UI将命令添加到队列,程序按顺序执行命令。

Additionally, you could hang onto (memento's) of executed commands and provide an undo facility. 此外,您可以挂起(memento)执行的命令并提供撤消功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM