命令队列
所以我有一个我一直在开发的数据库(sql)管理程序,处理我公司销售的物品。对于程序的主要部分,项目信息会随着用户使用一些自定义控件而更新。一切都很好。但现在我进入了程序的更高级别部分,即添加/删除/复制项目。这不能是“动态更新”,就像编辑项目信息一样,因为很容易把事情搞砸。所以我基本上想建立一个文件->在这里为他们保存交易类型。我如何看待这个工作是在列表中创建所有 sql 命令,然后在保存时运行它们。
这里可用的功能包括添加新项目、删除项目、从另一个项目复制以及重命名。
因此,它将是一个如下列表:
insert...
update...
update...
delete...
update...
etc.
它将遍历并执行所有命令。我遇到的问题是我已经有一个类,它具有处理所有这些函数的方法。我想不出一种方法来调用队列中的所有这些方法,并且在另一个类中重写所有 sql 语句对我来说似乎很愚蠢。有没有一种方法可以让我记住类似以下内容的列表:
item.Rename()
item.ChangeSize()
item.Delete()
我觉得我想得太多了……或者可能想得不够……我不知道。
So I have a database (sql) management program I have been working on, dealing with items my company sells. For the main part of the program the item information is updated as the user goes through using some custom controls. That all works fine. But now I am on to the higher level section of the program that is adding/deleting/copying items. This cannot be "update on the fly", like editing item information, as it would be too easy to screw things up very badly. So I basically want to set up a file-> save type of deal for them here. How I see this working is creating all of the sql commands in a list and then running them all when saving.
The functions available here would be adding a new item, deleting an item, copying from another item, and renaming.
So it would be a list like:
insert...
update...
update...
delete...
update...
etc.
It would run through and execute all of the commands. The problem I am having is I already have a class that has methods to handle all of these functions. I can't think of a way to call all of those methods in a queue, and rewriting all the sql statements in another class seems stupid to me. Is there a way that I can remember a list of something like:
item.Rename()
item.ChangeSize()
item.Delete()
I feel like I'm overthinking this... or maybe underthinking... I don't know.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在寻找的是
命令模式
。基本思想是您有一个
ICommand
接口,其中有一个Execute()
方法。然后,您实现多个
ConcreteCommand
,例如带有参数化构造函数或属性NewName
的RenameCommand
。不要忘记相应地处理异常,如果可能发生的话,也可以实现撤消方法。我对你的申请了解不够,无法在这里做出假设。
What you are looking for is a
Command Pattern
.The basic idea is that you have an
ICommand
Interface which has a methodExecute()
.Then you implement multiple
ConcreteCommand
, e.g. aRenameCommand
with a parameterized constructor or a propertyNewName
.Dont't forget to handle exceptions accordingly and maybe implement an Undo-Method as well, if these could occur. I don't know enough about your application to make assumptions here.