Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7673945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:37:40+00:00 2026-05-31T16:37:40+00:00

If I have this classes: public interface IBinaryCalculator{ double addition(double numb1, double numb2); }

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T16:37:42+00:00Added an answer on May 31, 2026 at 4:37 pm

    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:

    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;
                }
            };
        }
    
    }
    

    Then you could simply do:

    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);
    }
    

    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.

    BinaryCalculator simple = (num1, num2) -> num1 + num2;
    

    Or even inline in a method, for example

    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);
            }
        }
    

    Then you could simple provide an lambda implementation for your SAM type like this:

    Pair p = new Pair(10,11);
    double result = p.calculateWith( (num1, num2) -> num1 + num2 );
    System.out.println(result);
    

    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 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have trouble when designing classes like this class C1 { public: void foo();
I have some classes layed out like this class A { public virtual void
I currently have service classes that look something like this public class UserService :
Say, I have such classes hierarchy: public interface IRepository { } public class SomeSimpleRepository
I have this two interfaces and classes: public interface Identifiable<T> { T getId(); }
I have this class/interface definitions in C# public class FooBase { ... protected bool
I have some classes that look like this: MODEL public abstract class BaseEntity<O extends
I have the following classes public interface InterfaceBase { } public class ImplementA:InterfaceBase {
The classes: public interface Inter { ...some methods... } public class Impl implements Inter
I have this class that have a function to load other classes and create

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.