Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It's difficult to provide a detailed answer without more information but one approach you could consider is:</p> <ul> <li>Remodel a product using a <code>Product</code> class hierarchy rather than representing each product as a <code>Map</code> of (name, value) pairs. The current Map representation means that many errors are not going to be detected until run-time; better to prefer stronger typing so you can perform more checks at compile-time.</li> <li>Define a template method on the top-level Product class or interface that performs much of the processing currently defined in your 500 line method. For example, you could pass in a visitor to the template method as per the below code extract.</li> </ul> <p><strong>Example</strong></p> <pre><code>/** * Our visitor definition, responsible for processing each product. */ public interface ProductVisitor { void processProductA(ConcreteProductA a); void processProductB(ConcreteProductB b); } /** * Top-level product definition. */ public interface Product { void process(ProductVisitor v); } /** * A conrete product implementation. Makes a callback to the visitor * allowing itself to be processed. */ public class ConcreteProductA implements Product { public void process(ProductVisitor v) { v.processProductA(this); } } /** * As per the previous product implementation but makes a *different* * callback to the visitor. */ public class ConcreteProductB implements Product { public void process(ProductVisitor v) { v.processProductB(this); } } </code></pre> <p>The main advantage with this approach is that you avoid lots of explicit if-then blocks in your code making the design more loosely coupled. The disadvantage is it can make code less readable.</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