Saturday 15 September 2012

Design pattern for dynamic C# object -


I have a line that processes the objects in the loop for a while. They are added to asynchronous anywhere ... in this way:

  myqueue.pushback (string value);   

and they are processed like this:

  while (true) {string path = queue.pop (); If (process (path)) {Console.WriteLine ("Good!"); } And {queue.pushback (path); }} Now, this is the thing that I would like to modify a TTL-like (for live living) flag, so that the file path can be added more than N times.  

How can I do this by keeping the bool process (string path) function signature I do not want to modify it.

I thought about organizing a map, or a list, which calculates how many times the process returned false for the path and left the path from the list on the false n-th return. Gave. I wonder how this can be done more dynamic, and preferably I would like TTL to automatically reduce it on every new addition in the process. I hope I'm not saying garbage probably using something like this

  class JobData {public string path; Public Small TTL; Public stable built-in operator string (JobData jobdata) {jobData.ttl--; I like the idea of ​​jobdata class, but I like the idea of ​​a jobtata class, but like the idea of ​​the jobtata class I have already shown such an answer, and the fact is that you are working with the file path , Give you another potential benefit. Some characters are not valid in the file path, and therefore you can choose one to use as a delimiter. The advantage here is that the string type is a string, and therefore you do not have to modify your existing asynchronous code. You can see a list of the reserved path characters here:  

For our purposes, I will use the percent (%) character. You can then modify your code as follows, and there is no need to change anything else:

  const int startTTL = 100; Const String Delimiter = "%"; While (true) {string [] path = quo pop (). Split (delimiter cochrane ()); Int ttl = Path.Labby & gt; 1 - int.Parse (path [1]): startingTTL; If (process (path [0])) {Console.WriteLine ("Good!"); } And if (TTL> 0) {queue.pushback (string.Format ("{0} {1} {2}", path [0], delimiter, TTL)); } Else {Console.WriteLine ("TTL expires for path: {0}" path [0]); Again, from the point of view of pure architecture, a class with two properties is a better design ... but from a practical perspective: this option means that you can avoid returning and changing. Other asynchronous codes that push the queue still need to know about the code, and will work with this unconnected.  

One more thing I want to say is that it is likely to run with the CPU core, it is a very strong loop. In addition, if it is. If the net queue type and your tight loop overtake your asynchronous, then you will throw an exception to empty the queue, which will be out of the (true) block, you can solve both issues with code in this way :

  while (true) {try {string {] path = queue.pop (). Split (delimiter.ToCharArray ()); Int ttl = Path.Labby & gt; 1 - int.Parse (path [1]): startingTTL; If (process (path [0])) {Console.WriteLine ("Good!"); } And if (TTL> 0) {queue.pushback (string.Format ("{0} {1} {2}", path [0], delimiter, TTL)); } Else {Console.WriteLine ("TTL expires for path: {0}" path [0]); }} Hold (invalid operation exception ex) {//Queue.Dequeue throws invalid operation if queue is empty ... before sleeping for a while before trying again. Sleep (100); }}    

No comments:

Post a Comment