如何在 OOP 中为 API 或框架设计一个健壮的 Status 类?
我目前正在致力于从临时企业解决方案生成 API,并且我陷入了生成通用类和表达类以处理相关类中的操作状态的过程中。
简而言之,我想知道设计状态处理程序的标准(或最佳实践)是什么。我想要实现的是属于一个公共“模块”的方法和类,它们都返回并处理属于定义的状态结构的状态消息,因此不同的状态消息并不依赖于实现。
一个可能的解决方案是: - 在模块中创建一个状态类,将一系列状态号定义为常量 - Status 类将仅管理那些已定义的状态编号,并为每个状态编号定义状态消息。 - 需要状态处理的模块方法返回 Status 实例,该实例在模块的较高层中处理,而不是在每个方法中硬编码。
有更好的模型建议吗?好的 API 如何管理这个?
I'm currently working on generating an API from an ad-hoc enterprise solution and I'm stuck in the process of generating a both generic and expressive class for handling the status for operations in related classes.
In a nutshell, what I want to know is which are the standards (or best practices) on designing a Status handler. What I want to achieve is that methods and classes that belong to a common 'module', all of them return and handle status messages that belong to a defined status structure, so the different status messages are not that dependent of implementation.
A possible solution would be:
- Create a Status Class in the module that defines a series of status numbers as Constants
- The Status class would manage only those defined status numbers and would define status messages for each one.
- The methods of the module that require status handling return an instance of Status, that is handled in higher layers of the module, and not hard coded in each method.
Any suggestions for better models? How does good API's manage this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用状态 enum,而不是常量类。它将为您提供一组明确定义且适当约束的状态值。它还可以具有 getMessage() 以及任何其他状态相关的功能。
Use a Status enum, not a class of constants. It will provide you with a well defined and properly constrained set of status values. It can also have a getMessage() as well as any other status related functionality.