开发者

Design Pattern to require multiple events before executing method?

开发者 https://www.devze.com 2023-03-29 23:16 出处:网络
There are many times that I\'ve needed to execute some code after a number of events have fired, and I\'ve come up with counters and such but I feel there must be a better way.

There are many times that I've needed to execute some code after a number of events have fired, and I've come up with counters and such but I feel there must be a better way.

For example, say five files need to be loaded, after which a UI component will become active.

If I set up a counter that increments each time a file is requested, then decrements each time one has loaded, I run the risk that the first two or thr开发者_如何学运维ee files may somehow get completely loaded before my code gets around to requesting the fourth and fifth, which would mean that my counter would be at zero when I still have two files to load, thus allowing the UI component to be prematurely activated.

There are some cases where you could know the number that need to be loaded before the requests go out, but it's possible that the first file contains the paths (and therefore the number of) files. (And this file-loading scenario is only an example of the pattern I'm trying to explain.)

Does anyone have an elegant solution for this? (Does my description make sense?) Thanks!


You could do something with a task framework like spicelib

Using that as an example

  • Create a FileRecursionLoadTask which grabs a file and completes when that file and any references it makes are loaded.
  • Add each FileRecursionLoadTask to a SequentialTaskGroup.
  • When the TaskGroup is completed, then you know all of the file loads have completed.

There are also plenty of other task frameworks which you might like better. For example, Spring ActionScript also has one.


Before executing a request, store a reference (a unique request uri, the loader object or a special command object) in a list. When a loader has finished, remove that object and call a function that checks if there are remaining active tasks in the list.

This isn't specific to file requests nor request in general, it can be used for anything that needs to wait for multiple actions to finish. Multiple list can be used to process multiple types of action at the same time. The object stored in the list could be implemented as a command object, which could provide more information about the task. This is called command pattern.


If you're doing just loading, like Jacob, I would also suggest a library that handles loading

If the case of a more complicated situation like mixing loaders and other event listeners, I would suggest using an event that fires whenever there is any change to any of the dependencies. In addition all the objects/classes would have a state.

Then I would create a listener adding function for the class that would need to do the function or initiate it, that would have 3 parameters

  • object with event dispatcher (assuming they all use the same update event) ie. assetLoader
  • name of object state ie. headerLoaded
  • state value's desired ie. true

the function would add the listener to a chain of listeners, and any time any of the listeners fires, all objects would check if the state value.

This would allow for regression as well (like when a user presses a button, the content starts loading, but then the user presses cancel, even if all the assets load, the state of one object would be false, thus not allowing the item to complete) If you were using counters, it would be the equivalent to adding instead of subtracting, but much more reliable.


Looking for a design pattern? Try the command pattern (http://johnlindquist.com/2010/09/09/patterncraft-command-pattern/) (The video is a great example of what command pattern is and how it works - using Starcraft as an example.

The implementation is that you queue your load commands so that they do not execute out of order, and you can add the enable or disable commands to your command que. So the command pattern will play back your commands something like: load, load, load, enable ui item, load, load, enable another item

Good luck

0

精彩评论

暂无评论...
验证码 换一张
取 消