Interpreter Design Pattern

preview_player
Показать описание

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.
Рекомендации по теме
Комментарии
Автор

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

derekbanas
Автор

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 :) <3 :D

CrazySkillz
Автор

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
Автор

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

Vendettaaaa
Автор

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
Автор

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.

trejkaz
Автор

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
Автор

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

derekbanas
Автор

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

derekbanas
Автор

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!

patrickkane
Автор

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 :)

derekbanas
Автор

Actually, i just mesmerize by your voice. Idk why

dimasnaresh
Автор

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?

bengodw
Автор

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?

Mueleski
Автор

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.

PedroOliveira-slnw
Автор

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

fadwasadkaoui
Автор

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

jprondonp
Автор

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

aysikl
Автор

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

derekbanas
Автор

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.

wjrasmussen