Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I cannot be entirely sure, but it appears that you are worrying too much about the performance issues related to the creation of many instances of a given object.</p> <p>IMHO you should worry about the design first, and later on you can worry about performance if such things get to be an issue. Honestly, your class does not look like the kind of class for which you will have a million objects swimming in the heap.</p> <p>Your interface is what is called a SAM Type (single abstract method). And there are plenty of examples of them in the JDK itself. For instance java.util.Comparator and java.lang.Comparable are two good examples.</p> <p>If you define your method as static, it must be based on your design and not on the functionality or the simplicity of the task that it does. You may know your design better than anyone else and the developers of this forum can help you with good ideas or challenge your current ones which could be helpful to improve what you already have. </p> <p>The singlenton pattern that you mention has the intention to prevent the creation of more than a predefined number of instances of an given class, most typically they restrict it to one single instance. It is not evident in your design why you would like to do such thing, but worries about performance related to the number of instances does not sound like the best reason here.</p> <p>In looking ways to simplify your design you may like to use inline anonymous inner classes, instead of providing a class implementation of your interface, if you are planning to have different types of calculator, perhaps having a <a href="http://en.wikipedia.org/wiki/Factory_method_pattern" rel="nofollow">static factory method</a> class where you can put all your SAM type implementations:</p> <pre><code>public class Calculators { public static BinaryCalculator getBasicCalculator(){ return new BinaryCalculator() { @Override public double addition(double numb1, double numb2) { return numb1 + numb2; } }; } public static BinaryCalculator getSofisticatedCalculator(){ return new BinaryCalculator() { @Override public double addition(double numb1, double numb2) { //do any other sofisticated calculation return numb1 + numb2; } }; } } </code></pre> <p>Then you could simply do:</p> <pre><code>public static void main(String[] args) { BinaryCalculator simple = Calculators.getBasicCalculator(); BinaryCalculator complex = Calculators.getSofisticatedCalculator(); double result; result = simple.addition(10,11); result = complex.addition(10,11); } </code></pre> <p>Also, if you are allowed to experiment, you may like to give it a try to the <a href="http://jdk8.java.net/lambda/" rel="nofollow">JDK 8 Lambda Preview</a> where you could write the implementation of your calculator as a lamdba expression somewhat like this.</p> <pre><code>BinaryCalculator simple = (num1, num2) -&gt; num1 + num2; </code></pre> <p>Or even inline in a method, for example</p> <pre><code>public class Pair { private final double a; private final double b; public Pair(double a, double b){ this.a = a; this.b = b; } public double calculateWith(BinaryCalculator calculator){ return calculator.addition(a,b); } } </code></pre> <p>Then you could simple provide an lambda implementation for your SAM type like this:</p> <pre><code>Pair p = new Pair(10,11); double result = p.calculateWith( (num1, num2) -&gt; num1 + num2 ); System.out.println(result); </code></pre> <p>Of course, this is just a preview of the JDK 8, but hey, if you are allowed to experiment with the latest features, that'd be a really cool solution :-)</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