Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It depends on who knows that the methods should be called that way.</p> <ul> <li><p><strong>Consumer knows:</strong> For example, if the object is a <code>Stream</code>, usually the <em>consumer</em> of the Stream decides when to <code>Open</code>, <code>Read</code>, and <code>Close</code> the stream. Obviously, these methods need to be public or else the object can't be used properly. (*)</p></li> <li><p><strong>Object knows:</strong> If the <em>object</em> knows the order of the methods (e.g. it's a <code>TaxForm</code> and has to make calculations in a specific order), then those methods should be private and exposed through a single higher-level step (e.g. <code>ComputeFederalTax</code> will invoke <code>CalculateDeductions</code>, <code>AdjustGrossIncome</code>, and <code>DeductStateIncome</code>).</p></li> <li><p>If the number of steps is more than a handful, you will want to consider a <code>Strategy</code> instead of having the steps coupled directly into the object. Then you can change things around without mucking too much with the object or its interface.</p></li> </ul> <p>In your specific case, it does not appear that a consumer of your object cares about anything other than a processing operation taking place. Since it doesn't need to know about the order in which those steps happen, there should be just a single public method called <code>Process</code> (or something to that effect).</p> <hr/> <p>(*) However, usually the object knows at least the <em>order</em> in which the methods can be called to prevent an invalid state, even if it doesn't know <em>when</em> to actually do the steps. That is, the object should know enough to prevent itself from getting into a nonsensical state; throwing some sort of exception if you try to call <code>Close</code> before <code>Open</code> is a good example of this.</p>
 

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