Tag Info

New answers tagged

-1

Static classes almost always a bad smell because they make unit testing hard. You can't make mock or make test versions of a static class. If any of your code is a client of a static class, then there is tight coupling between the two classes. Instead of static classes, you should be using a normal object but manage it with an Inversion of Control ...


1

yes, to create a Singleton class, you have to use the private constructor as it is the only way to prevent another class from creating an instance of your class. public class Singleton { private static Singleton instance = null; private Singleton() { } public static synchronized Singleton getInstance() { if (instance == null) { ...


2

Disadvantages are that: it is not obvious where it is used when you use something that uses it, you cannot supply alternatives (esp. when testing, you do not want to use eg. real credit card payment) when you decide that you want two contexts in which it should be unique you must rewrite every usage. These disadvantages are sometimes acceptable, ...


2

on of the biggest misconceptions that I see is that lazy instantiation is needed for all singletons, it isn't. Java has it's own lazy loading of classes that will allow for lazy instantiation when the class is first needed: public class Singleton { private static Singleton instance = new Singleton (); private Singleton() { } public static ...


0

The reason they are often a code smell is that a Singleton is to create a global. If you require a state to be global to the application and stored for the life of the application, then Singletons might be a good idea. The main problem, is that you are removing the ability for the programmer to decide whether they want the state to be global or temporary. ...


0

If I understand your question correctly, then yes, to create a Singleton class, you have to use the private constructor as it is the only way to prevent another class from creating an instance of your class. If your question is another way of getting around getInstance(), then it is also possible to use a private static inner class and either use that to ...


3

Avoid the Gang of Four Singleton pattern, for reasons cited in the other answers. Mainly it is an anti-pattern based on difficulties it creates for testing. Factory and Dependency Injection made Singleton obsolete. The best answer is to use a Factory that decides whether to instantiate one instance, or many, of a given class. That way, the class and its ...


1

Singletons objects made so there can only be 1 instance at any given time and can be used application wide. Objects that handle stuff like connection pools are a good candidate to be Singletons; You only want 1 instance in the entire application you must be able to access it from different parts of the application the data it holds has to be persisted ...


4

A case where a static class might be a good idea is when you want to collect related pieces of functionality, but you don't need to have any internal state in any object. An example could be the Math class in Java. It contains a whole bunch of related functions that are accessed outside the context of any specific object instance. I've done similar things ...


0

You have to use the right tool for the right problem. MVC is used to separate the representation logic from routing request logic (controller) and business logic (the model). For creating objects you should use Creational Patterns. Now, in my opinion, If I initialize the xml attributes (reading and converting to class objects), I'm effectively doing ...



Top 50 recent answers are included