java - OO Design pattern for shopping cart -


i trying learn oo design pattern , developing shopping cart application different cart items have different types. each type have additional attributes.

option 1: create separate class every cart item own attributes , common attributes in base class. in option, have create multiple classes , corresponding table hierarchy in database.

option 2: create single cart item type attribute identify type of attribute. irrelevant attributes particular cart item null in case.

i know basic question want know how people follow best practices.

thanks in advance.

i use strategy this, example:

public interface cartitem{   string getname();    bigdecimal getprice(); } 

only work interface in shopping cart. way, if want new cart item, let implement interface, , work :)

by using interface, still have full freedom in how design actual cart items (either having 1 class, or multiple classes)


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -