如何构造许多类似事件的方法?
我正在开发的系统的中心对象变得有点太笨重了。问题在于它可以通过多种不同的方式采取行动。您可以:
- 取消它
- 拒绝它
- 丢弃它
- 出售它
- 拆分它
- 使其过期
- ......以及更多。
我正在考虑将那些类似事件的方法从类中分解出来,并使其更加通用,就像命令模式一样。但大多数方法都有不同的参数,因此使用像 Run() 或 Execute() 这样的解决方案可能会出现问题?
有什么想法如何构建它,以使其灵活且简单地添加新命令、操作或您想称呼它们的内容吗? (我不会称它们为真实事件,因为没有听众。)
The central object of a system I'm developing is getting a bit too unwieldy. The problem is that it can be acted upon in so many different ways. You can:
- Cancel it
- Deny it
- Discard it
- Sell it
- Split it
- Expire it
- ... and plenty more.
I'm considering factorizing those event-like methods away from the class and make it a bit more generic, like a Command pattern. But most of the methods have different parameters, so using a solution like Run()
or Execute()
could be a problem perhaps?
Any ideas how to structure this, to make it flexible and simple to add new commands, or actions or what you would like to call them? (I wouldn't call them real events since there are no listeners.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在其构造函数中传递命令所需的参数吗?这样,您就可以使用无参数命令。
Can you pass the required parameters for your commands in their constructor ? That way, you can have a parameter-less command.
将参数封装为单个参数:
它是一种伪代码,您可能喜欢使用与您的编程框架相匹配的集合库。
Encapsulate the parameters, as a single parameter:
Its sort of pseudocode, you may like to use the collection libraries that match your programming framework.