从库公开 API 的最佳方式
我正在设计一个 Win32 库来解析文件的内容(列和值)并将其内部存储在数据结构(映射)中。现在我需要公开 API,以便消费者可以调用这些 API 来获取结果。
该文件可能具有不同的格式,例如 FM1、FM2 等。消费者可能会这样查询:
FM1Provider.GetRecords("XYZ");
FM2Provider.GetRecords("XYZ");
我计划做的是拥有一个 CParser 类来完成所有解析并公开该类。
CParser
{
bool LoadFile(string strFile);
Map<string,string> GetFM1Records(string key);
Map<string,string> GetFM1Records(string key);
};
或者
class CResultProvider
{
virtual Map<string,string> GetRecords(string key)=0;
}
class CFM1ResultProvider : public CResultProvider
{
Map<string,string> GetRecords(string key);
}
class CFM2ResultProvider : public CResultProvider
{
Map<string,string> GetRecords(string key);
}
CParser
{
bool LoadFile(string strFile);
CResultProvider GetFM1ResultProvider();
CResultProvider GetFM1ResultProvider();
};
考虑到我正在开发一个库,请建议我其中哪一种方法是正确且可扩展的。
I am designing a Win32 library to parse the contents of the file (Columns and Values) and store it internally in a datastructure (Map). Now i need to expose API's so that the consumer can call those API's to get the results.
The file may have different formats eg FM1, FM2 etc. The consumer may query like
FM1Provider.GetRecords("XYZ");
FM2Provider.GetRecords("XYZ");
What i am planning to do is to have a CParser class that does all the parsing and expose the class.
CParser
{
bool LoadFile(string strFile);
Map<string,string> GetFM1Records(string key);
Map<string,string> GetFM1Records(string key);
};
or
class CResultProvider
{
virtual Map<string,string> GetRecords(string key)=0;
}
class CFM1ResultProvider : public CResultProvider
{
Map<string,string> GetRecords(string key);
}
class CFM2ResultProvider : public CResultProvider
{
Map<string,string> GetRecords(string key);
}
CParser
{
bool LoadFile(string strFile);
CResultProvider GetFM1ResultProvider();
CResultProvider GetFM1ResultProvider();
};
Please suggest me which one of these approaches are correct and scalable considering i am developing a library.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的组件似乎正在处理两个问题:解析和存储。将它们分成不同的组件以便它们可以独立使用是一个很好的设计实践。
我建议您仅向解析器提供解析数据的回调。这样,用户可以选择最适合其应用程序的容器,或者可以选择应用和丢弃读取的数据而不存储。
例如:
顺便说一句,更喜欢使用命名空间,而不是在类中添加 C 前缀。
Your component seems to be dealing with two problems: parsing and storing. It is a good design practise to separate these into different components so that they can be used independently.
I would suggest you provide the parser only with callbacks for parsed data. This way the user of it can choose the most suitable container for her application, or may choose to apply and discard read data without storing.
E.g.:
BTW, prefer using namespaces instead of prefixing your classes with C.
假设客户端只需调用
GetRecords
一次,然后使用地图,第一种方法我更喜欢第一种方法,因为它更简单。如果客户端必须在代码中的不同位置重新加载地图,那么第二种方法是更好的选择,因为它使客户端能够针对一个接口 (
CResultProvider
) 编写代码。因此,他只需选择不同的实现即可轻松切换文件格式(他的代码中应该有一个地方选择了该实现)。Assuming the client would only have to call
GetRecords
once, and then work with the map, the first approach I prefer the first approach because it is simpler.If the client has to reload the map in different places in his code, the second approach is preferable, because it enables the client to write his code against one interface (
CResultProvider
). Thus, he can easily switch the file format simply by selecting a different implementation (there should be exactly one place in his code where the implementation is chosen).