开发者

Categorizing items the right way

I'm开发者_如何学C working on an application which among other things downloads items that belong to a certain category form a server. I want to make the downloader look like this:

class Downloader
{
    Downloader(const ItemCategoryBase &category);
    ...
}

Each class derived from ItemCategoryBase will provide it's category ID trough a virtual function (in fact that's the only thing each derived class will do).

The issue I'm having is that I have a total of 120 item categories and writing a derived class for each one is going to be painful.

I've considered using a primitive to hold the ID but, I do not wish to implement range checking and throw exceptions in case the ID is out of range mainly because category IDs aren't all part of the same interval.

What I'm looking for is an efficient way of writing code that would fit the scheme above.

Any help is highly appreciated.


If you really have determined that this is the right way to do things, then I would suggest writing a code generator to handle it for you: create a CSV document containing all the Category ID's, and write an app that inserts each ID into template header/source files, and saves it out.. (For instance, put "$CATEGORY_ID" in wherever the Category ID goes in the files, and then just do a replace on "$CATEGORY_ID" with each ID in turn.)

However, I'm not sure I understand your statement: "I've considered using a primitive to hold the ID but, I do not wish to implement range checking and throw exceptions in case the ID is out of range mainly because category IDs aren't all part of the same interval." I can't imagine a case in which you wouldn't have to handle the complexity somewhere in your application anyway, and the range checking wouldn't be hard: just put all the valid Category IDs into a list structure of whatever your ID type is, and a simple index lookup call can answer whether the ID is part of that list.

If I have misunderstood you, what exactly is it about your setup that makes dealing with 120 ItemCategoryBase derived classes simpler than one ItemCategoryBase base class validated against a list of the IDs? You say "mainly because category IDs aren't all part of the same interval," so perhaps the checking against a list would give you what you need there. Otherwise, can you explain a bit more about how it works? Although I realize there are always exceptions, 120 classes doing nothing other than providing different IDs really strikes me as something that's unlikely to be a solution that will serve you well in the long run.


Since you're using C++, why not use templates and specify a non-type template parameter containing the ID?

For example, supposing that the category is an integer:

template<int category_id>
class Downloader : public ItemCategoryBase
{
    public:
    virtual int get_id()
    {
        return category_id;
    }
};

You might as well let the compiler do the work for you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜