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 trialtyronelobban
594 PointsOOP Design
Hi,
I'm interested in learning more about how to correctly design classes, and to learn what things to consider when designing them.
For example, within this lesson, it was strange to me to see a Combat class - as it didn't feel too intuitive to me. Having a Combat class makes me think that we would also have something like an Escape class, which could implement a run_away method and say, a crawl_away method. Monsters and Characters would then need to inherit from both Combat and Escape classes...which seems like it would start to get messy.
Instead of a Combat class (and Escape Class), I would think that we would rather have some sort of CharacterBase class that implements the methods in Combat and Escape (ie attack(), dodge(), run_away(), crawl_away() ), and then both Monster and Character could inherit from CharacterBase. Would that make sense as an alternative to Combat and Escape?
A lesson on oop design would be great, particularly if it showed some bad designs. Also, if it dealt with something slightly less obvious than game characters that would be great, ie perhaps if we had an application that needed to upload images to different websites (facebook, instagram etc). Would this application need to have a FaceBook_Uploader class and a Instagram_Uploader class? Or would it rather just need to have a generic Image_Uploader class that takes a target website on instantiation? Those kind of design decisions are interesting to me as that's where I struggle to know what to do.
Love the courses, hoping to see something along the above lines soon :)
Ty
3 Answers
Kenneth Love
Treehouse Guest TeacherDesign Patterns, especially in the OOP space, is a huge topic. Yes, it'd probably make sense to have an EntityBase
class that extends both Combat
and Escape
classes. And then Character
and Monster
would both each extend that EntityBase
class. That way you have multiple points of entry for adding new attributes/methods.
Andrei Fecioru
15,059 PointsI would view the Combat class more of a mixin rather than a base class. Python allows for multiple inheritance (which not all OOP programming languages do) and one can emulate the mixing of multiple pieces of functionality into a class via multiple inheritance.
Take a closer look at the Combat class and notice the following traits:
- it's name is a verb indicating that it is not a thing to be instantiated over and over again (like Monster or the Character classes)
- it does not define a thing but rather a behaviour or action (implements the mechanics of waging combat)
- it does not hold any state: you only have a couple of class-level attributes which act more like class-level constants. But you have nothing explicitly set on self.
So the Combat class defines pure behaviour and is not meant to be directly instantiated. It's only purpose is to be mixed-in into all the classes that want to be able to access the mechanics of combat.
Some programming languages have explicit built-in constructs to define this types of abstractions called mixins. These constructs are emulated in Python via multiple inheritance.
Kenneth Love
Treehouse Guest TeacherCombat is a noun, too :)
But, yes, it's mostly a mixin. I wasn't ready to bring up the word mixin at this point in teaching, though. It's just muddying the water on what a class is and how inheritance works.
Andrei Fecioru
15,059 PointsYeah I noticed it's a noun... but I didn't mention that 'cause it messed with my argument. One should never let the truth get in the way of a good story...
Devin Harris
9,693 PointsTyronne,
I don't have any good recommendations (yet) on TreeHouse to point you at however there is one book that I love on OOP (The Object Oriented Thought Process by Matt Weisfeld), there are also some good courses on Kahn Academy that cover OOP from a computer science perspective. I have found that even among experts in the field OOP methodologies can have greatly differing views and it seems these views are constantly evolving. I hope you find what you are looking for.
tyronelobban
594 PointsHey Devin,
Thanks so much for the reply! I will definitely check out those resources - the book sounds very interesting.
Have a good day.. Tyrone
Devin Harris
9,693 PointsDevin Harris
9,693 PointsTyronne,
I don't have any good recommendations (yet) on TreeHouse to point you at however there is one book that I love on OOP (The Object Oriented Thought Process by Matt Weisfeld), there are also some good courses on Kahn Academy that cover OOP from a computer science perspective. I have found that even among experts in the field OOP methodologies can have greatly differing views and it seems these views are constantly evolving. I hope you find what you are looking for.