Draw Your Idea
00:00 In this lesson, I’ll try to sketch out some ideas that I have for how I might build the model of my farm. Drawing is a great way to brainstorm and to get a better feel for what the task you’re trying to tackle is all about.
00:14 Your aim here shouldn’t be to get a perfect representation of the code that you’ll eventually write, but rather just to get an initial idea scribbled into your notebook.
00:24 It’ll still give you a place to start and a direction to move towards, but you’ll probably revisit the model after you’ve spent a bit of time coding. So let’s get started and draw a little.
00:36
I want to have an Animal
class. This is something I know about. So let’s say I’ll have this class called Animal
. That’ll be my first class.
00:46 And then I want to have three child classes. And then you can use these arrows to represent these.
00:54
They’re going to be child classes of Animal
. I’m going to model a Dog
,
01:02
and I’ll stick with dogs, right? We’ve been doing a lot with dogs, so why not? And then because it’s a farm, I’m going to model a Pig
and let’s say a Sheep
.
01:16
So these are the three subclasses of Animal
that I’m going to try to model and they’re going to inherit from Animal
. Now I want to have some common methods.
01:25
I’m going to define them in the Animal
class so that all three of the subclasses have access to them. I want my animals to be able to move.
01:34
So .move()
, we’re going to have that. They should be able to eat,
01:40
and maybe talk, sure, on a nice farm, .talk()
.
01:46
These are going to be the three instance methods that I’m going to implement and I’ll implement them in the Animal
parent class. And then each of the child classes are going to have access to them.
01:55
They’re all going to be able to move in the same way and eat in the same way. But let’s override the .talk()
method or probably extend it in the way that we did in the exercise as well.
02:06
So I’m going to also define .talk()
in each of the child classes.
02:12
And for fun, I want to be able to throw some things so the dog is going to go and fetch it. So additionally, I’m going to implement a .fetch()
method for Dog
that Pig
and Sheep
don’t have.
02:25
Okay, so this looks pretty nice for now. Maybe there’s going to be some class attributes I’m not thinking about right now, but I think this is an idea of modeling an Animal
class and then three subclasses.
02:36
Now I also want to be able to move to different locations. So I’m going to have another parent class that I’m going to call FarmLocation
so that I can also have things that are not buildings.
02:49
Let’s make two different locations. I’m going to have one that is a Field
and one that is a Barn
. The weather’s bad.
03:01
and these are going to inherit from FarmLocation
. So again, I’m going to be able to define methods and attributes over in FarmLocation
that then both Field
and Barn
are going to share.
03:13
And I want animals to be able to enter and exit this location. So I’ll make some methods called .enter()
and .exit()
. They should be valid for either fields or barns.
03:27
And I also, I’m going to want to keep track of which animals are in a location. So I will use some sort of attribute here, .animals
, and this is going to be interesting because that’s going to map to Animal
objects.
03:42
So Animal
objects can be part of such a FarmLocation
. And this is badly drawn Ro boy. I guess here we’re talking about composition, right? So this is a, has a relationship which is composition, or specifically in this case it’s aggregation because this FarmLocation
can also not have any animals in there.
04:06 So it’s not composed of animals, but it can aggregate animals inside of it. This arrows that I drew before, this is inheritance, right? So we’re creating child classes here.
04:17
Aggregation is somewhat similar to composition, but it’s a little less severe if you want. The difference is that the FarmLocation
can exist without the animals in it.
04:25 And composition. You would actually build the object from other objects. I think this looks pretty good. I’m going to have to probably also keep track of some spaces.
04:36 So those are probably instance attributes here.
04:39 Spaces how many animals can fit in a field or in a barn. And I’ll initialize that when I create the instances of these classes. And I’m sure I forgot something, but this is going to be can I’m like my working scribble that I’m going to go off on and then see what happens when I try to convert it into code.
05:00 And keep in mind that drawing such a diagram, it doesn’t have to be perfect at all. This is really just a way for you to organize your brain a bit and try to think about what are you probably going to do in your code.
05:12 And it helps you to guide your coding and maybe already catch a couple of mistakes or avoid them, or at least have a path that you can follow when you start coding and it’s going to make it easier to develop your classes.
05:25 So let’s get started and code something up in the next lesson.
Become a Member to join the conversation.
ajackson54 on July 20, 2024
Hello, Martin — I had a plan of creating a purchase class and then updating the number of a particular animal purchased to the current inventory. Here is the code: ```class PurchaseAnimals(Animal): def buy(self,number): update_count(self,number)
I tried various variations by placing the total_num as a parent class instance, putting it outside the methods in PurchaseAnimals, and inside the update_count method. The last time I used OOP was in Java. Is the use of parameters the same in Python? In this code above I received an “update_count not defined” error. Finally, I think I should branch out with the learning path and take the OOP course and Getters and Setters along with my current path. I’ve had problems understanding OOP. (maybe because of the different terminologies). P.S. I’m still having problems with my format even though I’m entering “```” around my code.