命令队列

发布于 2024-11-09 08:13:45 字数 590 浏览 0 评论 0原文

所以我有一个我一直在开发的数据库(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魔法少女 2024-11-16 08:13:45

您正在寻找的是命令模式

基本思想是您有一个 ICommand 接口,其中有一个 Execute() 方法。
然后,您实现多个ConcreteCommand,例如带有参数化构造函数或属性NewNameRenameCommand

不要忘记相应地处理异常,如果可能发生的话,也可以实现撤消方法。我对你的申请了解不够,无法在这里做出假设。

public interface ICommand
{
    void Execute();
}

public class RenameCommand : ICommand
{
    private string newName;

    public RenameCommand(string newName)
    {
        this.newName = newName;
    }

    void ICommand.Execute()
    {
        item.rename(newName);
    }
}

What you are looking for is a Command Pattern.

The basic idea is that you have an ICommand Interface which has a method Execute().
Then you implement multiple ConcreteCommand, e.g. a RenameCommand with a parameterized constructor or a property NewName.

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.

public interface ICommand
{
    void Execute();
}

public class RenameCommand : ICommand
{
    private string newName;

    public RenameCommand(string newName)
    {
        this.newName = newName;
    }

    void ICommand.Execute()
    {
        item.rename(newName);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文