Interpreter Design Pattern

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
well hello internet and welcome to port 23 of my designs patterns video tutorial today we're gonna talk about the interpreter design pattern so what is the interpreter design pattern well as I was first taught about this pattern the professor said this exact line I wrote it down in a notebook the interpreter pattern is normally ignored and I've seen many places online this pattern is almost never used well I'm gonna show you in this tutorial how to do some really cool things with the interpreter design pattern however there's one caveat if you do not like Java reflection you might as well stop this video right now and stop watching it also if you are here for just a complete copy of the Gang of Four book and their interpretation and the interpreter design pattern wow I will for the most part stick to that I am going to make changes so if you don't like that fact also go watch somebody else's video if you do however want to see how to do some really cool things with the interpreter design pattern you're in the right place and let me just show you what we're gonna do so let's say you would want to make a conversion little tool here so that you could convert different types of measurements to other types of measurements well the interpreter design pattern is gonna help you do that really easily let's say you wanted to take one gallon and you wanted to convert it to tablespoons that's all you need to do and hit OK and there's your answer one gallon equals 256 tablespoons and I could go in and type in all types of other different types of measurements like I mentioned before the previous professor who had talked about the interpreter design pattern pretty much said it was worthless that night whenever I used it in combination with Java reflection I was able to create just with those two ideas I was able to put together the Zork text-based video game so if you want to see some really cool stuff and learning a whole bunch about Java reflection and a whole bunch of other things that I've received in regards to questions you guys have asked let's get into the interpreter design batter so like I said before I find it extremely useful if used in connection with Java reflection techniques and pretty useful in general it is basically just used to convert one representation of data into another representation of data what you're gonna have is mainly three pieces and this all comes from the Gang of Four book to a certain extent you have the context which contains all the information that is going to be interpreted you have the X rushon which is going to be an abstract class that's going to define all the methods needed to perform the different types of conversions and then you're gonna have the terminal or concrete expressions which are gonna provide very specific conversions on different types of data let's look at this in sort of a UML class diagram okay on the Left you're gonna have your client that's going to ask for certain information that information is going to be stored in what is called the context and you could see that right here conversion question then you're going to have all sorts of responses and different information is going to be relayed from the context over to concrete expressions that are all going to extend this abstract class and you can see a very specific example what I'm gonna do here is say okay you're gonna give me gallons in exchange I'm gonna give you either gallons courts or pints or if you give me quarts I'm gonna give you gallons courts or pints as a response so that's the basic idea of how the interpreter design pattern works so let's jump into the code okay so where I am in the conversion or the context I call this conversion context Java and I'm gonna define a whole bunch of different things inside of here it's a string conversion question there's gonna be the question they're gonna send like I showed you in the example which is you know one gallon two whatever you want to convert it to and all the code is available underneath this video it is heavily commented if you have any questions of course you can always leave questions down below then I'm gonna have conversion response and then and I've also found the need to store from conversion which would be something like gallons which is what I'm going to be converting from and then I'm also going to just store inside of here a string which is gonna be to conversion which is gonna be what I want to convert to so let's say tablespoons or pints or whatever you want and then I'm gonna create a string here string array and it's going to be parts of question it's gonna store everything in individual pieces so now I need to create my constructor here and it's going to be passed a string which is just going to be the input that is received from the user and then I'm just gonna go this actually they don't have to do that I can just do conversion question is equal to whatever the input is that's easy then I'm gonna go parts of question I'm covering a lot of things I've received questions on in this tutorial also and I'm gonna split that string into pieces based off of spaces and then get input let's just copy that right there I'm just gonna create this so it's gonna be public string get input and it's just going to return conversion question like that so there we got that all set up and then I need to get my from conversion and you can make this very very sophisticated like I said I created a dork game from that which I was gonna actually go over but I just figured it would be way too long so I'm gonna try and simplify it and then what I'm also gonna do is capitalize this string so that it is easier to work with no matter what sort of questions they ask so that means I'm gonna have to create get capitalized so let's bounce down here and go public string response get actually just paste that in there and it's gonna get a string and I'm gonna call it word to capitalize and the first thing I'm gonna do is actually put everything here that are sent to me in lowercase letters and to do that just go word to capitalized and to lower case like that and then I'm gonna make the first letter uppercase and how you do that is go character to upper case and we go word to capitalized character at the zero index position which is you know the first character in the string put a plus there and then go word to capitalized and type in substring one like that and that's going to capitalize just my first letter and then on top of that I'm gonna throw a couple other things I'm gonna put an s on the end of this so if they say one gallon two pints it's gonna work and if they say one gallons or two gallons two pints it's also going to work so to do that I'm gonna go int length of word is equal to word to capitalized I'm gonna get the length of this so that's I'm gonna get the length then I'm gonna say if word capitalized dot character at and I'm gonna type in length of word - one which is gonna get me the last character inside of this guy and then after this I'm gonna say is not equal to ass well then I want to put an S at the end of it so that we'll be able to once again to work with gallons or gallon so we're gonna go word capitalize is equal to new and to insert a character into the end of the string I'm gonna use a string buffer and go word to capitalize and then after that go insert and then we're gonna say length of word call them in there and then I'm gonna say that I want the index the very last index in this string to be changed to s and then I have to call to string and input that there so that's going to insert the letter S at the end of the string if it doesn't exist which we checked right there that's kind of neat and then after we've done all of that we're gonna go return word capitalized and there it is it's all setup there's a whole bunch of different things we just learned so that's kind of cool let's give myself some space so there's get capitalized and then I'm gonna do two conversion which is going to be what do we want to convert from two so we want to go from gallons to pints okay well that's what two conversions gonna have inside of it so we're gonna go get lower case and we're gonna pass it parts of because I know what type string they're gonna be sending me we could change this to three like that because I know what they're gonna be sending me is gonna be something to the extent of one gallons two pints okay so one is going to be this guy say gallons right there and then three of course is gonna be zero one two three right there so that's gonna be what I need to convert it to okay well I'm using something else here get lowercase so I need to create that let's just get that out of the way there and I'm just gonna bounce right here and go public in a coffee get lowercase it's gonna return a string get lowercase it's gonna receive a string and it's gonna be word to lowercase so we're just gonna change this to lower case like that and then let's create our little method here and it's gonna be real simple I'm just gonna go return word to lowercase and go to lower case so pretty simple and let's save that so that we got that all set up so then we're gonna bounce up here again get some more information we want to also get the quantity and we want to convert this into a double because it's a string right now so we're gonna go value of and then this is going to be parts of question and the quantity is going to be the very first value passed remember one gallons two pints so the first index is gonna be zero and then I'm gonna zoom up here and go double quantity and then let's make this private as well and then we're gonna go conversion response is equal to parts of question and they're basically gonna kick out the same thing it's gonna say whatever like if we say 1 gallons two pints it's gonna say one throw space inside of there and then we can spit out either this or we could do that whatever let's just throw that in there so that's going to be one gallons and then after this we're gonna go equals and then later on we're gonna put the rest of our conversion information in there so then we need to output and return some different information in regards to let's say we want to give them the from conversion well get and I'm just gonna change this to from conversion and it's just going to return from conversion which is going to be right here copy that bounce down paste that there and then we're also going to return to conversion so there's two conversion and then we're just going to return teo conversion and then we could also return get response maybe and then this is gonna return conversion response which is right there if you haven't been what I figured out yet there's a whole bunch of things being covered in this tutorial aside from just the basic pattern I could definitely see why we're gonna need to get the quantity so let's just return the quantity and that's all we're gonna need so there we are we're gonna file save that and that is conversion context dot Java that we got set up there so the next thing we want to do is go into expression dot Java and define all the different methods or things that we're going to be converting into so here's expression job and like I said it's an abstract class and you could make this list a mile long and be able to convert into just about anything so we're gonna go string now let's say I want to be able to convert to gallons so we're just going to go double and it's going to receive a quantity so we got that all set up then we have to think about all the other different things that we might want to convert to so we might also want to convert to quarts so there's quarts we also might want to convert to pints there's pints you might also want to convert to cups and then finally let's convert to tablespoons and then we aren't we're done with that so that is the expression you're basically going to define all of the things that the concrete expressions are going to be able to convert into which would be gallons quarts pints cups and tablespoons like I said this could be a mile long and being able to do really insanely awesome things so now we're going to implement that inside of gallons Java you can see here public class gallons extends expression we're gonna go here zoom in on this and it's going to say hey you need to implement some methods okay fine do that for me because I'm lazy and there we are now we got all our methods we need so basically if we're gonna be converting into gallons what do we need to return well gallons into gallons is equal to gallon so we're just going to return whatever quantity they send to us so that's real easy however we're gonna want to convert this into a string so we're gonna go double to string like that and then close that off so that's gonna convert that double that's sent over and with string and then we're gonna basically have to do that for everything else that we have here let's just give it up all those pasted that in there and then it's just a matter of doing the conversion which is about math and it's not really about programming but you Williams do it right so how many gallons goes into quarts well basically you're just gonna come in here and multiply quantity times 4 here for pints we're going to multiply this times 8 and then for cups we're gonna multiply that time 16 and then tablespoons we're gonna multiply that times 256 and there you go we've just defined our conversions so we're done I'll save okay so now that that's all out of the way let's go into measurement conversion dot Java and let's start playing around with reflection as you can see here this is the library and we're gonna use a couple little swing tools just to help us along so that we have our little pop-up that shows on the screen so the first thing we're gonna do is we're gonna go J frame frame is equal to new jframe so that we can open our little pop up on our screen and then we're going to ghost in question ass is equal to J option pain which is our little pop up whenever we put in show input I log then we just need to pass it the frame that we want to use and the question we want to ask which is what is your question and there we go now we're gonna get our string which is going to contain the question that it's gonna be up to us to be able to translate this into an answer so then we're gonna go conversion context which is the very first class that we created and we're gonna go question is equal to new conversion context which is gonna store remember the initial response or information that is sent to us and we're gonna pass that question asked and it's gonna be its job to parse it and put it in two different forms that we're gonna be able to use well then we're gonna go string from conversion which is going to find the object class that we're going to be converting from say in this situation we're gonna be converting from gallons to either gallons quarts pints cups or tablespoons so from conversion means that we're referring to this class right here so let's go into that and to get that we're just gonna go question get from conversion we're gonna call that method we created earlier and open that up well then we're gonna need some other information like what are we gonna be converting to or the to conversion now to conversion is gonna be a reference to the method we're going to be calling we're gonna be using reflection again and I'll put a link to a reflection tutorial below if you don't know what that is and there that is so that's going to be this is going to be the class object we're going to be calling and this is going to be the method we're gonna be calling within that class object so get to conversion and for the most part like I said that we're sticking to the basic Gang of Four interpretation of the interpreter pattern but I'm adding up a whole bunch of other functionality to make this really cool well then we're gonna go double quantity we need to get our quantity that we are going to start with so get quantity is gonna do just that give me that and then we need to get the class based on the from conversion strength or the class object so how do we do that well we class temp class and then we're gonna use our conversion cool stuff for name and if we want to call the class well we just want to pass it from conversion which is going to be a reference to the class object that we are converting from which is in this situation gallons like I said before so that's the class object we want to convert from and it's gonna give us an error message saying this needs to be surrounded with a try-catch block so let's just surround with the try-catch block let them do all the work for us and then we're going to get the constructor dynamically for the conversion string and using reflection we're just gonna go and create a constructor object and to call the constructor on our class we just go temp class get constructor and it doesn't have any parameter types so we can just leave it like that and it's gonna give us another message it says hey we need to add some things to our try-catch block so let's just let it do that and just say add catch clause just surrounding try keep everything nice and tidy and it did that for us so that's cool now we need to create a new instance of the object that you want to convert from and to do that you're gonna go object convert from and we know this is going to be an expression because all of these extend the abstract class named expression so we want to tell it that because that always is going to apply then we're gonna go Colin which is the reference to the constructor and go new instance and there are no arguments for the constructor so we can just leave it that way and what's it gonna do it's gonna say hey we need to add some more caches to our to our catch block add catch Clause to surrounding try click on that we're gonna do that again so come back up here so we know what class we're going to be using we know what constructor and we also created an object of that class type so now what do we need to have to do is to find the method parameters that are gonna be expected by the method that was going to convert from the original chosen unit of measure to the new one so let's just go over here what is gonna be sent to this a double that's all that's gonna be sent to all of the methods that we're going to be calling on so we need to come in here and define that fact to do that we're gonna go class create a class or a method parameters when you get a new class like this and then we're gonna pass it a double so we need to go double type and I talked about the other types but that's integer and all these other things it's just basic common sense to figure what type of class we're gonna be passing there's a double and that's what we're gonna be passing then we need to get the method dynamically that will be needed to convert into our chosen unit of editor so we're gonna go and create a method object conversion method is gonna be its name and to get that we're gonna go a temp class get method which is another reflection tool and if we want to figure out what method we want to get well we want to figure out what do they want to convert into and to conversion is a string that contains that name and that is what we're gonna put inside of there so there's two conversion and what's gonna be passed to the method well this guy right here so we're gonna go method parameters and copy that guy and paste in method parameters so this is gonna be the parameter type which is gonna be a double which we defined above and if you want like I said the codes underneath this video well then we need to define the method parameters that will be passed to this above method that we just got ahold of and to do that we're gonna go object and I'm just gonna call this params is equal to new object and then we're going to pass it an actual double value and what's that double value we're passing the quantity that is stored inside of here so there we're actually passing a real or there we're defining that we're gonna pass a real double quantity or value over to our new method that we just created and then we just have to get the quantity after the conversion which is real simple we're gonna go string to quantity and then we're gonna have to convert this into a string so there's the cast conversion method we're gonna call that method and how do we call a method using reflection we use invoke and then we're going to say okay well what object well the objects name remember gallons so how do we get this object it's actually stored right here say convert from so we have to get the object that we're gonna be calling the method for and then inside of that we have to pass the actual values or the double that we want to pass to that very specific method so this is powerful stuff might be a little bit harder to wrap your head around sometimes but it's really cool whenever you do and then we're gonna go string and we're gonna go answer to question we're done all the hard stuffs done so if you have any trouble with any of this chances are it's this stuff right here any other reason you're having trouble with it as you don't understand how reflection works if you study this you will you'll know everything you want to know about reflection so then we need to craft our answer to the question which is gonna be pretty simple we're just gonna go question and get response which we created earlier which is gonna be the beginnings of this so let's just go over here see get response is right there so conversion response and where does conversion response come from up here see conversion response which is gonna spit out this guy right here so if it's 1 gallons two points what's this gonna spit out well it's gonna spit out one and then what's it gonna spit out parts of question gallons and it's gonna say equals and I need so equals right now be honor so that's what's gonna come out and there it is that's are going to be the beginning of our response to our answer let's save that jump back over into measurement so that's what this guy's gonna do so let's store that in our string here actually is just go in here and go plus and then I'm gonna go to quantity is this guy right here it is the new converted whatever you're converting into this is going to be a number and then we'll put a space in there and then another place and then we're going to define what are we converting to well to conversion is what we're converting to clothes I don't close that off and there we go so that's our answer that we're gonna be shooting out and then all we need to do is shoot our answer out so we'll go J option pain show message dialog and we're just gonna go null in this situation and then the message we're going to provide is answer two questions so let's paste that in there and then after they click OK if we want everything to shut down meaning the program altogether we're just going to get this done the quick way we're just gonna go frame dispose and then we are so when they hit OK it's gonna give them the answer it's gonna do all the things we wanted to do and it's also gonna curl those the program whenever we are done now of course I also went in and created court star Java which is almost identical it into gallons da Java and pints and cups and tablespoons there's all that information let's so let's run it execute and then as you can see this little box pops up here and if we go one pints or let's just say pint two table spoons it okay one pint equals 32 tablespoons and if we zoom out and hit okay you see this goes away and there it goes so here's how to do some really cool things with an interpreter design pattern leave any questions or comments below or the last til next time
Info
Channel: Derek Banas
Views: 66,603
Rating: 4.7590361 out of 5
Keywords: Interpreter Design Pattern, Interpreter Pattern, Reflection, Java Reflection, Design Pattern Tutorial
Id: 6CVymSJQuJE
Channel Id: undefined
Length: 22min 22sec (1342 seconds)
Published: Thu Oct 25 2012
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.