U
U
unfapable2016-03-27 18:22:25
OOP
unfapable, 2016-03-27 18:22:25

When to use a static class and when alone?

Some people say that using a static class where the methods are all static is bad and you should use the singleton pattern instead, but why is that? What's the difference and what do you prefer?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
O
OnYourLips, 2016-03-27
@OnYourLips

What's the difference and what do you prefer?

Static classes are grouped collections of functions that don't have a single hint of state. It is used very rarely, an example is Math.
A singleton is an anti-pattern in which an object is limited to a single instance and made global.
More details about the shortcomings are written here: https://rsdn.ru/forum/design/2615563.1
In their pure form, they are also extremely rare, almost always come out sideways and rewritten. It's easier to write quality code from the very beginning.
Sometimes singletons are confused with facades.

D
Denis Zagaevsky, 2016-03-27
@zagayevskiy

If the class is utilitarian, such as Math from Java, where all methods are essentially functions and do not change the external state, then a static one is better. As soon as the class has a state (in the form of the same static data fields), it is worth making a singleton out of it. A singleton can implement some interfaces, and it can be used like a regular object.
It is also easy to turn a singleton into a non-singleton (that is, a class with many instances), if necessary.

A
AtomKrieg, 2016-03-28
@AtomKrieg

What interesting things have been written. Static classes in Java are needed because there are no functions in Java. In other languages, functions are grouped by namespace.
The difference between a static class and a singleton is that a static class is initialized at the start of the program, and a singleton is only initialized when it is accessed (in addition, a method can be added to the singleton to free memory from the instance).
For example, there is frequent functionality required by the user throughout the program, and you can make a static class. If the user rarely needs functionality and may need it in the middle or at the end of the program, then we choose the sington.

enum class ProgState {Init, Run, Release};
//вариант 1
bool isLastDoc = (App::state () == ProgState::Init);
App::LoadDocument ( isLastDoc ); 
//вариант 2
bool isLastDoc = (App::getInstance () -> state () == ProgState::Init);
App::getInstance() -> LoadLastDocument( isLastDoc );

Personally, I don’t like getInstance singletons throughout the program code, and static classes are so that you need to write the word static everywhere when describing the class and initiate all members of the class in the cpp file. So I do this:
//app.h
App& app (); //здесь можно и статик переменую, а не функцию
//app.cpp
App & app ()
{
  static App app;
  return app;
}
//в коде
bool isLastDoc = (app() -> state () == ProgState::Init);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question