Advantage of Static class over use of Singleton - class

Duplicate
What’s wrong with singleton?
Singletons: good design or a crutch?
Singleton: How should it be used
What is so bad about Singletons
You can find numerous reasons for using a Singleton over a Static class. But there must surely be some situations where it is better to use a static class before a Singleton. What are they?

You can use static class when:
1) all its methods are utilities (nice example - class Math)
2) you don't want to deal with preserving your instance from garbage collector (in applets), but I would better use singleton there
3) you are absolutely sure that it wouldn't become stateful in the future and you are sure that you will always need only one instance of that class
If you are using singleton and in one moment you realize that you need several instances then your singleton easily can be transformed to multitone, but you'll have a problem with static class

Having fought with the testability of consumers of static classes over the years I can honestly say that they are the work of evil minds. Seriously though, I'd use static classes for extention methods in C# but not really anywhere else.

If your class doesn't store any state, then use a Static class.
If it stores state and you require a single instance, then (maybe) use a Singleton.
Otherwise use a regular class.

Static class is better for when you don't need to change the implementation. With a Singleton, you can have an interface with various implementations. A Static class, can only be an implementation.

A singleton is a class of which only one instance can be instantiated, whereas there is no instance associated with a static method.
If you can implement the function you want with a single static method, then that is probably your best approach, because it is easier to implement. Consider extension methods - they are just static methods with syntactic sugar. If you can logically view the static method as a helper to an existing class, then it makes sense to use a static method.
On the other hand, if there is some sort of state involved in the functionality you are trying to achieve, then it is probably best to use a Singleton instead. The Singleton object can contain/manage its state and manage concurrent access/threading, whereas this becomes much more complicated with static classes and static methods. If you are using Singleton's in C#, I highly recommend reading Jon Skeet's article on proper Singleton implementation, which is available at http://www.yoda.arachsys.com/csharp/singleton.html .
Singleton's are more comparable to static classes than static methods. A big advantage that singletons have in this comparison is that they can implement interfaces and derive from base classes. This allows you to decouple their implementations from their interfaces. For example, if I have an interface IAccountService in my core assembly with a Singleton implementation, SingletonAspNetAccountService in my service layer, then I can inject the IAccountService into my UI layer with an IoC container, without requiring a dependency on my service layer in the UI layer. On the other hand, if I had a static Accounts class, then I would have to either create an adapter to the static class's methods or have a dependency on the service layer in my UI in order to access the static account functionality.

It's always where you don't actually need to pass the singleton instance anywhere.
For example, singleton will be useful if it implements some interface, you can't do it with a static class.
Remember, every Class instance is a singleton, managed by JVM.
So static class is a singleton.

Related

What is a thought process behind having static implementations in Interfaces

What could be the thought process behind having static implementations in Interfaces (Java-8)?
(As these static methods are not inherited by implementing classes nor static methods can be overridden)
The purpose could be different from different perspectives but what Oracle says is below:
This makes it easier for you to organize helper methods in your libraries; you can keep static methods specific to an interface in the same interface rather than in a separate class.
Basically, it allows putting utility methods like null check, string manipulations etc within the interface itself. This basically eliminates the need of writing Utility classes or Wrapper classes with utility features.
As a examples Collections utility class could go away and the utility methods could be put inside the interfaces itself.
For more read here at oracle.

In Swift OOP design, how do I arrange a commonly-used class?

I am new to Swift and OOP. For example, I have a class that manages the system-wide configurations.
class system_conf {
init()
getValue1()
getValue2()
...
setValue1()
setValue2()
...
reloadValues()
activateX()
activeteY()
...
}
This class should have only one instance and many other classes will use it. What's the recommended way for this case?
Should I pass around this instance?
Should I consider to use Singleton?
Should I use static functions directly?
Should I create a global instance, so every other class can access it directly?
or?
It seems your class is a configuration class. If you intend to pass it to a bunch of classes, you should wonder if you need to write unit tests for them.
If so, assuming you are either using a singleton or static methods or a global var, take a moment to think about how you would mock this configuration class for each of your tests. It's not easy, is it?
If your class is a kind of mediator, a global var or static methods are fine (or any other alternative you suggested). However, in your case, it would be better to pass your object in any initializer/constructor of each class using it. Then, testing would definitely be easier. Also, passing it via an interface is even better: you can mock it super easily (mock up libraries mostly work with interfaces only).
So there is no unique answer to your question. It is just a matter of compromises and scaling. If your app is small, any of the method you listed above is perfectly fine. However, if you app tends to get bigger, a proxy solution would be better for maintainability and testability.
If you fancy reading, you should glance at this article from Misko Hevery, especially this chapter.

In Objective-C, what's the advantage of having a singleton over a class method?

As above, but careful, I'm only interested in Objective-C context, so there is no point in pointing out the advantages of a singleton over a static methods.
I would say nothing. Singletons are often considered bad practice and this applies to Obejctive-C as well. One thing that might make singletons a better solution than class methods is that singletons are proper instances that can access instance variables, so if one needs the class to store data in ivars, singletons can be a solution. (But well, even implementing singletons often requires using static global or local variables - so strictly speaking, you can't really avoid them entirely, at most you can reduce their number to one.)
One significant advantage is class methods in Objective-C can't hold onto any data (unless declared as static within the method). Whereas with a singleton, you have access to all of the data of the single-instance

Are static classes and methods bad? Global variables frowned upon?

I have an application that has database connectivity and although there are obviously objects that correspond to data in my database, I find that all my data processing methods could be static as there is no real need for an instance of the object as my classes simply operate on the data and spit something out, no need to store anything outside the method's scope. If I can make a method or class static should I?
Also I use a utility singleton class for common (single instance) "global data". I want to have a good design, but are these frowned upon?
Let me give you an example of what I'm doing. I load some data from my database using a static method to place it into a global varaiable in my Singleton class (a list of a custom object)
So my singleton class has something like
List<MyCustomObject> SomeList
and my static class has
static void LoadData()
foreach(data in database something or other)
singletonClass.SomeList.Add()
So the code above might load in some records from the database into SomeList, where each item in SomeList is of type MyCustomObject, which contains a single record of information.
Is this good implementation? Is this how you would code it?
Then in my presentation layer I would make calls to another static class of methods to get data from the singleton class in to a format required.
It doesn't feel very OOPey. But I can't really think how to do it another way you do it.
Allow me to direct you toward an excellent article on this topic: Singletons are Pathological Liars.
The problem is that the need to call your LoadData() function isn't self-evident. Compare your situation to that described in the article and I think you'll see some parallels.
Statics and singletons are frowned upon somewhat. But only the same way as starting a sentence with “but” — bad when overused, but sometimes it's what works best.
In your example, why have separate classes, one a singleton and one static? A singleton is in many ways equivalent to a class with only static data and methods. If you already have a singleton, I'd say you should add the methods to load the data to it rather than to a separate class. A class with static methods would be more appropriate if, say, you have utility code common to all of your stored data types.
(Also, I wouldn't worry too much about what's OOPey and what's not. Overengineering in the blind service of OOP principles can be a serious problem, speaking as someone who's had to wade through the Eclipse code base …)
Singletons is one but static is another very big one.
OOP or not, static variables have many drawbacks but little coding convenience.
Can't determine exact allocation time, life span
Can't work well in multi-threaded
Future problem to program expansion
...

Pseudo-multiple-inheritance with extension methods on interfaces in C#?

Similar question but not quite the same thing
I was thinking that with extension methods in the same namespace as the interface you could get a similar effect to multiple inheritance in that you don't need to have duplicate code implementing the same interface the same way in 10 different classes.
What are some of the downsides of doing this? I think the pros are pretty obvious, it's the cons that usually come back to bite you later on.
One of the cons I see is that the extension methods can't be virtual, so you need to be sure that you actually do want them implemented the same way for every instance.
The problem that I see with building interface capability via extension methods is that you are no longer actually implementing the interface and so can't use the object as the interface type.
Say I have a method that takes an object of type IBar. If I implement the IBar interface on class Foo via extension methods, then Foo doesn't derive from IBar and can't be used interchangeably with it (Liskov Substitution principle). Sure, I get the behavior that I want added to Foo, but I lose the most important aspect of creating interfaces in the first place -- being able to define an abstract contract that can be implemented in a variety of ways by various classes so that dependent classes need not know about concrete implementations.
If I needed multiple inheritance (and so far I've lived without it) badly enough, I think I'd use composition instead to minimize the amount of code duplication.
A decent way to think about this is that instance methods are something done by the object, while extension methods are something done to the object. I am fairly certain the Framework Design Guidelines say you should implement an instance method whenever possible.
An interface declares "I care about using this functionality, but not how it is accomplished." That leaves implementers the freedom to choose the how. It decouples the intent, a public API, from the mechanism, a class with concrete code.
As this is the main benefit of interfaces, implementing them entirely as extension methods seems to defeat their purpose. Even IEnumerable<T> has an instance method.
Edit: Also, objects are meant to act on the data they contain. Extension methods can only see an object's public API (as they are just static methods); you would have to expose all of an object's state to make it work (an OO no-no).