• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

create object  RSS feed

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a question about object design. I want to create a "Item" class which belongs to different Categories and Subcategories.

My question is : do I need to create list of Categories and Subcategories inside to Item class and what is right approach?

Class Item {

String id;
List category = new ArrayList();
List subCategory = new ArrayList();

...

}

or

Class Item {

String id;
String category;
String subCategory;
....

}

which way is better?

Thanks
 
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It depends on your requirements. If your Item can belong to many categories, then the first way is right. If it can only belong to one category, then the second way is better.

However, I'm not sure about the way you've modeled category as a String, especially when you also have subcategories. How are the categories related to subcategories? How would you show this relationship if you just have two Lists?
 
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Depends. Can an item belong to multiple categories and/or sub-categories? Do sub/categories carry more information than just a name, in which case they should be objects, not strings? Are sub-categories related to categories?

Edit What he said.
 
hong ye
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Greg/David.

In my case, Each item can belong to one or more categories and Each category can have multiple sub-categories and the sub/category can carry more information. Yes the sub/category are objects.

Can you explain a little more in detail about the benefit by modeling the sub/category as list in Item class?

Thanks
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using a string doesn't give you any benefits, and has several drawbacks, including no type-safety, no ability to trivially represent the data model you claim you're using, and doesn't enforce any relationship between sub/categories.
 
hong ye
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks David.

Can you give me a recommendation for which web site or book to learn about modeling object?

thanks a lot.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!