开发者

How to decide if this should be split into 2 classes or kept in the same class?

I've got a page with a list of some items. Each item has got some properties: comments, images, a title, a comments form, and content. It seems simple enough to create a class called item which will represent a single item in the list.

However, I've also got an 'orphaned' item which is used for adding new items to the list. E.g, it has got all the same properties, textboxes, buttons, etc for adding images, writing the content, etc but its not stored in the db, and it has got a new 'add' button which saves it to db, and its then added to the list.

Should I still create just 1 class called item and just have a property called something like isSaved to determine whether to show the 'Add' button, whether to show the comment form for it, etc?

Or should I create 2 seperate classes, call one newItem and use it for the orphaned item, and another savedItem and use it for the items which are already in the db?

Also, this is 开发者_如何学运维in javascript so I don't think I can use much inheritance.


In general, you subclass when behavior or properties need to change. If the only difference is whether the object is saved to the DB or not, most likely, that is not a candidate for a subclass. Adding a property such as isSaved would probably be the best bet. Unless, that is, the behavior of one is fundamentally different from the other when it is saved/unsaved.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜