I have an external thermometer connected via USB that is controlled by my SW. Different parts of my system will use it but never at the same time (all in one thread). However, it is a single device communicating over an established connection so for some reason I think either its method (GetTemperature) should be static or I could do it as Singleton.I will be happy for suggestions. Thanks
Is this a candidate for Singleton?
design patterns;singleton;static methods
No, it's not a good candidate.What if you buy a second thermometer in order to get the temperature in two different locations?What if you want to create a mock or a stub of your thermometer in order to unit test parts which rely on it?With singleton, you'll end up doing something like:public class TemperatureRealTimeDisplay{    // Show the current temperature to the user.    public void Show()    {        var temperature = Thermometer.Instance.Measure();        ...    }}public class TemperatureChart{    // Record the temperature in order to display a chart over time.    public void OnTimer()    {        var temperature = Thermometer.Instance.Measure();        ...    }}...var display = new TemperatureRealTimeDisplay();var chart = new TemperatureChart();This code:Cannot be tested, since it relies on the thermometer which cannot be replaced by a mock or a stub,Is difficult to maintain or reuse given its dependency on the actual thermometer.If, on the other hand, you use instances and Dependency Injection:public interface IProbe<TResult>{    public TResult Measure();}public class HardwareThermometer : IProbe<Temperature>{    ...}public class RealTimeDisplay<TMetric>{    public RealTimeDisplay<TMetric>(IProbe<TMetric> probe)    {        this.probe = probe;    }    public void Show()    {        var current = this.probe.Measure();        ...    }}public class Chart<TMetric>{    public Chart<TMetric>(IProbe<TMetric> probe)    {        this.probe = probe;    }    public void OnTimer()    {        var current = this.probe.Measure();        ...    }}...var thermometer = new HardwareThermometer();var temperatureDisplay = new RealTimeDisplay(thermometer);var temperatureChart = new Chart(thermometer);you remove the dependencies, making it possible to use general classes RealTimeDisplay and Chart which can then be reused for anything else, like measuring the CPU load or the number of WTFs per second during a coding review. With:public class ProbeStub : IProbe<int>{    ...}they can easily be tested as well.Can't you use both dependency injection and singleton? You can, but the point of using a singleton is to being able to use the instance anywhere without having to pass it through parameters down the chain. In my example, you only instantiate thermometer once, making a singleton useless.The fact that the singleton guarantees you that only one instance will ever be created is not particularly useful in my example. RealTimeDisplay and Chart already receive an instance of a thermometer, so the incentive to create another thermometer is low. The only remaining risk is to end up with code like this in the entry point of the application:var temperatureDisplay = new RealTimeDisplay(new HardwareThermometer());var temperatureChart = new Chart(new HardwareThermometer());but it's unlikely that a maintainer will do such change.