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

Android Build an Interactive Story App (Retired) The Model-View-Controller Pattern Creating the Story

Trying to wrap my brain around the purpose of Creating the separate "story" class.

Hello everyone!

I am trying to understands the point of creating the serpate class of story that holds the array structure of Page objects. What is the reason and rationale behind this? I have to admit, i would have never even thought to separate the Choice and Page classes, let alone the Story and Page classes. I feel...kind of intimated by this jump in thinking that i feel i would never make :(

1 Answer

Jon Kussmann
PLUS
Jon Kussmann
Courses Plus Student 7,254 Points

It takes a long time to get used to thinking about, but try to think of making everything as re-usable as possible and while modeling real world objects.

In this case, you have a Story class and a Page class. Sure, a story can have several pages in it... But let's say later on you want to create a new "Book", or a "Magazine" If you didn't separate out the Page class from the Story class, then you'd have to sort of do it over again for those new classes. Instead, you can just use that same Page.class for those other projects of yours, and you won't have to change much to the class (if at all).

That's a more practical of looking at it, but it also helps to understand much larger applications/projects that you may want to work on in the future...

thanks for your helpful words there! So, you recommend to think about things in a way "how future-proofed is this?". I have a hard time identifying the "dividing lines" so to speak for the concept of "Only one responsibility per class"

Zaw Mai
Zaw Mai
1,605 Points

Thanks for the helpful advice. Ben summarized it really well when he say "Single Responsibilty Principle" (SRP). Adding on the benefit of this principle, it is easy reading and handling of your codes when you're working in a team or reviewing it. I'm still trying to figure out the "dividing lines" as well. What helps for me is, when I write codes, I start to realize that the snippet of code I wrote could be reused elsewhere. So I organize it into a class. Another way of thinking is finding similarity of codes and breaking it down the fundamental unit of codes. This can be used as a member variable for model classes or controller classes. Picture a human class. You can build a chef class, secretary class, or a janitor class, because they all share the same functions of human behaviors. However, they each possess unique skills and perks according to the profession.

Here's analogy of classes comparing it to different types of bikes. https://docs.oracle.com/javase/tutorial/java/concepts/class.html