If I have this classes:
public interface IBinaryCalculator{
double addition(double numb1, double numb2);
}
public class BinaryCalculator implements IBinaryCalculator{
double addition(double numb1, double numb2){
return numb1+numb2;
}
}
Ok, the method should be static, but hey I have an interface over here. IS singleton the only answer and just have on class for a BinaryCalc? Lets say I build 10, 000 BinaryCalculator, it just have methods, does it have impact on performance, or should I use singleton.
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.
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.
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.
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.
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.
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 static factory method class where you can put all your SAM type implementations:
Then you could simply do:
Also, if you are allowed to experiment, you may like to give it a try to the JDK 8 Lambda Preview where you could write the implementation of your calculator as a lamdba expression somewhat like this.
Or even inline in a method, for example
Then you could simple provide an lambda implementation for your SAM type like this:
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 🙂