Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would code the "Operations" you do as objects. You have the data, when you apply an operation to it, that operation is an object. the operation object holds some information of it's own (Perhaps a starting and ending value) and it knows how to format that value for output.</p> <p>When you start, you a queue of these operations--each one slightly different (possibly different classes, possibly just with internal state differences when they are created). You combine this queue with your data and pass the data from one piece in the queue to the next, or you can think of it as iterating over the queue and applying each operation to the data (which you probably pass in).</p> <p>When you are done, the operation objects each remember the history of what they did and know how to format it (for instance, "deducted %s percent for tax") and your data has been mutated to the correct value.</p> <p>This can also give you "Unwinds" where you could apply the pattern in the reverse order to back up to a previous value (Which probably has no interest to you here, but in some cases can be amazing).</p> <p>I believe this is called the "Command" pattern. It's terribly useful in any case.</p> <p>The other nice thing--the group of operations you place into the queue can be selected with data or any number of sources allowing you to adjust your calculations without any code changes in many cases.</p> <p>The classes would be terribly simple, something like:</p> <pre> public class TaxOperation { private double rate; private double amountTaxed; public TaxOperation(double rate) { this.rate=rate; } @Override public double getValue(double initial) { amountTaxed = initial * rate; return initial - amountTaxed; } @Override public String getDescription() { return "A tax of "+rate+" was applied which deducted "+amountTaxed } } </pre> <p>This seems really simple, and it is, but sometimes the simple classes do the most. Obviously the getValue operation isn't what you want, but you should be able to see how to derive what you want using that pattern.</p> <p>Also note that an "Operation" can do anything--for instance it can be amazingly good for threading (Imagine your operations take seconds or minutes, you just submit a queue of them to a processor and let them go).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload