Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

how many class that I have to make in OOP and MVC design pattern

hi,

when making code from scratch in MVC design pattern, I often confused when making Model data. do i have to separate this problem into 3 different class/object or just make it 2.

for example, if I want to build shoping cart app. from the tutorial I follow, they separate 3 different class.

  1. Product Data class (price, quantity)
  2. Calculator Class ( to calculate total cost after tax)
  3. shopping cart class

but when i build it by myself, I just make 2 class for model in MVC.

is there any guideline about how many class do I have to make in OOP ? should I split it into 2 different class or just one

thanks

1 Answer

Patrick Duhamel
Patrick Duhamel
5,786 Points

Usually, you will create as low or as much classes as you need for your projects. The class should do a specific job very well. It's technically possible to create a mega-class that will do everything, but when will come the time for debugging, or just adding features or options, it will become an issue to just understand the flow of the code and how to readjust things. By separating specific tasks to classes, you can easily know where a bug may come from and where to add features. For example, if you want to extend your shopping app internationally, you just need to adapt the calculator class for the different taxes depending on the country, or if you want to add more details to products, you just need to work on the product class without affecting the code on the other classes and add more bugs.