Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Favor composition over inheritance.</strong></p> <p>Solution takes into consideration that there could be another type of word that may need WordLengthSupport. </p> <p>Similarly other interfaces could be created and implemented and various word types can have mix and match of those interfaces. </p> <p>.</p> <pre><code>public class WordLength { private int length = 0; public int getLength(){return length}; public void setLength(int length){this.length = length}; } </code></pre> <p>.</p> <pre><code>public interface WordLengthSupport { public WordLength getWordLength(); } </code></pre> <p>.</p> <pre><code>public class BetterWord extends AbstractWord implements WordLengthSupport { WordLength wordLength; public WordLength getWordLength() { if(wordLength==null) { // each time word changes // make sure to set wordLength to null calculateWordLength(); } return wordLength; } private void calculateWordLength() { // This method should be // called in constructor // or each time word changes int length = // based on the variable word calculate Length.. this.wordLength = new WordLength(); this.wordLength.setLength(length); } } </code></pre> <p>.</p> <pre><code>public class BetterWordDescriptor extends AbstractWord implements WordLengthSupport { WordLength wordLength; public WordLength getWordLength(return wordLength); public void setWordLength(WordLength wordLength) { // Use this to populate WordLength of respective word this.wordLength = wordLength; } } </code></pre> <p>.</p> <p><strong>The Strategy Pattern</strong> defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.</p> <p>This solution does not use strategy pattern but can be refactored for same.</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. 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