Advertisement
_AMDG_

Task Observer Pattern

Nov 18th, 2014
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. This is a type of Observer pattern that I may or may not have come up with. Regardless, this tutorial is built of rules rather than how-to:
  2.  
  3. Rules:
  4. 1. There is a global operation thread for every task. That means , even a non-looping task, should be wrapped, as threads still run even if it only has one line of code that doesn't loop in it. It doesn't loop the code, but when you tell it to run, it will run alongside the rest of the program concurrently.
  5.  
  6. 2. Your program should be run entirely by Listeners (e.g. press a key, click a button), automated background tasks exempt (e.g. loading objects, animations, timers, debuggers, menu animations, etc)
  7.  
  8. 3. The only reason you should have a loop at all in your program is for updating the state of your objects, not schedulers as traditional, inefficient games do.
  9.  
  10.  
  11. Here is the reason why this works and why it is more efficient than a scheduler:
  12. Using a scheduler, it depends on the order of execution.
  13.  
  14. Thread main = new Thread(){
  15. @Override
  16. public void run() {
  17. while(true) {
  18. input();
  19. update();
  20. states();
  21. repaint();
  22. }
  23. }
  24. }
  25.  
  26. Using this method, you repaint when it isn't necessary, you check for things when unnecessary, and you update sometimes even thousands of objects depending on your hertz, causing major overhead for no reason at all. If nothing happens at all, your overhead should be close to absolute zero, not updating the screen when you aren't moving or a moving object is not visible. In contrast, this is easier to manage as everything is usually in one place unless you are some beginner or novice, an amateur just messing with code.
  27.  
  28. On the contrary, Task Observer Pattern is more efficient than a scheduler as you cause a lot less overhead due to the great lack of loops (except for updating positions when necessary, like press down, a loop updates every object in an ObjectTree). It can, at times be harder to manage as you have code in many different objects and places, though everything still goes to one place: your Listeners.
  29.  
  30. Must I tell you?
  31.  
  32. DOCUMENT YOUR CODE!!!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement