Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Is this way comfortable for you ?</p> <pre><code>public class AreaCalculator { private int length = 10; private int width = 10; private static final AreaCalculator areaCalculator = new AreaCalculator(); private AreaCalculator() { // TODO Auto-generated constructor stub } public static AreaCalculator getInstance() { return areaCalculator; } public AreaCalculator fillLength(int length) { this.length = length; return getInstance(); } public AreaCalculator fillWidth(int width) { this.width = width; return getInstance(); } public AreaCalculator fillAll(int length, int width) { this.length = length; this.width = width; return getInstance(); } public int calculateArea() { if (!isInit()) { throw new RuntimeException("Should Init first!"); } return length * width; } public boolean isInit() { // add something return true; } </code></pre> <p>Test Code</p> <pre><code>public class TestMain { public static void main(String[] args) { int result = AreaCalculator.getInstance().fillWidth(20).calculateArea(); System.out.println("10*20=" + result); result = AreaCalculator.getInstance().fillWidth(10).fillLength(10).calculateArea(); System.out.println("10*10=" + result); result = AreaCalculator.getInstance().fillAll(10, 30).calculateArea(); System.out.println("10*30=" + result); } </code></pre> <p>**</p> <blockquote> <p>Using Config interface.</p> </blockquote> <p>**</p> <p>ConfigInf(lib side)</p> <pre><code>public interface ConfigInf { public static String F_LENGTH = "length"; public static String F_WIDTH = "width"; public String getProperty(String key); } </code></pre> <p>ConfigFactory(lib side)</p> <pre><code>public class ConfigFactory { private static ConfigInf config = null; public static void init(ConfigInf config) { ConfigFactory.config = config; } public static ConfigInf getConfig() { if (config == null) { throw new NullPointerException("Config should be init first"); } return config; } } </code></pre> <p>ClientConfig (client side)</p> <pre><code>public class ClientConfig implements ConfigInf { String value = "10"; @Override public String getProperty(String key) { // read configFile if (ConfigInf.F_LENGTH == key) { return value; } else { return "100"; } } public void update() { value = "100"; } } </code></pre> <p>AreaCalculatorWithConfig(lib side)</p> <pre><code>public class AreaCalculatorWithConfig { private int length = 10; private int width = 10; // private static final AreaCalculatorWithConfig areaCalculator = new AreaCalculatorWithConfig(); private AreaCalculatorWithConfig() { // TODO Auto-generated constructor stub } public static AreaCalculatorWithConfig getInstance() { return areaCalculator; } public int calculateArea() { ConfigInf config = ConfigFactory.getConfig(); length = getInt(config.getProperty(ConfigInf.F_LENGTH)); width = getInt(config.getProperty(ConfigInf.F_WIDTH)); return length * width; } public static int getInt(String value) { return Integer.parseInt(value); } } </code></pre> <p>TestMainWithConfig (client side)</p> <pre><code>public class TestMainWithConfig { public static void main(String[] args) { // init; ClientConfig config = new ClientConfig(); ConfigFactory.init(config); // System.out.println("Before update:" + AreaCalculatorWithConfig.getInstance().calculateArea()); config.update(); System.out.println("After update:" + AreaCalculatorWithConfig.getInstance().calculateArea()); } } </code></pre>
    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. 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