Interpreter Design Pattern

[ad_1]
Code is Here:

Welcome to my Interpreter Design Pattern Tutorial! The Interpreter design pattern is ignored by most, but in this tutorial I’ll show you how to do some cool things with it.

In top of that, I’ll also answer all of the recent questions I’ve received about both Java Reflection and String manipulation. I found you can do pretty great things if you combine reflection with the interpreter design pattern.

All of the code in the link above is heavily commented to help you learn.


Posted

in

by

Tags:

Comments

24 responses to “Interpreter Design Pattern”

  1. Pedro Oliveira Avatar

    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.

  2. André Celestino Avatar

    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?

  3. Braden Best Avatar

    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.

  4. Franco Aguilera Avatar

    I find your voice so masculine… pretty heartwarming

  5. davix Avatar

    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 !

  6. Raul Zuniga Avatar

    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_namesrc' 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.

  7. cotarren Avatar

    I think you could make and explanation about grammars and showed up the strcuture. anyway I found your program very interesting. Thanks

  8. Vendettaaaa666 Avatar

    how about a factory for Expression that spits out an impl at runtime based on users input. you wouldnt need reflection right ?

  9. Mueleski Avatar

    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?

  10. trejkaz Avatar

    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.

  11. aysikl Avatar

    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.. :/

  12. wjrasmussen666 Avatar

    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.

  13. Derek Banas Avatar

    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.

  14. Patrick Kane Avatar

    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!

  15. Derek Banas Avatar

    I hope to some day fix the translation issue, but I haven't found an easy way yet

  16. fadwa sadkaoui Avatar

    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

  17. Derek Banas Avatar

    You're very welcome 🙂

  18. wong benson Avatar

    Thanks very much for this core idea!

  19. Derek Banas Avatar

    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

  20. wong benson Avatar

    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?

  21. Derek Banas Avatar

    You're very welcome 🙂 This was a fun one

  22. Allen Law Avatar

    Another great video Derek, thanks

  23. Derek Banas Avatar

    Design patterns definitely make it easier to make games and most anything. I'll eventually do a tutorial just on the game design patterns

  24. Herp Derpingson Avatar

    This can be used as video game consoles e.g.
    /give item 147

Leave a Reply

Your email address will not be published. Required fields are marked *