1

Consider a class called Effects that has a static ArrayList called effects.
The class contains a few static functions that are called across all the program, for example the static function: Effects.addEffect(effect) which adds an effect to the ArrayList effects

According to this, the class Effects has the following points:

  • Doesn't make sense to have more than one instance of this class.
  • The class functions are called globally across all the program, therefore if the class won't be static an instance should be passed across all the program!

According to good programming practices, should I make this class singleton, static or just a regular class with which I should pass an instance across all the program?

julian
  • 4,634
  • 10
  • 42
  • 59
  • 6
    It's difficult to answer, in some cases a singleton would be preferable, but testers don't like them, a class which just entirely fully of `static` fields and methods isn't much more then a singleton anyway. Having to pass an instance of `Effects` around has issues, especially for classes which don't directly need it, but which may rely on, directly or indirectly, classes which do...I don't think there is any "right" answer without more context – MadProgrammer Aug 05 '15 at 06:52
  • 1
    Consider having a look at [On Design Patterns: When to use the Singleton?](http://stackoverflow.com/questions/228164/on-design-patterns-when-to-use-the-singleton), [Use your singletons wisely](http://www.ibm.com/developerworks/library/co-single/) and [The Singleton Pattern](http://gameprogrammingpatterns.com/singleton.html) for some discussions on the subject. From I've read, where information traffic is one way (from your program to the Singleton), it is generally acceptable to use it and removing it or changing it doesn't effect the execution of the remaining code – MadProgrammer Aug 05 '15 at 07:05
  • @MadProgrammer Thanks a lot, I really liked the link from the Game Programming Patterns, I read this book a while ago, and it's definitely a great read to revisit, thanks for reminding me :) – julian Aug 05 '15 at 07:49

3 Answers3

2

It all comes down to what is "good programming practices" and what is the broader context.

For bigger, more robust solutions that require testing and mocking components, having a static class is quite hindering since it cannot be easily substituted. Some will argue that following these guidelines - avoiding static classes and making sure components are easily testable - make for better designs (TDD being a good example of this kind of philosophy).

However, note that "carrying" an instance everywhere, lets call it IEffects, can make your design "heavier" and laborious to maintain if you are not using Inversion of Control (IOC) and dependency injection (DI) for resolving dependencies.

These are some questions I generally ask myself when I face this kind of dilemma:

  • Will I ever want to switch implementations of this component (e.g. StandardEffects vs HighDefinitionEffects)
  • Will I ever want to fake or mock this component when testing other components dependent on this one?
  • Is it relatively easy/hassle-free to use an interface (coupled with a singleton in your case) instead of a static class?

Answering Yes to any of these questions generally leads to using the interface approach instead of the static class approach.

Community
  • 1
  • 1
Bryan Menard
  • 13,234
  • 4
  • 31
  • 47
  • This is a great answer, especially the questions are helpful to deal with the dilemma, in my situations the first question(switching/adding implementations) changed my perspective on the `Effects` class quite a bit! thanks! – julian Aug 05 '15 at 07:47
1

A singleton class is an object can implement interfaces you can pass around the object for delegates and callbacks. If you don't need any of these make it a static class.

maniacneron
  • 424
  • 6
  • 19
  • 1
    I initially made it a static class, but then I read that static classes are good just for utility, and my class is definitely not an utility class, but I still need the concept of having just one global instance that is accessible across all the program, thanks for the answer. – julian Aug 05 '15 at 07:00
0

As you have stated, you should have just static method in your Effect class.

You have only static variable that stores all the effects. It is still going to add the the effect in the same variable. Then why to create object for that every time you want to use? Simply add a static method instead. If you have multi-threading problem then you can handle it using Lock or synchronization. Making instance variable or instance method to access the static variable does not make sense. Are you talking about singleton pattern or Singleton class?

Nisheeth Shah
  • 600
  • 1
  • 9
  • 22
  • 4
    Why? Why would this be better then a singleton or passing a reference of `Effect`s to the classes that need it? So far, you answer is personal opinion with no evidence to it's worth - I'm such a pain ;) – MadProgrammer Aug 05 '15 at 07:06