Welcome to my State Design Pattern Tutorial! I explain how the state pattern is used by using it to simulate an ATM machine.
I explain how you come to decide on the different states. I then show you how to design the interface that each state will use. We think about the methods that are needed for every class that implements the interface then.
All the steps are looked at from many directions and the code below will fill in the gaps.
If you like videos like this it helps to tell Google [googleplusone]
Share if you think others may like it [nttfblike]
Code From the Video
ATMSTATE.JAVA
ATMMACHINE.JAVA
HASCARD.JAVA
NOCARD.JAVA
HASPIN.JAVA
NOCASH.JAVA
TESTATMMACHINE.JAVA
hi how r you ? could you please tell me how to add an twitter account for twitter stream?
Sorry, but I need more information. What exactly are you trying to do. It’s nice to see you back again 🙂
No Comments??
My website isn’t very popular, but I enjoy it with the few in my community 🙂
Great things are rarely appreciated just the nature of the beast. The site is awesome. The game I’m trying to make uses states so i wanted to understand them better. So i watched this twice. good vid!
I should have said fully appreciated. There definately seems to be alot of people here that do. Just find it hard to believe this site is’nt popular thats all.
I was wondering if you could touch on hierarchical state machines if you get time.
Thank you 🙂 Yes my site isn’t that popular, but that is ok. I like the people that find me. I’ll be covering design patterns again in my refactoring tutorial. It is coming up after I finish with Object Oriented Design
Thank you 🙂 I enjoyed covering design patterns and I’ll revisit them in my refactoring tutorial which should be starting very soon. Thanks for stopping by my site
k thanks…. i found what i was doing wrong… just a simple mistake… i thought i maybe i needed to use a diff approach to state design because the lib im using works with them but really i just needed to look closer.
So basically your awesome and your code is bulletproof, i just forgot an important statement heh… thnx as always
Thank you very much for the compliment 🙂 I do my best
Great! I’m glad you got it working. I figured it was just a simple error
Hello,
nice tutorial
I watched some of your previous tutorial and after reading about state design pattern , it came to my mind that we can include singleton design pattern to ATMMachine , so that not more one instance of ATMMachine() is created .This is also true in real world Atm application.
is my thinking correct or am i thinking too much ?? 😛 😉
That is a good point. I try to focus on just one subject at a time so I don’t confuse people
yeah it is good to keep stuff simple and easy to understand.My thought on this came up after watching your previous tutorial.So this shows that your previous tutorial is helping me in understanding and following different approaches…which is gr8 🙂 :).
Thank you 🙂 Stick with it and it will all make sense very soon. I’m glad you are finding the videos useful
Sir Thanks a Lot For Great work you are doing.
the way you are teaching is very easy to understand
i don’t have so much money to go and learn java outside
you are saving me lots and lots money
You are very very welcome 🙂 I as well didn’t have money to attend college and I made this site for people like you and I. I’m extremely happy to be able to provide a free education to the best of my ability.
hi, thanks for the great tutorial
i am making a ATM simulation . i am also using uml diagram . now that i have a class diagram i wanted to use the state pattern for it, but my coding is completely different from yours, can you help me ?
You’re welcome 🙂 This isn’t by any means the only way to create an ATM simulator. I’m not the type of person that says if you don’t do it my way you are wrong. If your way works then go with that. Feel free to ask questions, but if your code works and it is understandable then it is good in my opinion
Thanks for the reply.
yes it works,but i can’t figure out a way to use state pattern in my UML class or my coding. that is my problem.because i have totally different classes.but still i will try to make it work.
i love every single video that you have..so clear..thank you
You are my hero! 🙂
Thank you 🙂
very nice …
you are the real Hero…
I am requesting you.. can u upload web services plz…
thank u….
Thank you 🙂 I am planning a web services tutorial now. I’ll get it up asap
You have always been providing in depth and concise information, through very simple demos .. I really appreciate your good work on Design pattern . Kindly keep this good work up and running as you are obviously helping less knowledgeable people like me 🙂
Thanks again
Thank you very much 🙂 I do my best to make everything understandable and interesting. Those are my main goals. I also struggled with the same bad textbooks that you probably have seen. I always wanted to do what ever I could to make these topics understandable.
Nice clean code, very good example….
If I was to adapt the structure for something else…how do you feel would be the best way to incorporate something like a sub state in one of those states….say
Counting state (increments until hits a target then changes state to Execute state (which allows you to do something, then reverts back to Counting until reaches a target again)
Thanks 🙂
Also the execute state does not preventing other items counting so several items counting in parallel, but cannot do anything until reached the target…but the user could choose to do nothing on one of the items so a later item could reach its target and the user could choose to do something with it before an item which hit its target earlier…if that makes sense
🙂
great job !
clear and useful 🙂
Thank you 🙂
Hi!! I’m C++ programmer, but your video tutorials for patterns are the best I’ve ever seen so far, so I’ve been following them, and I review them whenever I need to implement a pattern (for C++).
Your explanations and source code are clean and easy to digest; however, I think a FSM diagram is needed in order to see when and why a transition is needed. Of course reading the source code gives that information, but you know, an image says more than a thousand words.
Thank you and keep going!!
Hi Xavier,
Thank you 🙂 It is nice to know that these tutorials are helping people. Especially if you like them enough to use as a reference. that is very cool. Yes I should go in a see if I can add anything to improve them. I’ll set some time aside soon.
Derek
Hi,
I’m studying CS in a Finnish uni, and took a design patterns course. After banging my head against the wall with the aid of 3-4 books ( a bit like this: http://www.youtube.com/watch?v=7qHL2PSpecI&t=3m02s ) I discovered your tutorials and I am more than glad for this coincidence.
Writing the code alongside your tutorials is the best learning routine I’ve come up with so far. It shortened the learning experience of getting from theory to practice from 2-10hrs with the book to 15-30mins with your tutorial. It’s such a good method, I’m undecided on should I tell everyone about this or should I keep it my secret for the time being 😉
To other ones that might be reading this, GoF’s book, Head First Design Patterns and maybe Java Design Patterns vol 1 with Derek’s tutorials is a guaranteed way to go about these patterns – in case that the tutorials left any questions. Just sharing..
A huge thanks to you!
I’ll be checking your uml, refactoring and android dev tutorials next.
-r
Hi,
Thank you for all of the nice compliments 🙂 I very much appreciate them. I did my best to translate the GOF book into a series of easy to understand videos. I struggled with them as well many years ago.
I wish you all the best
Derek
Thank you very much … very nice tutorial on design patterns
You’re very welcome 🙂
Derek,
Thank you for all of the good work you put to create these videos. Could you please provide UML class diagram for this video? I don’t know how I can show ATMState hasCard, noCard, hasCorrectPin, atmOutOfMoney in ATMMachine class
You’re very welcome 🙂 Sorry, but I don’t have one. I have a class diagram tutorial here that may help.