3

I need to call function to recalculate argument from different place of program {different classes}. I need to recalculate with different coefficient which can be changed in running time. Simple example: new_value = old_value * coefficient.

At the moment I have class which hold those coefficient and has methods which are doing that recalculation. Disadvantage: I need to pass that instance of class to each place where I want to used it.

This is my singleton:

public class Converter { private double lbU_2_U; private static Converter instance = new Converter(); public Converter() { lbU_2_U = 1; } public static Converter getInstance() { return instance; } public void updateVelocityCoeficient(double lb_2_p) { lbU_2_U = lb_2_p; } public double velToP(double lbU) { return lbU_2_U * lbU; } public double velToLB(double u) { return u / lbU_2_U; } } 

so, advantage now will be that in anywhere in program I can write

newVelocity = Converter.getInstance().velToP(velocity) 

Go for forward, I would do something like this:

newVelocity = Converter.velToP(velocity) 

so I am thinking about change my Singleton to this:

public class Converter { private double lbU_2_U; private static Converter instance = new Converter(); public Converter() { lbU_2_U = 1; } public static Converter getInstance() { return instance; } public static void updateVelocityCoeficient(double lb_2_p) { instance.lbU_2_U = lb_2_p; } public static double velToP(double lbU) { return instance.lbU_2_U * lbU; } public static double velToLB(double u) { return u / instance.lbU_2_U; } } 

What do you thing? I am not sure if this is effective, if I can used this in multipleThread app, and if this is correct way of using Singleton.

thank you

5
  • Those identifiers (variable/method names) are cryptic. Please use more descriptive identifiers Commented Dec 22, 2015 at 10:36
  • Singleton is one concept, not a formal specification. Better is to understand concepts. see this: stackoverflow.com/questions/228164/… Commented Dec 22, 2015 at 10:38
  • 4
    @elif He has a programming problem (cannot achieve dynamic nature), so I don't think it would be proper for CodeReview Commented Dec 22, 2015 at 10:38
  • Stateful singletons are a bad idea. See Why is Global State so Evil? Commented Dec 22, 2015 at 10:42
  • This is not really a singleton; I would recommend to make your constructor private. Commented Dec 22, 2015 at 10:53

3 Answers 3

2

This is not a good scenario to use the Singleton pattern.

Have a look into these answers: When should the Singleton pattern NOT be used?

Sign up to request clarification or add additional context in comments.

Comments

1

You don't need this to be a Singleton - all you need is some static methods.

public class Converter { private static double lbU_2_U = 1; private Converter() { // No non-static methods so disallow construction. } public static void updateVelocityCoeficient(double lb_2_p) { lbU_2_U = lb_2_p; } public static double velToP(double lbU) { return lbU_2_U * lbU; } public static double velToLB(double u) { return u / lbU_2_U; } } 

I will not go into the discussion here on whether you are using a good or bad pattern here.

Comments

0

That's Nice. But :

-Your constructor should be private to be a Singleton. (you are the only able to create an instance)

private Converter() { lbU_2_U = 1; } 

-I dont believe you need to use a singleton. You only need a

private static double lbU_2_U; 

2 Comments

1) yes, constructor will be private, quick writing 2) well, I need to change that lbU_2_P coefficient in running program
you can change static attributes while running. "public void setlbU_2_P(double d)"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.