Designing a Python Class

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video I'm going to look at how we can design a class a Python class from a given specification well here we can see we've got a specification I'm just going to read it through whenever a customer joins a bank an account is created in their name and the balance is set based on how much they deposit the account must also allow for the deposit to credit the account and follow withdrawal to debit the account upon request the balance of the account and the name of the customer will be returned also once an account is created the name of the customer can be changed for example if somebody was named Rita Jones and they got married their name could be changed to their new married name for example Rita Jones could be changed to Rita Hartley once you've got the specification would you start what's your first step in trying to produce a class trying to build the class that will actually implement the specification given well one of the things you can do is you can look at the words in the specification and identify all of the verbs and identify all of the nouns a verb conveys an action which we can think of as a behavior ie a method if we're dealing with object orientated programs now when we talk about a verb we're talking about a doing word and an action of some kind and of course when is an action going to take place and we want a program to perform that action we will have program statements that we group together in a method so when we see a verb in a specification it kind of implies to us that oh I might have a method that will perform the action of that particular verb what that verb is suggesting a noun is a name of some specific thing a state of existence ie an attribute or more precisely a data attribute often called data fields it something that is likely to become a variable within our particular class that we wish to build to implement the specification given returning to the specification I'm now going to try and identify all of the verbs and nouns that appear and these verbs and nouns will then suggest what might be useful to help me build a class to implement this particular specification here now I'm not going to look for all the verbs first and then all the nouns I'm going to go through the specification one word at a time and stop when I think I come across a word that I feel is important to help me build a class and the first word I come across is customer if I scan through the actual specification I can see that this appears a couple of more times as you can see now that's a word that I'm going to extract and write in a list if I continue I come across another word here Bank and I can have a look to see if that appears elsewhere in the specification and I can see that it doesn't so I'll carry on and then I come across account and if I have a look I can see that appears numerous times throughout the specification suggesting to me that this is quite an important word that I need to consider if I now carry on I come to this one call created now created is really a verb is entities are doing with I'm going to be creating something of a program is going to be creating something so I think that's kind of important if I carry on I can say that it appears twice now going again one word at a time through the specification I then come on through this here name now I have a quick look at that and realize that this is going to be the name of the customer does it appear anywhere else in the specification we can see it appears twice more so that's a word that I'm going to use now if I think about the name for a moment it certainly is in the verb is it it's not a doing word this is a noun so that is likely to become a variable in my class now what kind of variable we're going to see later in fact it's going to become an attribute in my class a data attribute in my class a data field now if I continue through the actual specification I then come to balance which well I think that's important does it appear in anywhere else yes it does as you can see so balanced now what is balanced is that they doing what is that a verb well no I would suggest that's a noun in the context that we're considering it here and it's going to be the balance of the account of a particular customer ie how much money they have in the bank if we now continue through the specification we can look for other words and here you can see that I'm highlighting the word set now what does that suggest to me well it's not a noun is it that is a verbage doing something so it kind of suggests that I may have in my class definition a method that will be setting something I'm not saying it will be what I'm attempting to do here is get suggestions for what will actually be in my class if I continue I now come to this word here deposit and I'm going to have a look if that appears elsewhere and you can see it does so that suggests that's going to join my list continuing on I come to credit and I think well that's quite important let's concentrate on this one for a moment what am I doing here well I'm going to credit so if I go into the bank and give the bank a hundred pounds I want that to go and alter my balance by one hundred pound in the positive direction so if I had 50 pound in the bank and I go in and ask them to credit it with a hundred pound I should have 150 after I've credited it in other words my deposit was a hundred pound and I'm using the credit as the verb that's going to deposit 100 pound or however much I'm actually putting in the bank account so I can see here that there's a relationship between deposit the amount of money I wish to put in and credit so in the context of this particular specification I think credit and deposit go together so I am suggesting that there might be relationship between these when I write the code I will look later to see if that prediction actually comes true now if I continue analyzing the specification I can see I now come across the word withdrawal if I carry on now you now can come across the word debit now again I am suggesting that there's going to be a relationship between the word withdrawal and debit debit is going to be the verb that suggests a method that's going to receive the withdrawal which is going to be the amount of money the customer wishes to withdraw from their bank account so this is the verb that suggests to me at this stage that I'm likely to have a method called debit and in fact this withdrawal is going to be a parameter to that particular method the next name to be highlighted is this one here what it says returned so I'm going to add that one to the list now in the context of this specification this is suggesting that I'll need some mechanism to return both the balance of the account and the name of the customer for the account being dealt with caliing on I now come to this one here which is the word changed which in the context of the specification means that there must be some mechanism of being able to change the name of the customer for example if somebody gets married and they want to change from their maiden name to their married name once you've analyzed the actual specification and decided what words you you want to extract you'll end up with a list as can be seen here now some of these words are verbs and some of them are nouns you can which I haven't done in the video create two separate lists one of nouns and want adverbs it's up to you it what you feel most happy with now I'm not suggesting that the class that I build is going to use all of these words that I've extracted from this specification this is just an approach to think well how do I take aspect of how do I get it to a class and this is just one suggestion I must say that this is looking at contrived specifications here if you're going to be designing a software system then you would have processes before this stage using unified modeling language and they would define what the classes were going to be but that's something for another video what we can see here is we have a mechanism whereby we can analyze the specification concentrating on verbs and nouns and creating this list we now use this list as can be seen here and we use the list to see if we can derive a class and I'm going to represent the class in a UML like diagram a class diagram and I'm not going to adorn it with plus signs and negative signs which define what will be private and public within the class and leave that to a later video but here what I've now got a list of words that will help me build up my class now you can't take these words out of context you still have to keep on reflecting back on the specification but I'm happy now that I've got some words that will allow me to think about it and this is a thought process here you know it doesn't just automatically come that you have a class you've got to think about it carefully which of these names are going to be methods which of them are actually going to be data fields within your class and it's entirely up to experience how well you actually perform this task using my understanding of the specification and the words listed here I can now build my class diagram the first word I'm interested in is the word account and I'm showing that here and this is going to be the name of the class that I'm building the class that I'm designing the next thing I'm going to consider what are the variables this class is going to require what are the data fields well I look at all of the lists of words here and I look at the nouns and the two nouns I'm particularly interested in is the balance of the account and the name of the customer so I'm going to put those here and these are going to form what is often called the data field of the class these are the variables are going to hold the balance and the name of the customer the next thing to do is to have a look and consider where are the verbs well is two verbs I'm interested in credit and also debit and I'm going to show those in the other part of the UML diagram as you can see here now the credit is clearly in the context of this specification a verb it's going to do something to the balance so if I put a hundred-pound into the bank the balance will go up by a hundred pound and it's this method here the credit better that's going to do that for me the debit method will obviously reduce the balance by the amount I wish to withdraw now here I've got three methods I've got get balance and get name now get balance is going to return whatever the balances of the account at any particular time and the get name is going to return the name of the customer the chef's name or what this is going to do it's going to be the method that allows me to change the name of customer now these three methods belong to the classic way in which object orientated programs are written within Python these three methods are often not use these type of methods I mean but I'll talk about that in later videos here I'm using the classic approach to the development and the design of a class and in this case the class I'm going to use is going to have these type of get and set methods and they are going to be responsible for returning and setting these data fields now if we look at the credit method if it's going to alter the balance it needs to know by how much it needs to alter the balance in other words it needs a parameter and I'm showing that parameter here as the deposit so whatever is deposited will be added to whatever the current balance actually is if we look at the debit well here of course we also need a parameter to be passed to this particular method and that I'm going to call that the withdrawal so you can see here that I have used the deposit under withdrawal which you can see were in my list up here and I did suggest earlier that I felt there was going to be a relationship between the credit and the deposit and between the debit and the withdrawal and hopefully you can see that relationship here in other words we pass in the deposit to credit and we pass in the withdrawal to debit the responsibility of this method is to allow for the changing of the value stored here and to achieve that we have to pass in a parameter and you can see that the parameter I've put here and of course this parameter is taken from here in the particular list as an aside don't let this confuse you you see this is name and this is name they're not the same thing this is a data attribute of the class and this is a per - the setname method and what will happen whatever starting here will be passed to here now we may get confused by the fact that these are both called game but the interpreter will not it will know that this is a parameter and it will know that this is a data attribute by keeping an eye on the specification and this list produced from the specification we've now produced this UML class diagram here and the approach of GC will work for any object orientated language of course these videos are on the Python programming language so I know that if I'm going to be the programmer converting my UML diagram into code I will need to have another method and the method will be this one here the initialization method and we can see that this one takes in three parameters it takes in self balance and name whenever we create an instance of this class what will happen is this particular method will execute and the value passed to balance here we have to arrange for that value to be assigned to this data field here whatever is passed to this particular parameter will be assigned here to this particular data field this particular data attribute now the self well we've seen this earlier in the playlist and self will be passed the ID of the instance being created now I'm not going to talk about self in any detail here I have three videos on this very topic earlier than the playlist so if you are sitting there wondering what self actually is for I recommend you go and look at the videos earlier in the playlist as we will be converting this UML class diagram into a Tyson class what we now need to realize that the word self that I've just been discussing will have to be a parameter to all of the methods that I'm showing here in the diagram and also self must appear before the data feels balanced and named as you can see here I've introduced self in all of the places that can clearly be seen in red and bold font now the design approach that I followed in this video has resulted in this UML class diagram and as a summary just remember what we did we read the specification we then identified the verbs and the nouns in the specification created a list of verbs and nouns used both the list and an understanding of the specification to produce this unified modeling language class diagram the next thing to do is to convert this UML diagram into code now just to show you what the code will actually look like you can see into the side here now in this particular video I'm not going to discuss this code I'm going to leave that for the next video but as a summary just think about designing the class I've shown you an approach here and that is read the spec identify the verbs and the nouns list of verbs and the nouns use both the list and your understanding of the specification to produce the unified modeling language class diagram now it is the case that I could adorn this class diagram with more information but I'm going to leave it in this more simplified state for the time being now in the next video I'm going to show you how to build the class the one that you're looking at here from a UML class diagram check out the supporting website for these videos and also consider subscribing to the YouTube channel and the Google+ circle that relates to these videos in addition why not follow me on Twitter as I issue a tweet every time I upload a new video
Info
Channel: John Philip Jones
Views: 9,628
Rating: 5 out of 5
Keywords: Designing a Python class, Verb and noun analysis, UML class diagram, Python Class, Python
Id: RZntqQgi0gM
Channel Id: undefined
Length: 19min 58sec (1198 seconds)
Published: Fri Mar 10 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.