返回一个向量,知道它将始终包含单个记录,以便与界面的其余部分保持一致?

发布于 2024-10-17 09:37:28 字数 1616 浏览 6 评论 0原文

我正在编写一个小型地址簿应用程序,并且在数据源/后端接口方面遇到了设计困境。

我有以下数据源类的抽象基类:

class DataSource
{
    private:

    public:
        virtual std::auto_ptr<Contact> getContact(int id) = 0;
        virtual ContactRecordSet getAllContacts() = 0;
        virtual bool addContact(const Contact& c) = 0;
        virtual bool updateContact(int id, const Contact& c) = 0;
        virtual bool deleteContact(int id)=0;
        virtual ~DataSource() {};

};

下面是我的记录结构,tmy 记录集是这些对象的 STL 向量的 typedef。

class Contact 
{
    public:
        std::string firstName;
        std::string lastName;
        std::string phoneNumber;
        std::string address;
        std::string email;

};

typedef std::vector<Contact> ContactRecordSet;

我的问题涉及 DataSource::getContact() 方法和 DataSource::getAllContacts() 方法使用的返回值类型以及即将添加的搜索方法,该方法将根据查询获取记录。

DataSource::getContact() 将返回 0 或 1 条记录,因为我是通过唯一 id 查找的。 DataSource::getAllContacts() 将返回零个或多个联系人。 即将推出的搜索方法将返回零个或多个联系人。

正如我现在所拥有的, getContact() 方法正在将 auto_ptr 返回给联系人,因为如果我确定它们永远不会超过一个,则返回 ContactRecordSet 似乎很浪费,并且如果没有记录,它允许我返回 NULL有那个id。

getContact() 也返回一个 ContactRecordSet 是否会更好,只是为了保持界面一致?

我对返回像单个对象那样的数据结构的想法感到恼火,但另一方面,它更加一致,并且检查是否找到该 id 的值的语义似乎更符合整体抽象设计(检查返回记录集的长度与检查 NULL auto_ptr)。

大家觉得怎么样?

(注意 - 我知道我可能对一个简单的地址簿应用程序进行了过度设计,但我希望能够轻松地交换不同的后端(平面文件、SQL 等...),前提是它们实现了通用的目标是实践良好的模块化设计和关注点分离。)

更新

我想我可以从相反的角度来看,并使多个记录方法将 auto_ptrs 返回到 ContactRecordSet objectcs。这样a)它是一致的,因为你总是得到一个指向对象的指针,b)如果记录集为空,你就没有返回std::vector的开销,只需返回一个NULL指针。

I'm writing a little address book application and have a design dilemna regarding the interface for the data source / backend.

I have the following abstract base class for data source classes:

class DataSource
{
    private:

    public:
        virtual std::auto_ptr<Contact> getContact(int id) = 0;
        virtual ContactRecordSet getAllContacts() = 0;
        virtual bool addContact(const Contact& c) = 0;
        virtual bool updateContact(int id, const Contact& c) = 0;
        virtual bool deleteContact(int id)=0;
        virtual ~DataSource() {};

};

Below is my record struct and tmy record set is a typedef to an STL vector of these objects.

class Contact 
{
    public:
        std::string firstName;
        std::string lastName;
        std::string phoneNumber;
        std::string address;
        std::string email;

};

typedef std::vector<Contact> ContactRecordSet;

My question involves the return value type used for the DataSource::getContact() method and the DataSource::getAllContacts() method and the search method to be added soon that will get records based on a query.

DataSource::getContact() will return zero or 1 records since I'm looking up by unique id.
DataSource::getAllContacts() will return zero or more contacts.
The upcoming search method will return zero or more contacts.

As I have it now the getContact() method is returning an auto_ptr to a Contact because it seemed wasteful to return a ContactRecordSet if I know for sure they'll never be more than one and it allows me to return NULL if there is no record that has that id.

Would it be better to for getContact() to return a ContactRecordSet also, simply for the interface to remain consistent?

Part of me chafes at the idea of returning a data structure like that for a single object, but on the other hand it is more consistent and the semantics for checking if a value was found for that id seem more in line with the overall abstraction of the design (check length of returned recordset vs. check for a NULL auto_ptr).

What do you all think?

(Note - I'm aware I'm probably be over-engineering for a simple address book application but I want it to be easy to swap out different back ends (flat file, SQL, etc...) provided they implement the common interface. The goal is to practice good modular design & seperation of concerns.)

UPDATE

I suppose I could look at from the opposite perspective and make the multiple record methods return auto_ptrs to ContactRecordSet objectcs. That way a)it's consistent in that you're always getting a pointer to an object and b) you don't have the overhead of returning a std::vector if the record set is empty, simply return a NULL pointer.

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

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

发布评论

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

评论(2

我的痛♀有谁懂 2024-10-24 09:37:28

我始终遵循返回定义对象的最简单事物的设计原则。向量旨在保存事物列表而不是单个项目,虽然它可能使语义对称,但无疑对另一个事物来说是不直观的开发商。

I always follow the design principle of return the least complex thing that defines your object. Vectors are meant to hold lists of things not single item and while it might make the semantics symmetric it will undoubtedly be nonintuitive to another developer.

吃颗糖壮壮胆 2024-10-24 09:37:28

为非复数函数返回复数类型有何一致性?

我认为你需要对“一致性”有不同的定义。

What's consistent about returning a plural type for a non-plural function?

I think you need to be working with a different definition of "consistency".

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