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

janci
  • 49
  • 6
  • Those identifiers (variable/method names) are cryptic. Please use more descriptive identifiers – Vince Dec 22 '15 at 10:36
  • Singleton is one concept, not a formal specification. Better is to understand concepts. see this: http://stackoverflow.com/questions/228164/on-design-patterns-when-to-use-the-singleton – guillaume girod-vitouchkina Dec 22 '15 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 – Vince Dec 22 '15 at 10:38
  • Stateful singletons are a bad idea. See [Why is Global State so Evil?](http://programmers.stackexchange.com/questions/148108/why-is-global-state-so-evil) – Jesper Dec 22 '15 at 10:42
  • This is not really a singleton; I would recommend to make your constructor private. – e.doroskevic Dec 22 '15 at 10:53

3 Answers3

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?

Community
  • 1
  • 1
cahen
  • 15,807
  • 13
  • 47
  • 78
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.

OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
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;
MeGoodGuy
  • 395
  • 2
  • 10
  • 1) yes, constructor will be private, quick writing 2) well, I need to change that lbU_2_P coefficient in running program – janci Dec 22 '15 at 10:52
  • 1
    you can change static attributes while running. "public void setlbU_2_P(double d)" – MeGoodGuy Dec 22 '15 at 10:55