开发者

Are static classes the right way?

I am developing an application in Java, which manages a database of bank's loans. I have some expierience in application developing and I have a question that might be silly but it is something I always talk about since I've been learning developing languages and never got a conviencing answer.

In my program I have many classes and 开发者_JAVA百科methods, which I use as generic tools. For example, I have a class for Excel writing, a class for file reading/writing, a class which manipulates strings in various ways, a class to show Dialogs/Messages in a default format of mine, a class for specific mathematical functions (like Math), etc.

I cannot imagine those classes as real things (like classes/objects are meant to), but they come to my mind as toolboxes. That is why I make them static. In that way I write for example

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

rather than

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 a toolbox like something that is always there for me and I don't need to create an object everytime I want to use it. Isn't C++'s Math the same case and static also? I have discussed it with many of my colleagues and coding-friends and most of them say that I have to create an object because that's what object oriented programming is all about.

I understand what they said in another context: In my spare time I've been developing a card game using VB.net. There I had classes for game, player, deck, hand. The objects I made where many out of every class, because I have many games, players, decks, hands. And they can be imagined as real things. It is specifically very different when you are developing a game. The developing of a game is much more object oriented because it contains real things.

I was wondering, am I somehow terribly wrong here? I would like to hear oppinions about what oop is all about in depth. I would also like to hear what static classes are for.

Thanks


No, a class containing only static member functions would not be the right thing in this case, at least in my opinion. The second ExcelWriter class would be better. Anyway, where are you storing the file handle for your Excel output? Not in a static variable, I hope? That is a prime candidate for a data member for this class.

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

Sometimes, classes containing only static member functions can be useful as a way of grouping related functions, but this is rare and often means the 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.


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

OOP design is very easy when you think of what you're doing and using literally. For example in you're case, you used the word "myExcelWriter" which is an excel writer. So, if it IS something, it is an object. Which means it needs an instance.

Always just describe what you're doing to yourself, if what you're doing can be described by a noun then it's an object. If it's an adjective it's an attribute or class member. If it's a verb it's a method. If it's an adverb it's an attribute or a variable passed to the function.

Static classes are tools that contain functions that are independent , and just do some calculations with the input variables. (Again like Math, that just calculates 2 numbers)

Static functions in classes are tools that are used on objects of that class that don't need an Instance's state to work. (Like Int.Parse())

OOP's much closer to a real language IMO, that's why it's more intuitive to program this way.


Classes don't have to represent a real world thing. If your use of them makes your program easier to understand, you are doing it right.

That said, a class should have both data and methods. Your excel and file classes seem good to me, but be wary of a class that only has methods and no data.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜