Are static classes correct?

I am developing a Java application that manages a bank loan database. I have some experience in application development, and I have a question that might be silly, but this is what I always talk about, since I learn languages ​​and never received an answer to the pipeline.

In my program, I have many classes and methods that I use as common tools. For example, I have a class for writing to Excel, a class for reading / writing files, a class that manipulates strings differently, a class for displaying Dialogs / Messages in my default format, a class for certain mathematical functions (e.g. Math) and etc.

I cannot imagine these classes as real things (e.g. classes / objects), but they come to my mind as toolbars. That is why I make them static. So I write, for example,

excelWriter.create("C:\temp.xls"); excelWriter.append("mydata1\tmydata2\nmydata3\tmydata4"); excelWriter.close(); 

but not

 ExcelWriter myExcelWriter; myExcelWriter.create("C:\temp.xls"); myExcelWriter.append("mydata1\tmydata2\nmydata3\tmydata4"); myExcelWriter.close(); 

This suits me better for the reason that I think of the toolbox as something that is always there for me, and I don’t need to create an object every time I want to use it. Isn't C ++ Math the same case and statics too? I discussed this with many of my colleagues and fellow coders, and most of them say that I need to create an object because it is object oriented programming.

I understand what they said in a different context: In my free time, I developed a card game using VB.net. There I had classes for the game, player, deck, hands. The objects that I did where a lot of each class, because I have a lot of games, players, decks, hands. And they can be imagined as real things. This is especially different if you are developing a game. Development of the game is much oriented to objects, because it contains the real thing.

I was wondering, am I somehow terribly wrong here? I would like to hear opponents about what oop is. I would also like to know for what static classes.

thanks

+4
source share
3 answers

No, a class containing only static member functions will not be correct in this case, at least in my opinion. The second ExcelWriter class will be better. Anyway, where do you store the file descriptor for Excel output? Hopefully not in a static variable? This is the main candidate for the data member for this class.

Try to imagine in this case the object as representing the stream of output you are writing. In the future, you may need to open two output streams at the same time, which would not be possible in your "toolbox" class.

Sometimes classes containing only static member functions can be useful as a way to group related functions, but this rarely and often means that a class can be redesigned around an object containing data. Static member functions can be useful as a way to have different constructors for a class, for example.

+2
source

Static classes and functions should be used only for neutral functions and calculations. Only when you just need to get an answer to something.

The design of OOP is very simple when you think about what you are doing and using literally. For example, in your case, you used the word "myExcelWriter", which is the author of Excel. So, if this is something, this is an object. This means that he needs an instance.

Always just describe what you do for yourself if what you do can be described by a noun and then an object. If it is an adjective, it is an attribute or member of a class. If it is a verb, it is a method. If it is an adverb, it is an attribute or variable passed to the function.

Static classes are tools that contain independent functions and simply perform some calculations on input variables. (Again, like Math , which just calculates 2 numbers)

Static functions in classes are tools that are used for objects of this class that do not require instance state. (Like Int.Parse() )

OOP is much closer to the real language of IMO, therefore it is more intuitive to program this method.

+2
source

Classes should not represent the real thing. If your use of them simplifies the understanding of your program, you are doing everything right.

However, a class must have both data and methods. Your excel and file classes seem useful to me, but be careful with a class that only has methods and data.

+1
source

Source: https://habr.com/ru/post/1333174/


All Articles