是否有一种设计模式可以帮助在通用显示器上配置命令?

发布于 2024-10-31 08:26:47 字数 468 浏览 1 评论 0原文

我有一个显示对象,它显示用户列表并提供一个用于对该列表进行操作的菜单 - 添加新用户、将它们添加到组、删除它们等。现在,显示器配置自己的菜单,因此它可以添加菜单命令如doCreateNewUsers()。不幸的是,这意味着显示的每个实例始终具有“创建新”选项。

我想针对不同的显示实例以不同的方式配置菜单 - 在“用户”选项卡中,它应该包含“创建新”选项,而在“组”选项卡中,它不应该包含。我的第一个想法是外部化菜单,以便我可以以不同的方式配置它。问题是我失去了调用私有doCreateNewUsers()函数的能力!

有针对这种情况的设计模式吗?我不喜欢将 doCreateNewUsers 公开的想法,因为它显示了一个不应由外部类触发的对话框。我可以使显示抽象,以便我可以在匿名子类中定义菜单,但这会扰乱我现在重用小部件的方式 - 我想在创建并初始化显示后配置菜单。我希望有一些行业标准的方法来处理这个问题!

I have a display object that displays a list of Users and provides a menu for acting on that list - adding new users, adding them to groups, deleting them, etc. Right now the display configures its own menu, so it can add a menu command like doCreateNewUsers(). Unfortunately, this means that every instance of the display always has the "create new" option.

I want to configure the menu differently for different instances of the display - in the "Users" tab, it should include the "create new" option, and in the "Groups" tab, it shouldn't. My first thought was to externalize the menu, so that I could configure it differently. The problem is that then I lose the ability to call the private doCreateNewUsers() function!

Is there a design pattern for this situation? I don't like the idea of making doCreateNewUsers public because it shows a dialog that shouldn't be triggered by external classes. I could make the display abstract, so that I could define the menu in anonymous subclasses, but that kind of messes up the way I reuse widgets right now - I'd like to configure the menu after the display has been created and initialized. I'm hoping there's some industry-standard way of dealing with this!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

夏见 2024-11-07 08:26:47

在我看来,在这种情况下您可能会使用策略模式

It sounds to me like you could possibly use the Strategy Pattern in this case.

梦醒灬来后我 2024-11-07 08:26:47

显示器可以配置自己的菜单,但前提是该操作可以封装在与菜单项关联的命令中。一旦您进行了这样的设置,您就可以从菜单中添加和删除项目,并且他们不需要访问私有方法,因为他们将有权访问负责该操作的命令。

我认为 命令模式 是你的朋友。

一旦获得命令对象,您就可以使用它们来创建菜单项并提供执行实际命令的方法。

如果您只是添加 User 对象列表,则传递命令对象可能更有意义。如果您传递一个代表用户集合的对象,那么添加一个方法来公开命令对象可能是有意义的。如果是这种情况,那么您可能应该让您的对象实现一个接口以提供对命令对象的访问,例如:

public ICommandProvider
{
    ICollection<ICommand> GetCommands();
}

那么命令可以是:

public ICommand
{
     String GetMenuText();
     void Execute();  
}

让正在显示的对象负责返回命令可以很容易地让命令具有对所显示内容的引用,因此可以对其进行修改(例如添加元素或删除元素)。
显然,确切的细节将取决于您的情况,但类似的东西应该允许您配置显示器的菜单选项,而显示器不需要了解正在执行的操作的详细信息。您可能需要在执行命令后刷新显示。

The display can configure its own menu but only if the action can be encapsulated in the command associated with the menu item. Once you set it up like that then you can add and remove items from the menu and they won't need access to the private methods, as they will have access to the command that is responsible for the action.

I think the command pattern is your friend here.

Once you have the command objects you can use those to create the menu items and provide the methods to execute the actual commands.

If you are just adding a list of User objects the passing the command objects might make more sense. If you are passing an object which represents a collection of users the it might make sense to add a method to expose the command objects. If this is the case then you should probably make your objects implement an interface to give access to the command objects, something like:

public ICommandProvider
{
    ICollection<ICommand> GetCommands();
}

then a command could be:

public ICommand
{
     String GetMenuText();
     void Execute();  
}

Having the object that is being displayed responsible for returning the commands makes it easy to have the commands have a reference to the thing being displayed so they can modify it (add elements or remove elements for example).
obviously the exact details will depend on your situation, but something like that should allow you to configure your display's menu options without your display needing to know about the details of what is being done. You'll probably need to refresh the display after a command.

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