Interpreter Design Pattern

  Рет қаралды 78,855

Derek Banas

Derek Banas

Күн бұрын

Пікірлер: 43
@derekbanas
@derekbanas 12 жыл бұрын
Great! I love to combine a bunch of things into one tutorial. I'm glad you liked it :)
@derekbanas
@derekbanas 12 жыл бұрын
For most every tutorial I draw a sequence diagram and use that to write the code. I just write out the diagram on paper. For the tutorials in on say a topic like arrays I look at the java api and write down every method I want to make sure I cover and then just write a program using each one. I use shortcuts in the real world much more than I do in videos. My object oriented design tutorial pretty much shows exactly how I work.
@derekbanas
@derekbanas 12 жыл бұрын
Design patterns definitely make it easier to make games and most anything. I'll eventually do a tutorial just on the game design patterns
@derekbanas
@derekbanas 12 жыл бұрын
There are many different forms of the interpreter pattern. Yes your way without reflection is still the interpreter pattern. The patterns are just a helpful guide and not iron clad rules you have to follow. Many if not most people think there is just one version of each pattern and that is false. I don't pay that much attention to optimizing for speed, but prefer making code readable. In the long run poorly written optimized code will get slower with each new feature
@derekbanas
@derekbanas 12 жыл бұрын
I hope to some day fix the translation issue, but I haven't found an easy way yet
@derekbanas
@derekbanas 12 жыл бұрын
I'd be interested to hear how you guys think I did it? It was all made with just the interpreter design pattern, reflection and composition. Everybody in the class liked it except for the professor :)
@srtghfnbfg
@srtghfnbfg 4 жыл бұрын
I think ik why your professor disliked it. Lots of inefficient coding, things initialised outside the constructor while not being static and i can go on for a bit.. I coded so many different interpreters in the past few years and combined a lot of them, but I cringed when u said that it ALWAYS needs reflection.. U can choose to use it but it isn't necessary for the interpreter pattern. Although u could say that it ALWAYS needs Composition, since basically any abstract syntax tree (AST) is basically just the Composite design pattern. Eventually, the interpreter pattern is just the conglomeration/combination of the composite and visitor design patterns and an optional custom context POJO. And based on how complicated the grammar your interpreter uses, u can split the interpreter object into a lexer (reads per char to build string tokens), parser (reads per tokens to build equivalent AST) and interpreter (the visitor part that reads and evaluates expression represented by AST). If you want a very well explained example (although in python) here's the link: ruslanspivak.com/lsbasi-part1/ I've personally learnt using that and wrote the equivalent interpreter using java, since the key ideas transferred are language-agnostic.
@derekbanas
@derekbanas 12 жыл бұрын
You're very welcome :) This was a fun one
@CrazySkillz15
@CrazySkillz15 6 жыл бұрын
Your tutorials provide a lot of clarity for such obscure and abstract design patterns. It helped me learn and implement it in my class. Thank you for taking the time and efforts for this informative video! Appreciate it :)
@derekbanas
@derekbanas 6 жыл бұрын
Thank you :) I'm happy to be of help
@trejkaz
@trejkaz 10 жыл бұрын
I know this isn't the focus of the demo, but the problem with this particular implementation is that to implement conversions from 5 units to 5 units, you would end up with 25 methods. I think it would pay off to introduce a Unit interface which can convert to some standard unit (like litres.) Then each Interpreter would only have to have one method, toUnit(Unit unit, double quantity) and you only have to write 2 methods and set 5 parameters for how many of the base unit everything is relative to. I did such a thing for PostScript points, millimetres and inches and it worked quite nicely. The only change I would make is to use a Rational class, because using double does mean you're "paying" for rounding errors up-front. If you introduce Rational, you can delay the rounding errors until the last minute when you want to present the result to the user (sometimes the user will be fine with rationals, of course, but decimal is more familiar.) Of course, the text adventure game use case is a much better fit for the pattern, because every expression is slightly different. this demo just suffers from choosing a set of expressions where everything is quite similar to everything else. DRY.
@derekbanas
@derekbanas 10 жыл бұрын
Yes the example isn't the best because the idea was to demonstrate the pattern in the simplest way possible. It was my judgement call that making the pattern easier to understand was the goal.
@anis.lounis
@anis.lounis 9 жыл бұрын
***** Indeed!
@jamesrao6783
@jamesrao6783 9 жыл бұрын
+trejkaz Great idea!!!
@derekbanas
@derekbanas 12 жыл бұрын
You're very welcome :)
@Vendettaaaa666
@Vendettaaaa666 10 жыл бұрын
how about a factory for Expression that spits out an impl at runtime based on users input. you wouldnt need reflection right ?
@patrickkane6041
@patrickkane6041 12 жыл бұрын
When you code, do you use shortcuts in eclipse? It seems like you copy and paste lines and individual lines very efficiently haha. Any tips would be appreciated!
@Mueleski
@Mueleski 10 жыл бұрын
Derek, first of all you are an amazing individual and I want to thank you for sharing so much of your knowledge. You are an excellent teacher and you present your information in a truly professional manner. Then I was also amazed to hear you say you made a Zork game. I played and loved the originals, is your Zork available for game play?
@derekbanas
@derekbanas 10 жыл бұрын
Thank you for the kind compliments :) I have made a bunch of games personally. For work I have had to do many simulations and would often make games during the down time. I plan on making a ton of Android games in the next 2 months. I hope everyone enjoys them.
@MX7681
@MX7681 4 күн бұрын
Good explanation. Thanks a bunch.
@PedroOliveira-sl6nw
@PedroOliveira-sl6nw 7 жыл бұрын
After so many hours watching your tutorials, I own you a big THANK YOU. Now, something I was wondering; I have recently used Zenject framework for Dependency Injection in Unity game development and it seems that the Interpreter Pattern tied with Decorator and other Creational Patterns would work really nice. . . Would you recommend the Interpreter Pattern for that? My idea is to have a main entry point that binds all the Interfaces to a Context, and when classes require some new class, they just ask the Context for it; and the context can return if it has it or make a new one.
@derekbanas
@derekbanas 12 жыл бұрын
Don't worry there are many games in the works :)
@dimasnaresh8268
@dimasnaresh8268 4 жыл бұрын
Actually, i just mesmerize by your voice. Idk why
@Macca_roni12
@Macca_roni12 3 жыл бұрын
agreed
@bengodw
@bengodw 12 жыл бұрын
Thanks Derek! A few quesitons, hope you can advise: 1. If I do not use Reflection to create instance in real-time, but I create the instances using if-then to create the corresponding instance instead and then use it (e.g. if input is "Gallons", then Gallons g = new Gallons()), does it still do the same effect? 2. Is the if-then way still Interpreter Pattern ? 3. As Reflection may slow down the program because the JVM can't optimize the code, is it better to use the if-then way?
@TheDebare
@TheDebare 12 жыл бұрын
Another great video Derek, thanks
@bengodw
@bengodw 12 жыл бұрын
Thanks very much for this core idea!
@andrecelestinodev
@andrecelestinodev 8 жыл бұрын
Hello, Derek. Amazing tutorial! Good job! I have a simple question related to the Interpreter itself. I was thinking about using it to "decode" the numbers of a bank slip to show the name of the bank, the currency, due date and amount to the user. Is it a good scenario to use this pattern?
@fadwasadkaoui6383
@fadwasadkaoui6383 12 жыл бұрын
i have lot of dificulties to understand all what you say because it's in english but almost i undesrtand the code because it''s tecnical language hahaha thak you
@jprondonp
@jprondonp 9 жыл бұрын
I think you could make and explanation about grammars and showed up the strcuture. anyway I found your program very interesting. Thanks
@aysikl
@aysikl 10 жыл бұрын
Same issue as wjrasmussen here I made a new project for the design patterns, then I'm adding a package for each pattern. - And thus have to add " package interpreterPattern; " fx. to all the files but it makes it all a bit easier to deal with :) .. Except for when it causes some sort of issue, like now.. Earlier alike troubles have been easy to fix by either referring to the file in another package by adding prefixstuff or by copying the files from old pack to new.. but this time.. :/
@wjrasmussen666
@wjrasmussen666 10 жыл бұрын
I found my problem, I never use the default java package and add my own package. Creating the project and using the default java package worked.
@davix4210
@davix4210 9 жыл бұрын
Hello Derek,thank you for such tutorial, I've learned a lot. However, error prompt when I run the MeasurementConversion: ------------------------------------------------------- java.lang.ClassNotFoundException: Gallons 2.0 at java.net.URLClassLoader$1.run(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Unknown Source) at Interpreter.MeasurementConversion.main(MeasurementConversion.java:31) ------------------------------------------------------- the code of line 31 is: ------------------------------------------------------- Class tempClass = Class.forName(fromConversion); ------------------------------------------------------- is it because the version of the jdk ? thx !
@raulzuniga4382
@raulzuniga4382 9 жыл бұрын
So I'm getting this error: java.lang.ClassNotFoundException: Pints at this line in the main program: Class tempClass = Class.forName(fromConversion); So I read where one of the other students moved his files under the 'project_name\src' directory instead of a subdirectory under 'src'. That fixed it. But I have each tut in its own package under 'src'. - project_name - src - interpreter But the reflection api seems to have a problem with this. Why? I'm not terribly familiar with Java package structure but it seems valid to me. I haven't had any problems with any of the other tuts. Seems like a bug in the api to me.
@pramalvi
@pramalvi 9 жыл бұрын
+Raul Zuniga same here. Did you find the issue ?
@AnhNguyen6368-md
@AnhNguyen6368-md 8 жыл бұрын
+Pradeep Malviya Class tempClass = Class.forName("package."+fromConversion); adding the package's name before class's name then you ll get through it
@raulzuniga4382
@raulzuniga4382 8 жыл бұрын
+ducanh nguyen very cool. thank you!
@herp_derpingson
@herp_derpingson 12 жыл бұрын
This can be used as video game consoles e.g. /give item 147
@BradenBest
@BradenBest 8 жыл бұрын
Shouldn't that line be word[0] = word[0].toUpperCase();? Or at least word[0] = Character.toUpperCase(word[0]); assuming Java acts as a sane programmer expects it to, either of those would change only a single character and are easier to read than that monstrosity of a line you just wrote.
@JasonRobards2
@JasonRobards2 Жыл бұрын
This video would have been a lot more informative if it had left reflection out of it.
@FrancoAguileradonillan
@FrancoAguileradonillan 8 жыл бұрын
I find your voice so masculine... pretty heartwarming
@derekbanas
@derekbanas 8 жыл бұрын
Thank you for the nice compliment :)
@أحمد-و3ز8ع
@أحمد-و3ز8ع 27 күн бұрын
Hello
Make Java Executable
4:43
Derek Banas
Рет қаралды 215 М.
Visitor Design Pattern
13:31
Derek Banas
Рет қаралды 281 М.
Try this prank with your friends 😂 @karina-kola
00:18
Andrey Grechka
Рет қаралды 9 МЛН
小丑教训坏蛋 #小丑 #天使 #shorts
00:49
好人小丑
Рет қаралды 54 МЛН
Quando eu quero Sushi (sem desperdiçar) 🍣
00:26
Los Wagners
Рет қаралды 15 МЛН
Decorator Design Pattern
12:57
Derek Banas
Рет қаралды 429 М.
Template Method Design Pattern
14:05
Derek Banas
Рет қаралды 149 М.
Mediator Design Pattern
18:31
Derek Banas
Рет қаралды 117 М.
State Design Pattern
20:51
Derek Banas
Рет қаралды 242 М.
Observer Design Pattern
22:27
Derek Banas
Рет қаралды 665 М.
Bridge Design Pattern
14:52
Derek Banas
Рет қаралды 203 М.
Abstract Factory Design Pattern
13:20
Derek Banas
Рет қаралды 500 М.
Design Patterns Video Tutorial 2
13:10
Derek Banas
Рет қаралды 389 М.
Facade Design Pattern
11:32
Derek Banas
Рет қаралды 286 М.
Proxy Design Pattern Tutorial
8:13
Derek Banas
Рет қаралды 167 М.
Try this prank with your friends 😂 @karina-kola
00:18
Andrey Grechka
Рет қаралды 9 МЛН