Interpreter Design Pattern

Поділитися
Вставка
  • Опубліковано 27 гру 2024

КОМЕНТАРІ • 42

  • @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 років тому

    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 років тому

    Great! I love to combine a bunch of things into one tutorial. I'm glad you liked it :)

  • @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

  • @trejkaz
    @trejkaz 10 років тому +3

    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 років тому +2

      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!!!

  • @Vendettaaaa666
    @Vendettaaaa666 10 років тому +7

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

  • @CrazySkillz15
    @CrazySkillz15 6 років тому +1

    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 років тому +1

      Thank you :) I'm happy to be of help

  • @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 років тому

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

  • @patrickkane6041
    @patrickkane6041 12 років тому +2

    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!

  • @dimasnaresh8268
    @dimasnaresh8268 4 роки тому +1

    Actually, i just mesmerize by your voice. Idk why

  • @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?

  • @derekbanas
    @derekbanas  12 років тому

    You're very welcome :) This was a fun one

  • @Mueleski
    @Mueleski 10 років тому +2

    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 років тому +9

      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.

  • @PedroOliveira-sl6nw
    @PedroOliveira-sl6nw 7 років тому +1

    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.

  • @TheDebare
    @TheDebare 12 років тому

    Another great video Derek, thanks

  • @andrecelestinodev
    @andrecelestinodev 7 років тому

    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?

  • @derekbanas
    @derekbanas  12 років тому

    You're very welcome :)

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

  • @derekbanas
    @derekbanas  12 років тому

    Don't worry there are many games in the works :)

  • @bengodw
    @bengodw 12 років тому

    Thanks very much for this core idea!

  • @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

  • @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.

  • @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 років тому +1

      +ducanh nguyen very cool. thank you!

  • @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 !

  • @herp_derpingson
    @herp_derpingson 12 років тому

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

  • @FrancoAguileradonillan
    @FrancoAguileradonillan 8 років тому

    I find your voice so masculine... pretty heartwarming

    • @derekbanas
      @derekbanas  8 років тому

      Thank you for the nice compliment :)

  • @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.

  • @أحمد-و3ز8ع
    @أحمد-و3ز8ع 7 днів тому

    Hello

  • @JasonRobards2
    @JasonRobards2 Рік тому

    This video would have been a lot more informative if it had left reflection out of it.